Skip to content

Commit

Permalink
fix: revert #13073, use consistent virtual module ID in module graph (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Jul 6, 2023
1 parent b0bfa01 commit f589ac0
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 19 deletions.
3 changes: 1 addition & 2 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,9 +751,8 @@ 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(
isRelative ? toAbsoluteUrl(url) : url,
toAbsoluteUrl(url),
ssr,
)
normalizedAcceptedUrls.add(normalized)
Expand Down
7 changes: 6 additions & 1 deletion 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 = !isAbsolute(id)
const isVirtual = isVirtualModule(id)
const dir = isVirtual ? undefined : dirname(id)
const matches = await parseImportGlob(
code,
Expand Down Expand Up @@ -645,3 +645,8 @@ 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: 0 additions & 9 deletions playground/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
removeFile,
untilBrowserLogAfter,
untilUpdated,
viteServer,
viteTestUrl,
} from '~utils'

Expand Down Expand Up @@ -677,14 +676,6 @@ 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: 0 additions & 7 deletions playground/hmr/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,6 @@ 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 f589ac0

Please sign in to comment.