Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@ngtools/webpack): cleanup resources after modules are loaded #12994

Merged
merged 1 commit into from
Nov 30, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 29 additions & 7 deletions packages/ngtools/webpack/src/angular_compiler_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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({
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -1012,6 +1030,10 @@ export class AngularCompilerPlugin {
}

getResourceDependencies(fileName: string): string[] {
if (!this._resourceLoader) {
return [];
}

return this._resourceLoader.getResourceDependencies(fileName);
}

Expand Down