Skip to content

Commit

Permalink
fix(@ngtools/webpack): remove redundant inline style cache
Browse files Browse the repository at this point in the history
The size of the cache could grow quite large over long active development periods. Webpack 5 memory caching will also allow for caching of the data.
  • Loading branch information
clydin authored and filipesilva committed May 31, 2021
1 parent ee5a7d7 commit f9657bc
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions packages/ngtools/webpack/src/resource_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export class WebpackResourceLoader {
private _reverseDependencies = new Map<string, Set<string>>();

private fileCache?: Map<string, CompilationOutput>;
private inlineCache?: Map<string, CompilationOutput>;
private assetCache?: Map<string, Asset>;

private modifiedResources = new Set<string>();
Expand All @@ -39,7 +38,6 @@ export class WebpackResourceLoader {
constructor(shouldCache: boolean) {
if (shouldCache) {
this.fileCache = new Map();
this.inlineCache = new Map();
this.assetCache = new Map();
}
}
Expand Down Expand Up @@ -106,7 +104,6 @@ export class WebpackResourceLoader {
mimeType?: string,
fileExtension?: string,
resourceType?: 'style' | 'template',
hash?: string,
containingFile?: string,
): Promise<CompilationOutput> {
if (!this._parentCompilation) {
Expand All @@ -118,7 +115,8 @@ export class WebpackResourceLoader {
filePath ||
(resourceType
? `${containingFile}-${this.outputPathCounter}.${fileExtension}!=!${this.inlineDataLoaderPath}!${containingFile}`
: `angular-resource:${resourceType},${hash}`);
: // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
`angular-resource:${resourceType},${createHash('md5').update(data!).digest('hex')}`);

if (!entry) {
throw new Error(`"filePath" or "data" must be specified.`);
Expand Down Expand Up @@ -337,24 +335,14 @@ export class WebpackResourceLoader {
return '';
}

const cacheKey = createHash('md5').update(data).digest('hex');
let compilationResult = this.inlineCache?.get(cacheKey);

if (compilationResult === undefined) {
compilationResult = await this._compile(
undefined,
data,
mimeType,
fileExtension,
resourceType,
cacheKey,
containingFile,
);

if (this.inlineCache && compilationResult.success) {
this.inlineCache.set(cacheKey, compilationResult);
}
}
const compilationResult = await this._compile(
undefined,
data,
mimeType,
fileExtension,
resourceType,
containingFile,
);

return compilationResult.content;
}
Expand Down

0 comments on commit f9657bc

Please sign in to comment.