Skip to content

Commit d2a0d48

Browse files
committed
Use a file URL when we can
1 parent ea7380a commit d2a0d48

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

packages/vite/src/node/ssr/ssrModuleLoader.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs'
22
import path from 'path'
33
import { pathToFileURL } from 'url'
44
import { ViteDevServer } from '..'
5-
import { dynamicImport, cleanUrl, isBuiltin, resolveFrom, unwrapId } from '../utils'
5+
import { dynamicImport, cleanUrl, isBuiltin, resolveFrom, unwrapId, usingDynamicImport } from '../utils'
66
import { rebindErrorStacktrace, ssrRewriteStacktrace } from './ssrStacktrace'
77
import {
88
ssrExportAllKey,
@@ -185,6 +185,9 @@ async function nodeImport(
185185
url = id
186186
} else {
187187
url = resolve(id, importer, config.root, !!config.resolve.preserveSymlinks)
188+
if (usingDynamicImport) {
189+
url = pathToFileURL(url).toString()
190+
}
188191
}
189192
const mod = await dynamicImport(url)
190193
return proxyESM(id, mod)

packages/vite/src/node/utils.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ export function toUpperCaseDriveLetter(pathName: string): string {
569569
export const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//gm
570570
export const singlelineCommentsRE = /\/\/.*/g
571571

572+
export const usingDynamicImport = typeof jest === 'undefined';
572573
/**
573574
* Dynamically import files. It will make sure it's not being compiled away by TS/Rollup.
574575
*
@@ -578,4 +579,4 @@ export const singlelineCommentsRE = /\/\/.*/g
578579
*
579580
* @param file File path to import.
580581
*/
581-
export const dynamicImport = typeof jest === 'undefined' ? new Function('file', 'return import(file)') : require;
582+
export const dynamicImport = usingDynamicImport ? new Function('file', 'return import(file)') : require;

0 commit comments

Comments
 (0)