Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vite-plugin-angular): correctly implement HMR of component styles #1542

Merged
merged 6 commits into from
Jan 7, 2025
106 changes: 87 additions & 19 deletions packages/vite-plugin-angular/src/lib/angular-vite-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ResolvedConfig,
Connect,
} from 'vite';
import { encapsulateStyle } from '@angular/compiler';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know what version this was introduced in? We support Angular v17-19

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it was added in 17.1.0: angular/angular@f0377d3

I can just make it lazy as we only need it if liveReload is enabled, which will only work in v19+

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea

Copy link
Contributor Author

@mattlewis92 mattlewis92 Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


import { createCompilerPlugin } from './compiler-plugin.js';
import {
Expand Down Expand Up @@ -350,15 +351,7 @@ export function angular(options?: PluginOptions): Plugin[] {

return ctx.modules.map((mod) => {
if (mod.id === ctx.file) {
// support Vite 6
if ('_clientModule' in mod) {
(mod as any)['_clientModule'].isSelfAccepting = true;
}

return {
...mod,
isSelfAccepting: true,
} as ModuleNode;
return markModuleSelfAccepting(mod);
}

return mod;
Expand All @@ -374,7 +367,42 @@ export function angular(options?: PluginOptions): Plugin[] {
const isDirect = ctx.modules.find(
(mod) => ctx.file === mod.file && mod.id?.includes('?direct')
);
if (isDirect) {
if (pluginOptions.liveReload && isDirect?.id && isDirect.file) {
const isComponentStyle =
isDirect.type === 'css' && isComponentStyleSheet(isDirect.id);
if (isComponentStyle) {
const { encapsulation } = getComponentStyleSheetMeta(isDirect.id);

// Track if the component uses ShadowDOM encapsulation
// Shadow DOM components currently require a full reload.
// Vite's CSS hot replacement does not support shadow root searching.
if (encapsulation !== 'shadow') {
ctx.server.ws.send({
type: 'update',
updates: [
{
type: 'css-update',
timestamp: Date.now(),
path: isDirect.id,
acceptedPath: isDirect.file,
},
],
});

return ctx.modules
.filter((mod) => {
// Component stylesheets will have 2 modules (*.component.scss and *.component.scss?direct&ngcomp=xyz&e=x)
// We remove the module with the query params to prevent vite double logging the stylesheet name "hmr update *.component.scss, *.component.scss?direct&ngcomp=xyz&e=x"
return mod.file !== ctx.file || mod.id !== isDirect.id;
})
.map((mod) => {
if (mod.file === ctx.file) {
return markModuleSelfAccepting(mod);
}
return mod;
}) as ModuleNode[];
}
}
return ctx.modules;
}

Expand Down Expand Up @@ -407,15 +435,7 @@ export function angular(options?: PluginOptions): Plugin[] {

return ctx.modules.map((mod) => {
if (mod.id === ctx.file) {
// support Vite 6
if ('_clientModule' in mod) {
(mod as any)['_clientModule'].isSelfAccepting = true;
}

return {
...mod,
isSelfAccepting: true,
} as ModuleNode;
return markModuleSelfAccepting(mod);
}

return mod;
Expand Down Expand Up @@ -497,6 +517,20 @@ export function angular(options?: PluginOptions): Plugin[] {
return;
}

/**
* Encapsulate component stylesheets that use emulated encapsulation
*/
if (pluginOptions.liveReload && isComponentStyleSheet(id)) {
const { encapsulation, componentId } = getComponentStyleSheetMeta(id);
if (encapsulation === 'emulated' && componentId) {
const encapsulated = encapsulateStyle(code, componentId);
return {
code: encapsulated,
map: null,
};
}
}

if (TS_EXT_REGEX.test(id)) {
if (id.includes('.ts?')) {
// Strip the query string off the ID
Expand Down Expand Up @@ -921,3 +955,37 @@ function getDiagnosticsForSourceFile(
...angularDiagnostics,
];
}

function markModuleSelfAccepting(mod: ModuleNode): ModuleNode {
// support Vite 6
if ('_clientModule' in mod) {
(mod as any)['_clientModule'].isSelfAccepting = true;
}

return {
...mod,
isSelfAccepting: true,
} as ModuleNode;
}

function isComponentStyleSheet(id: string): boolean {
return id.includes('ngcomp=');
}

function getComponentStyleSheetMeta(id: string): {
componentId: string;
encapsulation: 'emulated' | 'shadow' | 'none';
} {
const params = new URL(id, 'http://localhost').searchParams;
const encapsulationMapping = {
'0': 'emulated',
'2': 'none',
'3': 'shadow',
};
return {
componentId: params.get('ngcomp')!,
encapsulation: encapsulationMapping[
params.get('e') as keyof typeof encapsulationMapping
] as 'emulated' | 'shadow' | 'none',
};
}
Loading