@@ -608,12 +608,18 @@ export class AngularCompilerPlugin {
608
608
609
609
// Registration hook for webpack plugin.
610
610
// tslint:disable-next-line:no-big-function
611
- apply ( compiler : Compiler & { watchMode ?: boolean } ) {
611
+ apply ( compiler : Compiler & { watchMode ?: boolean , parentCompilation ?: compilation . Compilation } ) {
612
612
// cleanup if not watching
613
613
compiler . hooks . thisCompilation . tap ( 'angular-compiler' , compilation => {
614
614
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
+
615
621
// 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 ;
617
623
if ( ! watchMode ) {
618
624
this . _program = null ;
619
625
this . _transformers = [ ] ;
@@ -822,6 +828,22 @@ export class AngularCompilerPlugin {
822
828
throw new Error ( 'An @ngtools/webpack plugin already exist for this compilation.' ) ;
823
829
}
824
830
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
+
825
847
// Set a private variable for this plugin instance.
826
848
// tslint:disable-next-line:no-any
827
849
( compilation as any ) . _ngToolsWebpackPluginInstance = this ;
0 commit comments