diff --git a/packages/vite-node/src/utils.ts b/packages/vite-node/src/utils.ts index 1388c5fda9a5..cc2b2bf7e265 100644 --- a/packages/vite-node/src/utils.ts +++ b/packages/vite-node/src/utils.ts @@ -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 @@ -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( diff --git a/test/core/test/resolve-file-url.test.ts b/test/core/test/resolve-file-url.test.ts index 8d7c84280933..04651f171162 100644 --- a/test/core/test/resolve-file-url.test.ts +++ b/test/core/test/resolve-file-url.test.ts @@ -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) })