Skip to content

Commit 3602bbb

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular/build): avoid overwriting inline style bundling additional results
A component may contain multiple inline styles that will originate from the same containing file. The content of the processed style is sent directly to the Angular compiler. However, any additional result files are collected and emitted in the application output. In most cases, this worked as expected as inline styles rarely had resource references that would overwrite each other. However, the potential is present for later inline styles for a component to overwrite these output files. To avoid this potential problem, the internal identifier now accounts for both the class name and order of the inline styles. This ensures that each inline style retains a unique additional results entry. (cherry picked from commit cdad256)
1 parent ce68b32 commit 3602bbb

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

packages/angular/build/src/tools/esbuild/angular/compiler-plugin.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ export function createCompilerPlugin(
153153
let modifiedFiles;
154154
if (
155155
pluginOptions.sourceFileCache?.modifiedFiles.size &&
156-
referencedFileTracker &&
157156
!pluginOptions.noopTypeScriptCompilation
158157
) {
159158
// TODO: Differentiate between changed input files and stale output files
@@ -164,6 +163,8 @@ export function createCompilerPlugin(
164163
if (!pluginOptions.externalRuntimeStyles) {
165164
stylesheetBundler.invalidate(modifiedFiles);
166165
}
166+
// Remove any stale additional results based on modified files
167+
modifiedFiles.forEach((file) => additionalResults.delete(file));
167168
}
168169

169170
if (
@@ -181,6 +182,7 @@ export function createCompilerPlugin(
181182
sourceFileCache: pluginOptions.sourceFileCache,
182183
async transformStylesheet(data, containingFile, stylesheetFile, order, className) {
183184
let stylesheetResult;
185+
let resultSource = stylesheetFile ?? containingFile;
184186

185187
// Stylesheet file only exists for external stylesheets
186188
if (stylesheetFile) {
@@ -203,6 +205,11 @@ export function createCompilerPlugin(
203205
.digest('hex')
204206
: undefined,
205207
);
208+
// Adjust result source for inline styles.
209+
// There may be multiple inline styles with the same containing file and to ensure that the results
210+
// do not overwrite each other the result source identifier needs to be unique for each. The class
211+
// name and order fields can be used for this. The structure is arbitrary as long as it is unique.
212+
resultSource += `?class=${className}&order=${order}`;
206213
}
207214

208215
(result.warnings ??= []).push(...stylesheetResult.warnings);
@@ -213,7 +220,7 @@ export function createCompilerPlugin(
213220
}
214221

215222
const { contents, outputFiles, metafile, referencedFiles } = stylesheetResult;
216-
additionalResults.set(stylesheetFile ?? containingFile, {
223+
additionalResults.set(resultSource, {
217224
outputFiles,
218225
metafile,
219226
});

0 commit comments

Comments
 (0)