Skip to content

Commit

Permalink
fix: use consistent virtual module ID in module graph
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed May 2, 2023
1 parent 229c592 commit 7cc1d96
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
8 changes: 5 additions & 3 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
isExternalUrl,
isInNodeModules,
isJSRequest,
isVirtualModule,
joinUrlSegments,
moduleListContains,
normalizePath,
Expand Down Expand Up @@ -281,8 +282,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
const importedBindings = enablePartialAccept
? new Map<string, Set<string>>()
: null
const toAbsoluteUrl = (url: string) =>
path.posix.resolve(path.posix.dirname(importerModule.url), url)
const toAbsoluteUrl = (url: string) => {
return path.posix.resolve(path.posix.dirname(importerModule.url), url)
}

const normalizeUrl = async (
url: string,
Expand Down Expand Up @@ -742,7 +744,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
const normalizedAcceptedUrls = new Set<string>()
for (const { url, start, end } of acceptedUrls) {
const [normalized] = await moduleGraph.resolveUrl(
toAbsoluteUrl(url),
isVirtualModule(url) ? url : toAbsoluteUrl(url),
ssr,
)
normalizedAcceptedUrls.add(normalized)
Expand Down
6 changes: 1 addition & 5 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type { ModuleNode } from '../server/moduleGraph'
import type { ResolvedConfig } from '../config'
import {
evalValue,
isVirtualModule,
normalizePath,
slash,
transformStableResult,
Expand Down Expand Up @@ -629,8 +630,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('/')
}
5 changes: 5 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1214,3 +1214,8 @@ const escapeRegexRE = /[-/\\^$*+?.()|[\]{}]/g
export function escapeRegex(str: string): string {
return str.replace(escapeRegexRE, '\\$&')
}

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 @@ -672,6 +673,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 7cc1d96

Please sign in to comment.