Skip to content

Commit 61e4afe

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

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
@@ -133,7 +133,7 @@ export class AngularCompilerPlugin {
133133
private _program: (ts.Program | Program) | null;
134134
private _compilerHost: WebpackCompilerHost & CompilerHost;
135135
private _moduleResolutionCache: ts.ModuleResolutionCache;
136-
private _resourceLoader: WebpackResourceLoader;
136+
private _resourceLoader?: WebpackResourceLoader;
137137
// Contains `moduleImportPath#exportName` => `fullModulePath`.
138138
private _lazyRoutes: LazyRouteMap = Object.create(null);
139139
private _tsConfigPath: string;
@@ -579,9 +579,20 @@ export class AngularCompilerPlugin {
579579
}
580580

581581
// Registration hook for webpack plugin.
582+
// tslint:disable-next-line:no-big-function
582583
apply(compiler: Compiler & { watchMode?: boolean }) {
583-
// only present for webpack 4.23.0+, assume true otherwise
584-
const watchMode = compiler.watchMode === undefined ? true : compiler.watchMode;
584+
// cleanup if not watching
585+
compiler.hooks.thisCompilation.tap('angular-compiler', compilation => {
586+
compilation.hooks.finishModules.tap('angular-compiler', () => {
587+
// only present for webpack 4.23.0+, assume true otherwise
588+
const watchMode = compiler.watchMode === undefined ? true : compiler.watchMode;
589+
if (!watchMode) {
590+
this._program = null;
591+
this._transformers = [];
592+
this._resourceLoader = undefined;
593+
}
594+
});
595+
});
585596

586597
// Decorate inputFileSystem to serve contents of CompilerHost.
587598
// Use decorated inputFileSystem in watchFileSystem.
@@ -621,6 +632,9 @@ export class AngularCompilerPlugin {
621632
}
622633
}
623634

635+
// only present for webpack 4.23.0+, assume true otherwise
636+
const watchMode = compiler.watchMode === undefined ? true : compiler.watchMode;
637+
624638
// Create the webpack compiler host.
625639
const webpackCompilerHost = new WebpackCompilerHost(
626640
this._compilerOptions,
@@ -629,9 +643,11 @@ export class AngularCompilerPlugin {
629643
watchMode,
630644
);
631645

632-
// Create and set a new WebpackResourceLoader.
633-
this._resourceLoader = new WebpackResourceLoader();
634-
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+
}
635651

636652
// Use the WebpackCompilerHost with a resource loader to create an AngularCompilerHost.
637653
this._compilerHost = createCompilerHost({
@@ -785,7 +801,9 @@ export class AngularCompilerPlugin {
785801
(compilation as any)._ngToolsWebpackPluginInstance = this;
786802

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

790808
try {
791809
await this._update();
@@ -1010,6 +1028,10 @@ export class AngularCompilerPlugin {
10101028
}
10111029

10121030
getResourceDependencies(fileName: string): string[] {
1031+
if (!this._resourceLoader) {
1032+
return [];
1033+
}
1034+
10131035
return this._resourceLoader.getResourceDependencies(fileName);
10141036
}
10151037

0 commit comments

Comments
 (0)