Skip to content

Commit c17aee3

Browse files
committed
fix(@angular-devkit/build-angular): ensure latest inline stylesheet data is used during rebuilds
Fixes: angular#20904
1 parent 97959b0 commit c17aee3

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

src/ivy/host.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ export function augmentHostWithResources(
5858
}
5959

6060
if (options.inlineStyleMimeType) {
61-
const content = await resourceLoader.process(data, options.inlineStyleMimeType);
61+
const content = await resourceLoader.process(
62+
data,
63+
options.inlineStyleMimeType,
64+
context.type,
65+
context.containingFile,
66+
);
6267

6368
return { content };
6469
}

src/resource_loader.ts

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,16 @@ export class WebpackResourceLoader {
9898
filePath?: string,
9999
data?: string,
100100
mimeType?: string,
101+
resourceType?: 'style' | 'template',
102+
hash?: string,
103+
containingFile?: string,
101104
): Promise<CompilationOutput> {
102105
if (!this._parentCompilation) {
103106
throw new Error('WebpackResourceLoader cannot be used without parentCompilation');
104107
}
105108

106109
// Create a special URL for reading the resource from memory
107-
const entry = data ? 'angular-resource://' : filePath;
110+
const entry = data ? `angular-resource:${resourceType},${hash}` : filePath;
108111
if (!entry) {
109112
throw new Error(`"filePath" or "data" must be specified.`);
110113
}
@@ -116,7 +119,11 @@ export class WebpackResourceLoader {
116119
);
117120
}
118121

119-
const outputFilePath = filePath || `angular-resource-output-${this.outputPathCounter++}.css`;
122+
const outputFilePath =
123+
filePath ||
124+
`${containingFile}-angular-inline--${this.outputPathCounter++}.${
125+
resourceType === 'template' ? 'html' : 'css'
126+
}`;
120127
const outputOptions = {
121128
filename: outputFilePath,
122129
library: {
@@ -215,7 +222,14 @@ export class WebpackResourceLoader {
215222
if (parent) {
216223
parent.children = parent.children.filter((child) => child !== childCompilation);
217224

218-
parent.fileDependencies.addAll(childCompilation.fileDependencies);
225+
for (const fileDependency of childCompilation.fileDependencies) {
226+
if (data && containingFile && fileDependency.endsWith(entry)) {
227+
// use containing file if the resource was inline
228+
parent.fileDependencies.add(containingFile);
229+
} else {
230+
parent.fileDependencies.add(fileDependency);
231+
}
232+
}
219233
parent.contextDependencies.addAll(childCompilation.contextDependencies);
220234
parent.missingDependencies.addAll(childCompilation.missingDependencies);
221235
parent.buildDependencies.addAll(childCompilation.buildDependencies);
@@ -298,7 +312,12 @@ export class WebpackResourceLoader {
298312
return compilationResult.content;
299313
}
300314

301-
async process(data: string, mimeType: string): Promise<string> {
315+
async process(
316+
data: string,
317+
mimeType: string,
318+
resourceType: 'template' | 'style',
319+
containingFile?: string,
320+
): Promise<string> {
302321
if (data.trim().length === 0) {
303322
return '';
304323
}
@@ -307,7 +326,14 @@ export class WebpackResourceLoader {
307326
let compilationResult = this.inlineCache?.get(cacheKey);
308327

309328
if (compilationResult === undefined) {
310-
compilationResult = await this._compile(undefined, data, mimeType);
329+
compilationResult = await this._compile(
330+
undefined,
331+
data,
332+
mimeType,
333+
resourceType,
334+
cacheKey,
335+
containingFile,
336+
);
311337

312338
if (this.inlineCache && compilationResult.success) {
313339
this.inlineCache.set(cacheKey, compilationResult);

0 commit comments

Comments
 (0)