Skip to content

Commit 91ec9f2

Browse files
committed
fix(@ngtools/webpack): cleanup resources after modules are loaded
1 parent bd55bf0 commit 91ec9f2

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

packages/ngtools/webpack/src/angular_compiler_plugin.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export class AngularCompilerPlugin {
134134
private _program: (ts.Program | Program) | null;
135135
private _compilerHost: WebpackCompilerHost & CompilerHost;
136136
private _moduleResolutionCache: ts.ModuleResolutionCache;
137-
private _resourceLoader: WebpackResourceLoader;
137+
private _resourceLoader?: WebpackResourceLoader;
138138
// Contains `moduleImportPath#exportName` => `fullModulePath`.
139139
private _lazyRoutes: LazyRouteMap = Object.create(null);
140140
private _tsConfigPath: string;
@@ -580,10 +580,22 @@ export class AngularCompilerPlugin {
580580
}
581581

582582
// Registration hook for webpack plugin.
583+
// tslint:disable-next-line:no-big-function
583584
apply(compiler: Compiler & { watchMode?: boolean }) {
584585
// only present for webpack 4.23.0+, assume true otherwise
585586
const watchMode = compiler.watchMode === undefined ? true : compiler.watchMode;
586587

588+
// cleanup if not watching
589+
if (!watchMode) {
590+
compiler.hooks.thisCompilation.tap('angular-compiler', compilation => {
591+
compilation.hooks.finishModules.tap('angular-compiler', () => {
592+
this._program = null;
593+
this._transformers = [];
594+
this._resourceLoader = undefined;
595+
});
596+
});
597+
}
598+
587599
// Decorate inputFileSystem to serve contents of CompilerHost.
588600
// Use decorated inputFileSystem in watchFileSystem.
589601
compiler.hooks.environment.tap('angular-compiler', () => {
@@ -631,9 +643,11 @@ export class AngularCompilerPlugin {
631643
this._options.directTemplateLoading,
632644
);
633645

634-
// Create and set a new WebpackResourceLoader.
635-
this._resourceLoader = new WebpackResourceLoader();
636-
webpackCompilerHost.setResourceLoader(this._resourceLoader);
646+
// Create and set a new WebpackResourceLoader in AOT
647+
if (!this._JitMode) {
648+
this._resourceLoader = new WebpackResourceLoader();
649+
webpackCompilerHost.setResourceLoader(this._resourceLoader);
650+
}
637651

638652
// Use the WebpackCompilerHost with a resource loader to create an AngularCompilerHost.
639653
this._compilerHost = createCompilerHost({
@@ -787,7 +801,9 @@ export class AngularCompilerPlugin {
787801
(compilation as any)._ngToolsWebpackPluginInstance = this;
788802

789803
// Update the resource loader with the new webpack compilation.
790-
this._resourceLoader.update(compilation);
804+
if (this._resourceLoader) {
805+
this._resourceLoader.update(compilation);
806+
}
791807

792808
try {
793809
await this._update();
@@ -1012,6 +1028,10 @@ export class AngularCompilerPlugin {
10121028
}
10131029

10141030
getResourceDependencies(fileName: string): string[] {
1031+
if (!this._resourceLoader) {
1032+
return [];
1033+
}
1034+
10151035
return this._resourceLoader.getResourceDependencies(fileName);
10161036
}
10171037

0 commit comments

Comments
 (0)