Skip to content

Commit 76a3edc

Browse files
committed
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.
1 parent 8bc43bc commit 76a3edc

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

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

Lines changed: 23 additions & 2 deletions
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((path) => path.endsWith('.css')),
383+
removed: incrementalResult.removed.filter(({ path }) => path.endsWith('.css')),
384+
modified: incrementalResult.modified.filter((path) => path.endsWith('.css')),
385+
files: Object.fromEntries(
386+
Object.entries(incrementalResult.files).filter(([path]) => path.endsWith('.css')),
387+
),
388+
};
389+
390+
yield styleResult;
391+
}
392+
372393
const updateResult: ComponentUpdateResult = {
373394
kind: ResultKind.ComponentUpdate,
374395
updates: Array.from(templateUpdates, ([id, content]) => ({

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

Lines changed: 1 addition & 1 deletion
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)