Skip to content

Commit

Permalink
fix(vite-node): differentiate file url with hash and query (#7365)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Feb 3, 2025
1 parent 92da490 commit 926ca95
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/vite-node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export function normalizeRequestId(id: string, base?: string): string {
}

if (id.startsWith('file://')) {
return fileURLToPath(id)
// preserve hash/query
const { file, postfix } = splitFileAndPostfix(id)
return fileURLToPath(file) + postfix
}

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

function splitFileAndPostfix(path: string): {
file: string
postfix: string
} {
const file = cleanUrl(path)
return { file, postfix: path.slice(file.length) }
}

const internalRequests = ['@vite/client', '@vite/env']

const internalRequestRegexp = new RegExp(
Expand Down
8 changes: 8 additions & 0 deletions test/core/test/resolve-file-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@ test('resolve file url', async () => {
const fileUrl = new URL('./resolve-file-url%7Edep.js', import.meta.url).href
const mod = await import(fileUrl)
expect(mod.default).toMatchInlineSnapshot(`"[ok]"`)

const mod2 = await import(`${fileUrl}#hash=test`)
expect(mod2).toEqual(mod)
expect(mod2).not.toBe(mod)

const mod3 = await import(`${fileUrl}?query=test`)
expect(mod3).toEqual(mod)
expect(mod3).not.toBe(mod)
})

0 comments on commit 926ca95

Please sign in to comment.