Skip to content

Commit 2011d34

Browse files
clydinalan-agius4
authored andcommittedJan 27, 2025·
fix(@angular/build): support template updates that also trigger global stylesheet changes
In some cases a change to a component template may cause changes to other aspects of the application. Tailwind, for instance, may cause changes to the global stylesheets when class usage is changed in a template. To support hot module replacement of the component template in these cases, stylesheet changes are now analyzed and separated during the update process. This allows both a hot update of the template and the global stylesheet during the rebuild instead of the previously required full page reload. (cherry picked from commit 531dcdc)
1 parent 6880199 commit 2011d34

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed
 

‎packages/angular/build/src/builders/application/build-action.ts

+27-2
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ function* emitOutputResults(
300300
},
301301
};
302302

303+
let hasCssUpdates = false;
304+
303305
// Initially assume all previous output files have been removed
304306
const removedOutputFiles = new Map(previousOutputInfo);
305307
for (const file of outputFiles) {
@@ -316,8 +318,10 @@ function* emitOutputResults(
316318
}
317319

318320
if (needFile) {
319-
// Updates to non-JS files must signal an update with the dev server
320-
if (!/(?:\.m?js|\.map)$/.test(file.path)) {
321+
if (file.path.endsWith('.css')) {
322+
hasCssUpdates = true;
323+
} else if (!/(?:\.m?js|\.map)$/.test(file.path)) {
324+
// Updates to non-JS files must signal an update with the dev server
321325
incrementalResult.background = false;
322326
}
323327

@@ -345,6 +349,8 @@ function* emitOutputResults(
345349
continue;
346350
}
347351

352+
hasCssUpdates ||= destination.endsWith('.css');
353+
348354
incrementalResult.files[destination] = {
349355
type: BuildOutputFileType.Browser,
350356
inputPath: source,
@@ -369,6 +375,21 @@ function* emitOutputResults(
369375
// If there are template updates and the incremental update was background only, a component
370376
// update is possible.
371377
if (hasTemplateUpdates && incrementalResult.background) {
378+
// Template changes may be accompanied by stylesheet changes and these should also be updated hot when possible.
379+
if (hasCssUpdates) {
380+
const styleResult: IncrementalResult = {
381+
kind: ResultKind.Incremental,
382+
added: incrementalResult.added.filter(isCssFilePath),
383+
removed: incrementalResult.removed.filter(({ path }) => isCssFilePath(path)),
384+
modified: incrementalResult.modified.filter(isCssFilePath),
385+
files: Object.fromEntries(
386+
Object.entries(incrementalResult.files).filter(([path]) => isCssFilePath(path)),
387+
),
388+
};
389+
390+
yield styleResult;
391+
}
392+
372393
const updateResult: ComponentUpdateResult = {
373394
kind: ResultKind.ComponentUpdate,
374395
updates: Array.from(templateUpdates, ([id, content]) => ({
@@ -381,3 +402,7 @@ function* emitOutputResults(
381402
yield updateResult;
382403
}
383404
}
405+
406+
function isCssFilePath(filePath: string): boolean {
407+
return /\.css(?:\.map)?$/i.test(filePath);
408+
}

‎packages/angular/build/src/builders/dev-server/vite-server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ function handleUpdate(
589589
type: 'update',
590590
updates,
591591
});
592-
logger.info('HMR update sent to client(s).');
592+
logger.info('Stylesheet update sent to client(s).');
593593

594594
return;
595595
}

0 commit comments

Comments
 (0)
Please sign in to comment.