Skip to content

Commit 14ebbc6

Browse files
bluwyantfu
authored andcommitted
fix(hmr): handle virtual module update (#10324)
1 parent e2cdb28 commit 14ebbc6

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

packages/vite/src/node/plugins/importAnalysis.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import {
2121
debugHmr,
2222
handlePrunedModules,
2323
lexAcceptedHmrDeps,
24-
lexAcceptedHmrExports
24+
lexAcceptedHmrExports,
25+
normalizeHmrUrl
2526
} from '../server/hmr'
2627
import {
2728
cleanUrl,
@@ -629,7 +630,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
629630
str().prepend(
630631
`import { createHotContext as __vite__createHotContext } from "${clientPublicPath}";` +
631632
`import.meta.hot = __vite__createHotContext(${JSON.stringify(
632-
importerModule.url
633+
normalizeHmrUrl(importerModule.url)
633634
)});`
634635
)
635636
}

packages/vite/src/node/server/hmr.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import colors from 'picocolors'
55
import type { Update } from 'types/hmrPayload'
66
import type { RollupError } from 'rollup'
77
import { CLIENT_DIR } from '../constants'
8-
import { createDebugger, normalizePath, unique } from '../utils'
8+
import { createDebugger, normalizePath, unique, wrapId } from '../utils'
99
import type { ViteDevServer } from '..'
1010
import { isCSSRequest } from '../plugins/css'
1111
import { getAffectedGlobModules } from '../plugins/importMetaGlob'
@@ -154,12 +154,12 @@ export function updateModules(
154154
...[...boundaries].map(({ boundary, acceptedVia }) => ({
155155
type: `${boundary.type}-update` as const,
156156
timestamp,
157-
path: boundary.url,
157+
path: normalizeHmrUrl(boundary.url),
158158
explicitImportRequired:
159159
boundary.type === 'js'
160160
? isExplicitImportRequired(acceptedVia.url)
161161
: undefined,
162-
acceptedPath: acceptedVia.url
162+
acceptedPath: normalizeHmrUrl(acceptedVia.url)
163163
}))
164164
)
165165
}
@@ -484,6 +484,13 @@ export function lexAcceptedHmrExports(
484484
return urls.size > 0
485485
}
486486

487+
export function normalizeHmrUrl(url: string): string {
488+
if (!url.startsWith('.') && !url.startsWith('/')) {
489+
url = wrapId(url)
490+
}
491+
return url
492+
}
493+
487494
function error(pos: number) {
488495
const err = new Error(
489496
`import.meta.hot.accept() can only accept string literals or an ` +

0 commit comments

Comments
 (0)