Skip to content

Commit

Permalink
fix: use consistent virtual module ID in module graph (#13073)
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jun 6, 2023
1 parent e355c9c commit aa1776f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,8 +741,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// normalize and rewrite accepted urls
const normalizedAcceptedUrls = new Set<string>()
for (const { url, start, end } of acceptedUrls) {
const isRelative = url[0] === '.'
const [normalized] = await moduleGraph.resolveUrl(
toAbsoluteUrl(url),
isRelative ? toAbsoluteUrl(url) : url,
ssr,
)
normalizedAcceptedUrls.add(normalized)
Expand Down
7 changes: 1 addition & 6 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ export async function transformGlobImport(
): Promise<TransformGlobImportResult | null> {
id = slash(id)
root = slash(root)
const isVirtual = isVirtualModule(id)
const isVirtual = !isAbsolute(id)
const dir = isVirtual ? undefined : dirname(id)
const matches = await parseImportGlob(
code,
Expand Down Expand Up @@ -645,8 +645,3 @@ export function getCommonBase(globsResolved: string[]): null | string {

return commonAncestor
}

export function isVirtualModule(id: string): boolean {
// https://vitejs.dev/guide/api-plugin.html#virtual-modules-convention
return id.startsWith('virtual:') || id[0] === '\0' || !id.includes('/')
}
9 changes: 9 additions & 0 deletions playground/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
removeFile,
untilBrowserLogAfter,
untilUpdated,
viteServer,
viteTestUrl,
} from '~utils'

Expand Down Expand Up @@ -676,6 +677,14 @@ if (!isBuild) {
expect(await btn.textContent()).toBe('Compteur 0')
})

test('virtual module in module graph', async () => {
const moduleGraph = viteServer.moduleGraph
const virtualId = Array.from(moduleGraph.idToModuleMap.keys()).filter(
(id: string) => id.includes('virtual'),
)
expect(virtualId).toEqual(['\x00virtual:file', '/@id/__x00__virtual:file'])
})

test('handle virtual module updates', async () => {
await page.goto(viteTestUrl)
const el = await page.$('.virtual')
Expand Down
7 changes: 7 additions & 0 deletions playground/hmr/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ if (import.meta.hot) {
handleDep('multi deps', foo, nestedFoo)
})

import.meta.hot.accept(
['virtual:file', '/@id/__x00__virtual:file'],
([rawVirtualPath, acceptedVirtualPath]) => {
text('.virtual', acceptedVirtualPath.virtual)
},
)

import.meta.hot.dispose(() => {
console.log(`foo was:`, foo)
})
Expand Down

0 comments on commit aa1776f

Please sign in to comment.