Skip to content

Commit 9e39696

Browse files
committed
feat(@ngtools/webpack): reuse compiler host in webpack child compilations
1 parent 03d238f commit 9e39696

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

packages/ngtools/webpack/src/angular_compiler_plugin.ts

+24-2
Original file line numberDiff line numberDiff line change
@@ -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;

packages/ngtools/webpack/src/virtual_file_system_decorator.ts

+4
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)