Skip to content

Commit

Permalink
fix(@ngtools/webpack): allow generated assets of Angular component re…
Browse files Browse the repository at this point in the history
…sources

The asset caching for Angular component resources previously required that all assets had an originating file. However, some Webpack plugins may generate assets that do not originate from on-disk files and are instead synthetic. These type of assets are now supported by generating a cache key based on the output name of the asset. These assets will persist within the cache due to the lack of knowledge on the dependencies of these assets which results in the inability to invalidate the assets. Updated assets of the same output name will, however, replace older versions of the asset on rebuilds.

Fixes: #21290
(cherry picked from commit 7536338)
  • Loading branch information
clydin authored and filipesilva committed Jul 9, 2021
1 parent 191dc49 commit e1074eb
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions packages/ngtools/webpack/src/resource_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,15 @@ export class WebpackResourceLoader {

parent.warnings.push(...childCompilation.warnings);
parent.errors.push(...childCompilation.errors);
for (const { info, name, source } of childCompilation.getAssets()) {
if (info.sourceFilename === undefined) {
throw new Error(`'${name}' asset info 'sourceFilename' is 'undefined'.`);
}

this.assetCache?.set(info.sourceFilename, { info, name, source });
if (this.assetCache) {
for (const { info, name, source } of childCompilation.getAssets()) {
// Use the originating file as the cache key if present
// Otherwise, generate a cache key based on the generated name
const cacheKey = info.sourceFilename ?? `!![GENERATED]:${name}`;

this.assetCache.set(cacheKey, { info, name, source });
}
}
}

Expand Down

0 comments on commit e1074eb

Please sign in to comment.