Skip to content

Commit

Permalink
fix(vite-node): properly normalize file url import (#7087)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Dec 23, 2024
1 parent f9a6284 commit 31675e3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
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]'

0 comments on commit 31675e3

Please sign in to comment.