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(hmr): handle virtual module update #10324

Merged
merged 1 commit into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
debugHmr,
handlePrunedModules,
lexAcceptedHmrDeps,
lexAcceptedHmrExports
lexAcceptedHmrExports,
normalizeHmrUrl
} from '../server/hmr'
import {
cleanUrl,
Expand Down Expand Up @@ -629,7 +630,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
str().prepend(
`import { createHotContext as __vite__createHotContext } from "${clientPublicPath}";` +
`import.meta.hot = __vite__createHotContext(${JSON.stringify(
importerModule.url
normalizeHmrUrl(importerModule.url)
)});`
)
}
Expand Down
13 changes: 10 additions & 3 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import colors from 'picocolors'
import type { Update } from 'types/hmrPayload'
import type { RollupError } from 'rollup'
import { CLIENT_DIR } from '../constants'
import { createDebugger, normalizePath, unique } from '../utils'
import { createDebugger, normalizePath, unique, wrapId } from '../utils'
import type { ViteDevServer } from '..'
import { isCSSRequest } from '../plugins/css'
import { getAffectedGlobModules } from '../plugins/importMetaGlob'
Expand Down Expand Up @@ -154,12 +154,12 @@ export function updateModules(
...[...boundaries].map(({ boundary, acceptedVia }) => ({
type: `${boundary.type}-update` as const,
timestamp,
path: boundary.url,
path: normalizeHmrUrl(boundary.url),
explicitImportRequired:
boundary.type === 'js'
? isExplicitImportRequired(acceptedVia.url)
: undefined,
acceptedPath: acceptedVia.url
acceptedPath: normalizeHmrUrl(acceptedVia.url)
}))
)
}
Expand Down Expand Up @@ -484,6 +484,13 @@ export function lexAcceptedHmrExports(
return urls.size > 0
}

export function normalizeHmrUrl(url: string): string {
if (!url.startsWith('.') && !url.startsWith('/')) {
url = wrapId(url)
}
return url
}

function error(pos: number) {
const err = new Error(
`import.meta.hot.accept() can only accept string literals or an ` +
Expand Down