diff --git a/packages/vite/src/dev-bundler.ts b/packages/vite/src/dev-bundler.ts index 2e357c0a68..c13e5f0697 100644 --- a/packages/vite/src/dev-bundler.ts +++ b/packages/vite/src/dev-bundler.ts @@ -1,5 +1,6 @@ import { pathToFileURL } from 'url' import { existsSync } from 'fs' +import { builtinModules } from 'module' import { resolve } from 'pathe' import * as vite from 'vite' import { ExternalsOptions, isExternal as _isExternal, ExternalsDefaults } from 'externality' @@ -73,8 +74,11 @@ async function transformRequest (opts: TransformOptions, id: string) { // Remove for externals const withoutVersionQuery = id.replace(/\?v=\w+$/, '') if (await isExternal(opts, withoutVersionQuery)) { + const path = builtinModules.includes(withoutVersionQuery.split('node:').pop()) + ? withoutVersionQuery + : pathToFileURL(withoutVersionQuery) return { - code: `(global, exports, importMeta, ssrImport, ssrDynamicImport, ssrExportAll) => import('${(pathToFileURL(withoutVersionQuery))}').then(r => { exports.default = r.default; ssrExportAll(r) }).catch(e => { console.error(e); throw new Error('[vite dev] Error loading external "${id}".') })`, + code: `(global, exports, importMeta, ssrImport, ssrDynamicImport, ssrExportAll) => import('${path}').then(r => { exports.default = r.default; ssrExportAll(r) }).catch(e => { console.error(e); throw new Error('[vite dev] Error loading external "${id}".') })`, deps: [], dynamicDeps: [] }