Skip to content

Commit 1c9cb49

Browse files
authored
fix(module-runner): normalize file:// on windows (#20449)
1 parent 82b0228 commit 1c9cb49

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

packages/vite/src/module-runner/evaluatedModules.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,9 @@ const prefixedBuiltins = new Set([
135135
// transform file url to id
136136
// virtual:custom -> virtual:custom
137137
// \0custom -> \0custom
138-
// /root/id -> /id
139-
// /root/id.js -> /id.js
140-
// C:/root/id.js -> /id.js
141-
// C:\root\id.js -> /id.js
138+
// node:fs -> fs
139+
// /@fs/C:/root/id.js => C:/root/id.js
140+
// file:///C:/root/id.js -> C:/root/id.js
142141
export function normalizeModuleId(file: string): string {
143142
if (prefixedBuiltins.has(file)) return file
144143

@@ -149,5 +148,5 @@ export function normalizeModuleId(file: string): string {
149148
.replace(/^\/+/, '/')
150149

151150
// if it's not in the root, keep it as a path, not a URL
152-
return unixFile.replace(/^file:\//, '/')
151+
return unixFile.replace(/^file:\/+/, isWindows ? '' : '/')
153152
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'tinyglobby'

packages/vite/src/node/ssr/runtime/__tests__/server-runtime.spec.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@ import { posix, win32 } from 'node:path'
33
import { fileURLToPath } from 'node:url'
44
import { describe, expect, vi } from 'vitest'
55
import { isWindows } from '../../../../shared/utils'
6+
import type { ExternalFetchResult } from '../../../../shared/invokeMethods'
67
import { createModuleRunnerTester } from './utils'
78

89
const _URL = URL
910

1011
describe('module runner initialization', async () => {
11-
const it = await createModuleRunnerTester()
12+
const it = await createModuleRunnerTester({
13+
resolve: {
14+
external: ['tinyglobby'],
15+
},
16+
})
1217

1318
it('correctly runs ssr code', async ({ runner }) => {
1419
const mod = await runner.import('/fixtures/simple.js')
@@ -489,4 +494,24 @@ describe('virtual module hmr', async () => {
489494
expect(mod?.exports.default).toBe('reloaded')
490495
})
491496
})
497+
498+
it("the external module's ID and file are resolved correctly", async ({
499+
server,
500+
runner,
501+
}) => {
502+
await runner.import(
503+
posix.join(server.config.root, 'fixtures/import-external.ts'),
504+
)
505+
const moduleNode = runner.evaluatedModules.getModuleByUrl('tinyglobby')!
506+
const meta = moduleNode.meta as ExternalFetchResult
507+
if (process.platform === 'win32') {
508+
expect(meta.externalize).toMatch(/^file:\/\/\/\w:\//) // file:///C:/
509+
expect(moduleNode.id).toMatch(/^\w:\//) // C:/
510+
expect(moduleNode.file).toMatch(/^\w:\//) // C:/
511+
} else {
512+
expect(meta.externalize).toMatch(/^file:\/\/\//) // file:///
513+
expect(moduleNode.id).toMatch(/^\//) // /
514+
expect(moduleNode.file).toMatch(/^\//) // /
515+
}
516+
})
492517
})

0 commit comments

Comments
 (0)