@@ -608,12 +608,18 @@ export class AngularCompilerPlugin {
608608
609609 // Registration hook for webpack plugin.
610610 // tslint:disable-next-line:no-big-function
611- apply ( compiler : Compiler & { watchMode ?: boolean } ) {
611+ apply ( compiler : Compiler & { watchMode ?: boolean , parentCompilation ?: compilation . Compilation } ) {
612612 // cleanup if not watching
613613 compiler . hooks . thisCompilation . tap ( 'angular-compiler' , compilation => {
614614 compilation . hooks . finishModules . tap ( 'angular-compiler' , ( ) => {
615+ let rootCompiler = compiler ;
616+ while ( rootCompiler . parentCompilation ) {
617+ // tslint:disable-next-line:no-any
618+ rootCompiler = compiler . parentCompilation as any ;
619+ }
620+
615621 // only present for webpack 4.23.0+, assume true otherwise
616- const watchMode = compiler . watchMode === undefined ? true : compiler . watchMode ;
622+ const watchMode = rootCompiler . watchMode === undefined ? true : rootCompiler . watchMode ;
617623 if ( ! watchMode ) {
618624 this . _program = null ;
619625 this . _transformers = [ ] ;
@@ -822,6 +828,22 @@ export class AngularCompilerPlugin {
822828 throw new Error ( 'An @ngtools/webpack plugin already exist for this compilation.' ) ;
823829 }
824830
831+ // If there is no compiler host at this point, it means that the environment hook did not run.
832+ // This happens in child compilations that inherit the parent compilation file system.
833+ if ( this . _compilerHost === undefined ) {
834+ const inputFs = compilation . compiler . inputFileSystem as VirtualFileSystemDecorator ;
835+ if ( ! inputFs . getWebpackCompilerHost ) {
836+ throw new Error ( 'AngularCompilerPlugin is running in a child compilation, but could' +
837+ 'not find a WebpackCompilerHost in the parent compilation.' ) ;
838+ }
839+
840+ // Use the existing WebpackCompilerHost to ensure builds and rebuilds work.
841+ this . _compilerHost = createCompilerHost ( {
842+ options : this . _compilerOptions ,
843+ tsHost : inputFs . getWebpackCompilerHost ( ) ,
844+ } ) as CompilerHost & WebpackCompilerHost ;
845+ }
846+
825847 // Set a private variable for this plugin instance.
826848 // tslint:disable-next-line:no-any
827849 ( compilation as any ) . _ngToolsWebpackPluginInstance = this ;
0 commit comments