Skip to content

Commit

Permalink
fix(@ngtools/webpack): cleanup resources after modules are loaded (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
clydin authored and vikerman committed Nov 30, 2018
1 parent 717b02f commit 1ad42db
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions packages/ngtools/webpack/src/angular_compiler_plugin.ts
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

0 comments on commit 1ad42db

Please sign in to comment.