Skip to content

Commit

Permalink
fix(vite-plugin-angular): invalidation fixes for HMR/live reload (#1526)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonroberts authored Dec 28, 2024
1 parent b3f8a93 commit 7b783d9
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion packages/vite-plugin-angular/src/lib/angular-vite-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,20 @@ export function angular(options?: PluginOptions): Plugin[] {
}

const [fileId] = decodeURIComponent(componentId).split('@');
const result = await fileEmitter?.(resolve(process.cwd(), fileId));
const resolvedId = resolve(process.cwd(), fileId);
const invalidated =
!!server.moduleGraph.getModuleById(resolvedId)
?.lastInvalidationTimestamp;

// don't send an HMR update until the file has been invalidated
if (!invalidated) {
res.setHeader('Content-Type', 'text/javascript');
res.setHeader('Cache-Control', 'no-cache');
res.end('');
return;
}

const result = await fileEmitter?.(resolvedId);
res.setHeader('Content-Type', 'text/javascript');
res.setHeader('Cache-Control', 'no-cache');
res.end(`${result?.hmrUpdateCode || ''}`);
Expand Down Expand Up @@ -336,6 +348,11 @@ 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,
Expand Down Expand Up @@ -388,6 +405,11 @@ 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,
Expand Down

0 comments on commit 7b783d9

Please sign in to comment.