Skip to content

Commit

Permalink
Use a file URL when we can
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Oct 15, 2021
1 parent ea7380a commit d2a0d48
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs'
import path from 'path'
import { pathToFileURL } from 'url'
import { ViteDevServer } from '..'
import { dynamicImport, cleanUrl, isBuiltin, resolveFrom, unwrapId } from '../utils'
import { dynamicImport, cleanUrl, isBuiltin, resolveFrom, unwrapId, usingDynamicImport } from '../utils'
import { rebindErrorStacktrace, ssrRewriteStacktrace } from './ssrStacktrace'
import {
ssrExportAllKey,
Expand Down Expand Up @@ -185,6 +185,9 @@ async function nodeImport(
url = id
} else {
url = resolve(id, importer, config.root, !!config.resolve.preserveSymlinks)
if (usingDynamicImport) {
url = pathToFileURL(url).toString()
}
}
const mod = await dynamicImport(url)
return proxyESM(id, mod)
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ export function toUpperCaseDriveLetter(pathName: string): string {
export const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//gm
export const singlelineCommentsRE = /\/\/.*/g

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

0 comments on commit d2a0d48

Please sign in to comment.