Skip to content

Commit

Permalink
fix(hmr): handle virtual module update (#10324)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored and antfu committed Oct 6, 2022
1 parent e2cdb28 commit 14ebbc6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
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

0 comments on commit 14ebbc6

Please sign in to comment.