Skip to content

Commit

Permalink
feat(@ngtools/webpack): reuse compiler host in webpack child compilat…
Browse files Browse the repository at this point in the history
…ions
filipesilva authored and mgechev committed Apr 2, 2019
1 parent 1810394 commit 1d66e55
Showing 2 changed files with 28 additions and 2 deletions.
26 changes: 24 additions & 2 deletions packages/ngtools/webpack/src/angular_compiler_plugin.ts
Original file line number Diff line number Diff line change
@@ -596,7 +596,7 @@ export class AngularCompilerPlugin {

// Registration hook for webpack plugin.
// tslint:disable-next-line:no-big-function
apply(compiler: Compiler & { watchMode?: boolean }) {
apply(compiler: Compiler & { watchMode?: boolean, parentCompilation?: compilation.Compilation }) {
// The below is require by NGCC processor
// since we need to know which fields we need to process
compiler.hooks.environment.tap('angular-compiler', () => {
@@ -610,8 +610,14 @@ export class AngularCompilerPlugin {
// cleanup if not watching
compiler.hooks.thisCompilation.tap('angular-compiler', compilation => {
compilation.hooks.finishModules.tap('angular-compiler', () => {
let rootCompiler = compiler;
while (rootCompiler.parentCompilation) {
// tslint:disable-next-line:no-any
rootCompiler = compiler.parentCompilation as any;
}

// only present for webpack 4.23.0+, assume true otherwise
const watchMode = compiler.watchMode === undefined ? true : compiler.watchMode;
const watchMode = rootCompiler.watchMode === undefined ? true : rootCompiler.watchMode;
if (!watchMode) {
this._program = null;
this._transformers = [];
@@ -859,6 +865,22 @@ export class AngularCompilerPlugin {
throw new Error('An @ngtools/webpack plugin already exist for this compilation.');
}

// If there is no compiler host at this point, it means that the environment hook did not run.
// This happens in child compilations that inherit the parent compilation file system.
if (this._compilerHost === undefined) {
const inputFs = compilation.compiler.inputFileSystem as VirtualFileSystemDecorator;
if (!inputFs.getWebpackCompilerHost) {
throw new Error('AngularCompilerPlugin is running in a child compilation, but could' +
'not find a WebpackCompilerHost in the parent compilation.');
}

// Use the existing WebpackCompilerHost to ensure builds and rebuilds work.
this._compilerHost = createCompilerHost({
options: this._compilerOptions,
tsHost: inputFs.getWebpackCompilerHost(),
}) as CompilerHost & WebpackCompilerHost;
}

// Set a private variable for this plugin instance.
// tslint:disable-next-line:no-any
(compilation as any)._ngToolsWebpackPluginInstance = this;
4 changes: 4 additions & 0 deletions packages/ngtools/webpack/src/virtual_file_system_decorator.ts
Original file line number Diff line number Diff line change
@@ -21,6 +21,10 @@ export class VirtualFileSystemDecorator implements InputFileSystem {
private _webpackCompilerHost: WebpackCompilerHost,
) { }

getWebpackCompilerHost() {
return this._webpackCompilerHost;
}

getVirtualFilesPaths() {
return this._webpackCompilerHost.getNgFactoryPaths();
}

0 comments on commit 1d66e55

Please sign in to comment.