Skip to content

Commit 1ad42db

Browse files
clydinvikerman
authored andcommitted
fix(@ngtools/webpack): cleanup resources after modules are loaded (#12994)
1 parent 717b02f commit 1ad42db

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

packages/ngtools/webpack/src/angular_compiler_plugin.ts

Lines changed: 29 additions & 7 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,9 +580,20 @@ 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 }) {
584-
// only present for webpack 4.23.0+, assume true otherwise
585-
const watchMode = compiler.watchMode === undefined ? true : compiler.watchMode;
585+
// cleanup if not watching
586+
compiler.hooks.thisCompilation.tap('angular-compiler', compilation => {
587+
compilation.hooks.finishModules.tap('angular-compiler', () => {
588+
// only present for webpack 4.23.0+, assume true otherwise
589+
const watchMode = compiler.watchMode === undefined ? true : compiler.watchMode;
590+
if (!watchMode) {
591+
this._program = null;
592+
this._transformers = [];
593+
this._resourceLoader = undefined;
594+
}
595+
});
596+
});
586597

587598
// Decorate inputFileSystem to serve contents of CompilerHost.
588599
// Use decorated inputFileSystem in watchFileSystem.
@@ -622,6 +633,9 @@ export class AngularCompilerPlugin {
622633
}
623634
}
624635

636+
// only present for webpack 4.23.0+, assume true otherwise
637+
const watchMode = compiler.watchMode === undefined ? true : compiler.watchMode;
638+
625639
// Create the webpack compiler host.
626640
const webpackCompilerHost = new WebpackCompilerHost(
627641
this._compilerOptions,
@@ -631,9 +645,11 @@ export class AngularCompilerPlugin {
631645
this._options.directTemplateLoading,
632646
);
633647

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

638654
// Use the WebpackCompilerHost with a resource loader to create an AngularCompilerHost.
639655
this._compilerHost = createCompilerHost({
@@ -787,7 +803,9 @@ export class AngularCompilerPlugin {
787803
(compilation as any)._ngToolsWebpackPluginInstance = this;
788804

789805
// Update the resource loader with the new webpack compilation.
790-
this._resourceLoader.update(compilation);
806+
if (this._resourceLoader) {
807+
this._resourceLoader.update(compilation);
808+
}
791809

792810
try {
793811
await this._update();
@@ -1012,6 +1030,10 @@ export class AngularCompilerPlugin {
10121030
}
10131031

10141032
getResourceDependencies(fileName: string): string[] {
1033+
if (!this._resourceLoader) {
1034+
return [];
1035+
}
1036+
10151037
return this._resourceLoader.getResourceDependencies(fileName);
10161038
}
10171039

0 commit comments

Comments
 (0)