Skip to content

Commit 926ca95

Browse files
authored
fix(vite-node): differentiate file url with hash and query (#7365)
1 parent 92da490 commit 926ca95

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

packages/vite-node/src/utils.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ export function normalizeRequestId(id: string, base?: string): string {
3636
}
3737

3838
if (id.startsWith('file://')) {
39-
return fileURLToPath(id)
39+
// preserve hash/query
40+
const { file, postfix } = splitFileAndPostfix(id)
41+
return fileURLToPath(file) + postfix
4042
}
4143

4244
return id
@@ -58,6 +60,14 @@ export function cleanUrl(url: string): string {
5860
return url.replace(postfixRE, '')
5961
}
6062

63+
function splitFileAndPostfix(path: string): {
64+
file: string
65+
postfix: string
66+
} {
67+
const file = cleanUrl(path)
68+
return { file, postfix: path.slice(file.length) }
69+
}
70+
6171
const internalRequests = ['@vite/client', '@vite/env']
6272

6373
const internalRequestRegexp = new RegExp(

test/core/test/resolve-file-url.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,12 @@ test('resolve file url', async () => {
44
const fileUrl = new URL('./resolve-file-url%7Edep.js', import.meta.url).href
55
const mod = await import(fileUrl)
66
expect(mod.default).toMatchInlineSnapshot(`"[ok]"`)
7+
8+
const mod2 = await import(`${fileUrl}#hash=test`)
9+
expect(mod2).toEqual(mod)
10+
expect(mod2).not.toBe(mod)
11+
12+
const mod3 = await import(`${fileUrl}?query=test`)
13+
expect(mod3).toEqual(mod)
14+
expect(mod3).not.toBe(mod)
715
})

0 commit comments

Comments
 (0)