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): properly normalize file url import #7087

Merged
merged 3 commits into from
Dec 23, 2024
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
9 changes: 7 additions & 2 deletions packages/vite-node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ export function normalizeRequestId(id: string, base?: string): string {
id = id.replace(driveOppositeRegext, `${drive}$1`)
}

if (id.startsWith('file://')) {
return fileURLToPath(id)
}

return id
.replace(/^\/@id\/__x00__/, '\0') // virtual modules start with `\0`
.replace(/^\/@id\//, '')
.replace(/^__vite-browser-external:/, '')
.replace(/^file:(\/+)/, isWindows ? '' : '/') // remove file protocol and duplicate leading slashes
.replace(/\?v=\w+/, '?') // remove ?v= query
.replace(/&v=\w+/, '') // remove &v= query
.replace(/\?t=\w+/, '?') // remove ?t= query
Expand Down Expand Up @@ -89,10 +92,12 @@ export function normalizeModuleId(id: string) {
if (prefixedBuiltins.has(id)) {
return id
}
if (id.startsWith('file://')) {
return fileURLToPath(id)
}
return id
.replace(/\\/g, '/')
.replace(/^\/@fs\//, isWindows ? '' : '/')
.replace(/^file:\//, '/')
.replace(/^node:/, '')
.replace(/^\/+/, '/')
}
Expand Down
7 changes: 7 additions & 0 deletions test/core/test/resolve-file-url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from 'vitest'

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]"`)
})
1 change: 1 addition & 0 deletions test/core/test/resolve-file-url~dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default '[ok]'
Loading