fix(vite): correctly import node builtins in dev bundler (#2753)

This commit is contained in:
Daniel Roe 2022-01-17 10:36:44 +00:00 committed by GitHub
parent 2ba5f56374
commit 4b351504e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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: []
}