Skip to content

Commit 1d66e55

Browse files
filipesilvamgechev
authored andcommitted
feat(@ngtools/webpack): reuse compiler host in webpack child compilations
1 parent 1810394 commit 1d66e55

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

packages/ngtools/webpack/src/angular_compiler_plugin.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

packages/ngtools/webpack/src/virtual_file_system_decorator.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export class VirtualFileSystemDecorator implements InputFileSystem {
2121
private _webpackCompilerHost: WebpackCompilerHost,
2222
) { }
2323

24+
getWebpackCompilerHost() {
25+
return this._webpackCompilerHost;
26+
}
27+
2428
getVirtualFilesPaths() {
2529
return this._webpackCompilerHost.getNgFactoryPaths();
2630
}

0 commit comments

Comments
 (0)