Skip to content

Commit

Permalink
fix(vite): correctly import node builtins in dev bundler (nuxt#2753)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jan 17, 2022
1 parent 2ba5f56 commit 4b35150
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/vite/src/dev-bundler.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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: []
}
Expand Down

0 comments on commit 4b35150

Please sign in to comment.