From 4b0378ec2617137e0120001d69acf8e9eca1ccc9 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Mon, 19 Nov 2018 11:43:26 -0500 Subject: [PATCH] fix(@ngtools/webpack): cleanup resources after modules are loaded --- .../webpack/src/angular_compiler_plugin.ts | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/packages/ngtools/webpack/src/angular_compiler_plugin.ts b/packages/ngtools/webpack/src/angular_compiler_plugin.ts index 3aa428ac97c9..ef9ad5c1ed0f 100644 --- a/packages/ngtools/webpack/src/angular_compiler_plugin.ts +++ b/packages/ngtools/webpack/src/angular_compiler_plugin.ts @@ -134,7 +134,7 @@ export class AngularCompilerPlugin { private _program: (ts.Program | Program) | null; private _compilerHost: WebpackCompilerHost & CompilerHost; private _moduleResolutionCache: ts.ModuleResolutionCache; - private _resourceLoader: WebpackResourceLoader; + private _resourceLoader?: WebpackResourceLoader; // Contains `moduleImportPath#exportName` => `fullModulePath`. private _lazyRoutes: LazyRouteMap = Object.create(null); private _tsConfigPath: string; @@ -580,9 +580,20 @@ export class AngularCompilerPlugin { } // Registration hook for webpack plugin. + // tslint:disable-next-line:no-big-function apply(compiler: Compiler & { watchMode?: boolean }) { - // only present for webpack 4.23.0+, assume true otherwise - const watchMode = compiler.watchMode === undefined ? true : compiler.watchMode; + // cleanup if not watching + compiler.hooks.thisCompilation.tap('angular-compiler', compilation => { + compilation.hooks.finishModules.tap('angular-compiler', () => { + // only present for webpack 4.23.0+, assume true otherwise + const watchMode = compiler.watchMode === undefined ? true : compiler.watchMode; + if (!watchMode) { + this._program = null; + this._transformers = []; + this._resourceLoader = undefined; + } + }); + }); // Decorate inputFileSystem to serve contents of CompilerHost. // Use decorated inputFileSystem in watchFileSystem. @@ -622,6 +633,9 @@ export class AngularCompilerPlugin { } } + // only present for webpack 4.23.0+, assume true otherwise + const watchMode = compiler.watchMode === undefined ? true : compiler.watchMode; + // Create the webpack compiler host. const webpackCompilerHost = new WebpackCompilerHost( this._compilerOptions, @@ -631,9 +645,11 @@ export class AngularCompilerPlugin { this._options.directTemplateLoading, ); - // Create and set a new WebpackResourceLoader. - this._resourceLoader = new WebpackResourceLoader(); - webpackCompilerHost.setResourceLoader(this._resourceLoader); + // Create and set a new WebpackResourceLoader in AOT + if (!this._JitMode) { + this._resourceLoader = new WebpackResourceLoader(); + webpackCompilerHost.setResourceLoader(this._resourceLoader); + } // Use the WebpackCompilerHost with a resource loader to create an AngularCompilerHost. this._compilerHost = createCompilerHost({ @@ -787,7 +803,9 @@ export class AngularCompilerPlugin { (compilation as any)._ngToolsWebpackPluginInstance = this; // Update the resource loader with the new webpack compilation. - this._resourceLoader.update(compilation); + if (this._resourceLoader) { + this._resourceLoader.update(compilation); + } try { await this._update(); @@ -1012,6 +1030,10 @@ export class AngularCompilerPlugin { } getResourceDependencies(fileName: string): string[] { + if (!this._resourceLoader) { + return []; + } + return this._resourceLoader.getResourceDependencies(fileName); }