Skip to content

Commit 64b4b03

Browse files
clydinfilipesilva
authored andcommitted
fix(@ngtools/webpack): avoid caching binary resources as UTF8
1 parent 9b17c43 commit 64b4b03

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

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

+2-18
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ export class WebpackCompilerHost implements ts.CompilerHost {
9797
private _delegate: ts.CompilerHost;
9898
private _files: {[path: string]: VirtualFileStats | null} = Object.create(null);
9999
private _directories: {[path: string]: VirtualDirStats | null} = Object.create(null);
100-
private _cachedResources: {[path: string]: string | undefined} = Object.create(null);
101100

102101
private _changedFiles: {[path: string]: boolean} = Object.create(null);
103102
private _changedDirs: {[path: string]: boolean} = Object.create(null);
@@ -174,8 +173,8 @@ export class WebpackCompilerHost implements ts.CompilerHost {
174173
fileName = this.resolve(fileName);
175174
if (fileName in this._files) {
176175
this._files[fileName] = null;
177-
this._changedFiles[fileName] = true;
178176
}
177+
this._changedFiles[fileName] = true;
179178
}
180179

181180
fileExists(fileName: string, delegate = true): boolean {
@@ -299,22 +298,7 @@ export class WebpackCompilerHost implements ts.CompilerHost {
299298
if (this._resourceLoader) {
300299
// These paths are meant to be used by the loader so we must denormalize them.
301300
const denormalizedFileName = this.denormalizePath(fileName);
302-
const resourceDeps = this._resourceLoader.getResourceDependencies(denormalizedFileName);
303-
304-
if (this._cachedResources[fileName] === undefined
305-
|| resourceDeps.some((dep) => this._changedFiles[this.resolve(dep)])) {
306-
return this._resourceLoader.get(denormalizedFileName)
307-
.then((resource) => {
308-
// Add resource dependencies to the compiler host file list.
309-
// This way we can check the changed files list to determine whether to use cache.
310-
this._resourceLoader.getResourceDependencies(denormalizedFileName)
311-
.forEach((dep) => this.readFile(dep));
312-
this._cachedResources[fileName] = resource;
313-
return resource;
314-
});
315-
} else {
316-
return this._cachedResources[fileName];
317-
}
301+
return this._resourceLoader.get(denormalizedFileName);
318302
} else {
319303
return this.readFile(fileName);
320304
}

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

+17-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class WebpackResourceLoader {
1818
private _context: string;
1919
private _uniqueId = 0;
2020
private _resourceDependencies = new Map<string, string[]>();
21+
private _cachedResources = new Map<string, string>();
2122

2223
constructor() {}
2324

@@ -69,6 +70,11 @@ export class WebpackResourceLoader {
6970

7071
childCompiler.plugin('this-compilation', (compilation: any) => {
7172
compilation.plugin('additional-assets', (callback: (err?: Error) => void) => {
73+
if (this._cachedResources.has(compilation.fullHash)) {
74+
callback();
75+
return;
76+
}
77+
7278
const asset = compilation.assets[filePath];
7379
if (asset) {
7480
this._evaluate({ outputName: filePath, source: asset.source() })
@@ -104,12 +110,17 @@ export class WebpackResourceLoader {
104110
// Save the dependencies for this resource.
105111
this._resourceDependencies.set(filePath, childCompilation.fileDependencies);
106112

107-
resolve({
108-
// Output name.
109-
outputName: filePath,
110-
// Compiled code.
111-
source: childCompilation.assets[filePath].source()
112-
});
113+
const compilationHash = childCompilation.fullHash;
114+
if (this._cachedResources.has(compilationHash)) {
115+
resolve({
116+
outputName: filePath,
117+
source: this._cachedResources.get(compilationHash),
118+
});
119+
} else {
120+
const source = childCompilation.assets[filePath].source();
121+
this._cachedResources.set(compilationHash, source);
122+
resolve({ outputName: filePath, source });
123+
}
113124
}
114125
});
115126
});

0 commit comments

Comments
 (0)