@@ -596,7 +596,7 @@ export class AngularCompilerPlugin {
596
596
597
597
// Registration hook for webpack plugin.
598
598
// tslint:disable-next-line:no-big-function
599
- apply ( compiler : Compiler & { watchMode ?: boolean } ) {
599
+ apply ( compiler : Compiler & { watchMode ?: boolean , parentCompilation ?: compilation . Compilation } ) {
600
600
// The below is require by NGCC processor
601
601
// since we need to know which fields we need to process
602
602
compiler . hooks . environment . tap ( 'angular-compiler' , ( ) => {
@@ -610,8 +610,14 @@ export class AngularCompilerPlugin {
610
610
// cleanup if not watching
611
611
compiler . hooks . thisCompilation . tap ( 'angular-compiler' , compilation => {
612
612
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
+
613
619
// 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 ;
615
621
if ( ! watchMode ) {
616
622
this . _program = null ;
617
623
this . _transformers = [ ] ;
@@ -859,6 +865,22 @@ export class AngularCompilerPlugin {
859
865
throw new Error ( 'An @ngtools/webpack plugin already exist for this compilation.' ) ;
860
866
}
861
867
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
+
862
884
// Set a private variable for this plugin instance.
863
885
// tslint:disable-next-line:no-any
864
886
( compilation as any ) . _ngToolsWebpackPluginInstance = this ;
0 commit comments