Skip to content

Commit 131b1d0

Browse files
filipesilvahansl
authored andcommitted
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.
1 parent 2e1c966 commit 131b1d0

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

packages/@ngtools/webpack/src/resource_loader.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@ const NodeTemplatePlugin = require('webpack/lib/node/NodeTemplatePlugin');
55
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
66
const LoaderTargetPlugin = require('webpack/lib/LoaderTargetPlugin');
77
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
8-
const loaderUtils = require('loader-utils');
98

109

1110
interface CompilationOutput {
12-
hash: string;
11+
rendered: boolean;
1312
outputName: string;
1413
source: string;
15-
newSource?: string;
14+
}
15+
16+
interface CachedCompilation {
17+
outputName: string;
18+
evaluatedSource?: string;
1619
}
1720

1821
export class WebpackResourceLoader {
1922
private _parentCompilation: any;
2023
private _context: string;
2124
private _uniqueId = 0;
22-
private _cache = new Map<string, CompilationOutput>();
25+
private _cache = new Map<string, CachedCompilation>();
2326

2427
constructor() {}
2528

@@ -95,15 +98,13 @@ export class WebpackResourceLoader {
9598
}
9699
});
97100

98-
const source = childCompilation.assets[outputName].source();
99-
100101
resolve({
101-
// Hash of the source.
102-
hash: loaderUtils.getHashDigest(source),
102+
// Boolean showing if this entry was changed since the last compilation.
103+
rendered: entries[0].rendered,
103104
// Output name.
104105
outputName,
105106
// Compiled code.
106-
source
107+
source: childCompilation.assets[outputName].source()
107108
});
108109
}
109110
});
@@ -116,12 +117,12 @@ export class WebpackResourceLoader {
116117
const vmScript = new vm.Script(output.source, { filename: output.outputName });
117118

118119
// Evaluate code and cast to string
119-
let newSource: string;
120-
newSource = vmScript.runInContext(vmContext);
120+
let evaluatedSource: string;
121+
evaluatedSource = vmScript.runInContext(vmContext);
121122

122-
if (typeof newSource == 'string') {
123-
this._cache.set(output.outputName, { ...output, newSource });
124-
return Promise.resolve(newSource);
123+
if (typeof evaluatedSource == 'string') {
124+
this._cache.set(output.outputName, { outputName: output.outputName, evaluatedSource });
125+
return Promise.resolve(evaluatedSource);
125126
}
126127

127128
return Promise.reject('The loader "' + output.outputName + '" didn\'t return a string.');
@@ -132,17 +133,14 @@ export class WebpackResourceLoader {
132133

133134
get(filePath: string): Promise<string> {
134135
return this._compile(filePath)
135-
.then((result: any) => {
136-
// Check cache.
137-
const outputName = result.outputName;
138-
const output = this._cache.get(outputName);
139-
if (output) {
140-
if (output.hash === result.hash && output.newSource) {
141-
// Return cached newSource.
142-
return Promise.resolve(output.newSource);
143-
} else {
144-
// Delete cache entry.
145-
this._cache.delete(outputName);
136+
.then((result: CompilationOutput) => {
137+
if (!result.rendered) {
138+
// Check cache.
139+
const outputName = result.outputName;
140+
const cachedOutput = this._cache.get(outputName);
141+
if (cachedOutput) {
142+
// Return cached evaluatedSource.
143+
return Promise.resolve(cachedOutput.evaluatedSource);
146144
}
147145
}
148146

0 commit comments

Comments
 (0)