Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vite-node): differentiate file url with hash and query #7365

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})