@@ -596,7 +596,7 @@ export class AngularCompilerPlugin {
596596
597597 // Registration hook for webpack plugin.
598598 // tslint:disable-next-line:no-big-function
599- apply ( compiler : Compiler & { watchMode ?: boolean } ) {
599+ apply ( compiler : Compiler & { watchMode ?: boolean , parentCompilation ?: compilation . Compilation } ) {
600600 // The below is require by NGCC processor
601601 // since we need to know which fields we need to process
602602 compiler . hooks . environment . tap ( 'angular-compiler' , ( ) => {
@@ -610,8 +610,14 @@ export class AngularCompilerPlugin {
610610 // cleanup if not watching
611611 compiler . hooks . thisCompilation . tap ( 'angular-compiler' , compilation => {
612612 compilation . hooks . finishModules . tap ( 'angular-compiler' , ( ) => {
613+ let rootCompiler = compiler ;
614+ while ( rootCompiler . parentCompilation ) {
615+ // tslint:disable-next-line:no-any
616+ rootCompiler = compiler . parentCompilation as any ;
617+ }
618+
613619 // only present for webpack 4.23.0+, assume true otherwise
614- const watchMode = compiler . watchMode === undefined ? true : compiler . watchMode ;
620+ const watchMode = rootCompiler . watchMode === undefined ? true : rootCompiler . watchMode ;
615621 if ( ! watchMode ) {
616622 this . _program = null ;
617623 this . _transformers = [ ] ;
@@ -859,6 +865,22 @@ export class AngularCompilerPlugin {
859865 throw new Error ( 'An @ngtools/webpack plugin already exist for this compilation.' ) ;
860866 }
861867
868+ // If there is no compiler host at this point, it means that the environment hook did not run.
869+ // This happens in child compilations that inherit the parent compilation file system.
870+ if ( this . _compilerHost === undefined ) {
871+ const inputFs = compilation . compiler . inputFileSystem as VirtualFileSystemDecorator ;
872+ if ( ! inputFs . getWebpackCompilerHost ) {
873+ throw new Error ( 'AngularCompilerPlugin is running in a child compilation, but could' +
874+ 'not find a WebpackCompilerHost in the parent compilation.' ) ;
875+ }
876+
877+ // Use the existing WebpackCompilerHost to ensure builds and rebuilds work.
878+ this . _compilerHost = createCompilerHost ( {
879+ options : this . _compilerOptions ,
880+ tsHost : inputFs . getWebpackCompilerHost ( ) ,
881+ } ) as CompilerHost & WebpackCompilerHost ;
882+ }
883+
862884 // Set a private variable for this plugin instance.
863885 // tslint:disable-next-line:no-any
864886 ( compilation as any ) . _ngToolsWebpackPluginInstance = this ;
0 commit comments