From 86b3271cb637447f6e6abe6e38bf0fab4d6c3937 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Tue, 10 Oct 2017 12:47:22 +0100 Subject: [PATCH] fix(@ngtools/webpack): use `rendered` instead of hash in resource loader Using eval sourcemaps (#7919) the sourcemap will be included inside the source, and may change slightly even though the source has not. This busts the cache. A better way to determine if the entry changed is the `rendered` boolean. --- .../@ngtools/webpack/src/resource_loader.ts | 48 +++++++++---------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/packages/@ngtools/webpack/src/resource_loader.ts b/packages/@ngtools/webpack/src/resource_loader.ts index c5ecae3b63f1..8753ac37603f 100644 --- a/packages/@ngtools/webpack/src/resource_loader.ts +++ b/packages/@ngtools/webpack/src/resource_loader.ts @@ -5,21 +5,24 @@ const NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin'); const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin'); const LoaderTargetPlugin = require('webpack/lib/LoaderTargetPlugin'); const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin'); -const loaderUtils = require('loader-utils'); interface CompilationOutput { - hash: string; + rendered: boolean; outputName: string; source: string; - newSource?: string; +} + +interface CachedCompilation { + outputName: string; + evaluatedSource?: string; } export class WebpackResourceLoader { private _parentCompilation: any; private _context: string; private _uniqueId = 0; - private _cache = new Map(); + private _cache = new Map(); constructor() {} @@ -95,15 +98,13 @@ export class WebpackResourceLoader { } }); - const source = childCompilation.assets[outputName].source(); - resolve({ - // Hash of the source. - hash: loaderUtils.getHashDigest(source), + // Boolean showing if this entry was changed since the last compilation. + rendered: entries[0].rendered, // Output name. outputName, // Compiled code. - source + source: childCompilation.assets[outputName].source() }); } }); @@ -116,12 +117,12 @@ export class WebpackResourceLoader { const vmScript = new vm.Script(output.source, { filename: output.outputName }); // Evaluate code and cast to string - let newSource: string; - newSource = vmScript.runInContext(vmContext); + let evaluatedSource: string; + evaluatedSource = vmScript.runInContext(vmContext); - if (typeof newSource == 'string') { - this._cache.set(output.outputName, { ...output, newSource }); - return Promise.resolve(newSource); + if (typeof evaluatedSource == 'string') { + this._cache.set(output.outputName, { outputName: output.outputName, evaluatedSource }); + return Promise.resolve(evaluatedSource); } return Promise.reject('The loader "' + output.outputName + '" didn\'t return a string.'); @@ -132,17 +133,14 @@ export class WebpackResourceLoader { get(filePath: string): Promise { return this._compile(filePath) - .then((result: any) => { - // Check cache. - const outputName = result.outputName; - const output = this._cache.get(outputName); - if (output) { - if (output.hash === result.hash && output.newSource) { - // Return cached newSource. - return Promise.resolve(output.newSource); - } else { - // Delete cache entry. - this._cache.delete(outputName); + .then((result: CompilationOutput) => { + if (!result.rendered) { + // Check cache. + const outputName = result.outputName; + const cachedOutput = this._cache.get(outputName); + if (cachedOutput) { + // Return cached evaluatedSource. + return Promise.resolve(cachedOutput.evaluatedSource); } }