Skip to content

Commit 6162968

Browse files
fix(hmr): handle import.meta.hot.invalidate in virtual module (#23171)
Co-authored-by: c4605 <[email protected]>
1 parent 8cb872e commit 6162968

6 files changed

Lines changed: 54 additions & 2 deletions

File tree

‎packages/vite/src/node/server/environment.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
createExplicitDepsOptimizer,
1818
} from '../optimizer/optimizer'
1919
import { ERR_OUTDATED_OPTIMIZED_DEP } from '../../shared/constants'
20-
import { cleanUrl, promiseWithResolvers } from '../../shared/utils'
20+
import { cleanUrl, promiseWithResolvers, unwrapId } from '../../shared/utils'
2121
import type { ViteDevServer } from '../server'
2222
import { EnvironmentModuleGraph } from './moduleGraph'
2323
import type { EnvironmentModuleNode } from './moduleGraph'
@@ -176,7 +176,7 @@ export class DevEnvironment extends BaseEnvironment {
176176
({ path, message, firstInvalidatedBy }, client) => {
177177
this.invalidateModule(
178178
{
179-
path,
179+
path: unwrapId(path),
180180
message,
181181
firstInvalidatedBy,
182182
},

‎playground/hmr/__tests__/hmr.spec.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,20 @@ if (!isBuild) {
303303
}
304304
})
305305

306+
test('invalidate virtual module propagates to importers', async () => {
307+
const el = await page.$('.virtual-invalidation-parent')
308+
await expect.poll(() => el.textContent()).toBe('initial')
309+
310+
// Edit the real dep file — Vite detects the change and sends
311+
// js-update to the virtual module. The virtual module accepts
312+
// then invalidates, which should propagate to parent.js.
313+
editFile('virtual-invalidation/dep.js', (code) =>
314+
code.replace('initial', 'updated2'),
315+
)
316+
317+
await expect.poll(() => el.textContent()).toBe('updated2')
318+
})
319+
306320
test('invalidate on root triggers page reload', async () => {
307321
editFile('invalidation/root.js', (code) => code.replace('Init', 'Updated'))
308322
await page.waitForEvent('load')

‎playground/hmr/index.html‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
<div class="virtual-dep"></div>
2929
<div class="soft-invalidation"></div>
3030
<div class="invalidation-parent"></div>
31+
<div class="virtual-invalidation-parent"></div>
32+
<script type="module" src="./virtual-invalidation/parent.js"></script>
3133
<div class="invalidation-root"></div>
3234
<div class="invalidation-circular-deps"></div>
3335
<div class="invalidation-circular-deps-handled"></div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const depValue = 'initial'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { value } from 'virtual:invalidation-file'
2+
3+
if (import.meta.hot) {
4+
import.meta.hot.accept()
5+
}
6+
7+
document.querySelector('.virtual-invalidation-parent').innerHTML = value

‎playground/hmr/vite.config.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,41 @@ export default defineConfig(({ command }) => ({
5252
},
5353
},
5454
virtualPlugin(),
55+
virtualInvalidationPlugin(),
5556
transformCountPlugin(),
5657
watchCssDepsPlugin(),
5758
TestCssLinkPlugin(),
5859
hotEventsPlugin(),
5960
],
6061
}))
6162

63+
// Virtual module that calls invalidate()
64+
function virtualInvalidationPlugin(): Plugin {
65+
return {
66+
name: 'virtual-invalidation-file',
67+
resolveId(id) {
68+
if (id === 'virtual:invalidation-file') {
69+
return '\0virtual:invalidation-file'
70+
}
71+
},
72+
load(id) {
73+
if (id === '\0virtual:invalidation-file') {
74+
// Import a real file so editing it triggers Vite's HMR pipeline.
75+
// The virtual module accepts and immediately invalidates,
76+
// which should propagate to its importers.
77+
return `\
78+
import { depValue } from '/virtual-invalidation/dep.js';
79+
export const value = depValue;
80+
if (import.meta.hot) {
81+
import.meta.hot.accept(() => {
82+
import.meta.hot.invalidate()
83+
})
84+
}`
85+
}
86+
},
87+
}
88+
}
89+
6290
function virtualPlugin(): Plugin {
6391
let num = 0
6492
return {

0 commit comments

Comments
 (0)