Skip to content

Commit

Permalink
fix(@ngtools/webpack): avoid caching binary resources as UTF8
Browse files Browse the repository at this point in the history
  • Loading branch information
clydin authored and filipesilva committed Dec 21, 2017
1 parent 9b17c43 commit 64b4b03
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
20 changes: 2 additions & 18 deletions packages/@ngtools/webpack/src/compiler_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export class WebpackCompilerHost implements ts.CompilerHost {
private _delegate: ts.CompilerHost;
private _files: {[path: string]: VirtualFileStats | null} = Object.create(null);
private _directories: {[path: string]: VirtualDirStats | null} = Object.create(null);
private _cachedResources: {[path: string]: string | undefined} = Object.create(null);

private _changedFiles: {[path: string]: boolean} = Object.create(null);
private _changedDirs: {[path: string]: boolean} = Object.create(null);
Expand Down Expand Up @@ -174,8 +173,8 @@ export class WebpackCompilerHost implements ts.CompilerHost {
fileName = this.resolve(fileName);
if (fileName in this._files) {
this._files[fileName] = null;
this._changedFiles[fileName] = true;
}
this._changedFiles[fileName] = true;
}

fileExists(fileName: string, delegate = true): boolean {
Expand Down Expand Up @@ -299,22 +298,7 @@ export class WebpackCompilerHost implements ts.CompilerHost {
if (this._resourceLoader) {
// These paths are meant to be used by the loader so we must denormalize them.
const denormalizedFileName = this.denormalizePath(fileName);
const resourceDeps = this._resourceLoader.getResourceDependencies(denormalizedFileName);

if (this._cachedResources[fileName] === undefined
|| resourceDeps.some((dep) => this._changedFiles[this.resolve(dep)])) {
return this._resourceLoader.get(denormalizedFileName)
.then((resource) => {
// Add resource dependencies to the compiler host file list.
// This way we can check the changed files list to determine whether to use cache.
this._resourceLoader.getResourceDependencies(denormalizedFileName)
.forEach((dep) => this.readFile(dep));
this._cachedResources[fileName] = resource;
return resource;
});
} else {
return this._cachedResources[fileName];
}
return this._resourceLoader.get(denormalizedFileName);
} else {
return this.readFile(fileName);
}
Expand Down
23 changes: 17 additions & 6 deletions packages/@ngtools/webpack/src/resource_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class WebpackResourceLoader {
private _context: string;
private _uniqueId = 0;
private _resourceDependencies = new Map<string, string[]>();
private _cachedResources = new Map<string, string>();

constructor() {}

Expand Down Expand Up @@ -69,6 +70,11 @@ export class WebpackResourceLoader {

childCompiler.plugin('this-compilation', (compilation: any) => {
compilation.plugin('additional-assets', (callback: (err?: Error) => void) => {
if (this._cachedResources.has(compilation.fullHash)) {
callback();
return;
}

const asset = compilation.assets[filePath];
if (asset) {
this._evaluate({ outputName: filePath, source: asset.source() })
Expand Down Expand Up @@ -104,12 +110,17 @@ export class WebpackResourceLoader {
// Save the dependencies for this resource.
this._resourceDependencies.set(filePath, childCompilation.fileDependencies);

resolve({
// Output name.
outputName: filePath,
// Compiled code.
source: childCompilation.assets[filePath].source()
});
const compilationHash = childCompilation.fullHash;
if (this._cachedResources.has(compilationHash)) {
resolve({
outputName: filePath,
source: this._cachedResources.get(compilationHash),
});
} else {
const source = childCompilation.assets[filePath].source();
this._cachedResources.set(compilationHash, source);
resolve({ outputName: filePath, source });
}
}
});
});
Expand Down

0 comments on commit 64b4b03

Please sign in to comment.