Skip to content

Commit

Permalink
fix(@ngtools/webpack): re-emit component stylesheet assets
Browse files Browse the repository at this point in the history
With this change we re-emit assets referenced in component stylesheets which where uneffected by the change that re-triggered a re-compilation.

Since we cache the the result of processed component CSS, during a re-compilation `postcss-cli-resources` plugin will not run which causes assets to be to emit. With this change we now cache the asset and re-emit them on every change.

Closes angular#20882
  • Loading branch information
alan-agius4 authored and clydin committed May 22, 2021
1 parent b45715c commit 97959b0
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/resource_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { createHash } from 'crypto';
import * as path from 'path';
import * as vm from 'vm';
import { Compilation, EntryPlugin, NormalModule, library, node, sources } from 'webpack';
import { Asset, Compilation, EntryPlugin, NormalModule, library, node, sources } from 'webpack';
import { normalizePath } from './ivy/paths';

interface CompilationOutput {
Expand All @@ -25,13 +25,16 @@ export class WebpackResourceLoader {

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

private modifiedResources = new Set<string>();
private outputPathCounter = 1;

constructor(shouldCache: boolean) {
if (shouldCache) {
this.fileCache = new Map();
this.inlineCache = new Map();
this.assetCache = new Map();
}
}

Expand All @@ -40,15 +43,34 @@ export class WebpackResourceLoader {

// Update resource cache and modified resources
this.modifiedResources.clear();

if (changedFiles) {
for (const changedFile of changedFiles) {
const changedFileNormalized = normalizePath(changedFile);
this.assetCache?.delete(changedFileNormalized);

for (const affectedResource of this.getAffectedResources(changedFile)) {
this.fileCache?.delete(normalizePath(affectedResource));
const affectedResourceNormalized = normalizePath(affectedResource);
this.fileCache?.delete(affectedResourceNormalized);
this.modifiedResources.add(affectedResource);

for (const effectedDependencies of this.getResourceDependencies(
affectedResourceNormalized,
)) {
this.assetCache?.delete(normalizePath(effectedDependencies));
}
}
}
} else {
this.fileCache?.clear();
this.assetCache?.clear();
}

// Re-emit all assets for un-effected files
if (this.assetCache) {
for (const [, { name, source, info }] of this.assetCache) {
this._parentCompilation.emitAsset(name, source, info);
}
}
}

Expand Down Expand Up @@ -200,6 +222,13 @@ 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 });
}
}

// Save the dependencies for this resource.
Expand Down

0 comments on commit 97959b0

Please sign in to comment.