Skip to content

Commit 8bc1f9d

Browse files
committed
feat: improve deno and bun support (#14379)
1 parent 3d63ae6 commit 8bc1f9d

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

packages/vite/src/node/config.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
dynamicImport,
3434
isBuiltin,
3535
isExternalUrl,
36+
isNodeBuiltin,
3637
isObject,
3738
lookupFile,
3839
mergeAlias,
@@ -1082,13 +1083,15 @@ async function bundleConfigFile(
10821083
if (
10831084
kind === 'entry-point' ||
10841085
path.isAbsolute(id) ||
1085-
isBuiltin(id)
1086+
isNodeBuiltin(id)
10861087
) {
10871088
return
10881089
}
10891090

1090-
// partial deno support as `npm:` does not work with esbuild
1091-
if (id.startsWith('npm:')) {
1091+
// With the `isNodeBuiltin` check above, this check captures if the builtin is a
1092+
// non-node built-in, which esbuild doesn't know how to handle. In that case, we
1093+
// externalize it so the non-node runtime handles it instead.
1094+
if (isBuiltin(id)) {
10921095
return { external: true }
10931096
}
10941097

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ async function nodeImport(
268268
resolveOptions: InternalResolveOptionsWithOverrideConditions,
269269
) {
270270
let url: string
271-
if (id.startsWith('node:') || id.startsWith('data:') || isBuiltin(id)) {
271+
if (id.startsWith('data:') || isBuiltin(id)) {
272272
url = id
273273
} else {
274274
const resolved = tryNodeResolve(

packages/vite/src/node/utils.ts

+17-23
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,25 @@ export const flattenId = (id: string): string =>
8888
export const normalizeId = (id: string): string =>
8989
id.replace(replaceNestedIdRE, ' > ')
9090

91-
//TODO: revisit later to see if the edge case that "compiling using node v12 code to be run in node v16 in the server" is what we intend to support.
92-
const builtins = new Set([
93-
...builtinModules,
94-
'assert/strict',
95-
'diagnostics_channel',
96-
'dns/promises',
97-
'fs/promises',
98-
'path/posix',
99-
'path/win32',
100-
'readline/promises',
101-
'stream/consumers',
102-
'stream/promises',
103-
'stream/web',
104-
'timers/promises',
105-
'util/types',
106-
'wasi',
107-
])
108-
91+
// Supported by Node, Deno, Bun
10992
const NODE_BUILTIN_NAMESPACE = 'node:'
93+
// Supported by Deno
94+
const NPM_BUILTIN_NAMESPACE = 'npm:'
95+
// Supported by Bun
96+
const BUN_BUILTIN_NAMESPACE = 'bun:'
97+
// Some runtimes like Bun injects namespaced modules here, which is not a node builtin
98+
const nodeBuiltins = builtinModules.filter((id) => !id.includes(':'))
99+
100+
// TODO: Use `isBuiltin` from `node:module`, but Deno doesn't support it
110101
export function isBuiltin(id: string): boolean {
111-
return builtins.has(
112-
id.startsWith(NODE_BUILTIN_NAMESPACE)
113-
? id.slice(NODE_BUILTIN_NAMESPACE.length)
114-
: id,
115-
)
102+
if (process.versions.deno && id.startsWith(NPM_BUILTIN_NAMESPACE)) return true
103+
if (process.versions.bun && id.startsWith(BUN_BUILTIN_NAMESPACE)) return true
104+
return isNodeBuiltin(id)
105+
}
106+
107+
export function isNodeBuiltin(id: string): boolean {
108+
if (id.startsWith(NODE_BUILTIN_NAMESPACE)) return true
109+
return nodeBuiltins.includes(id)
116110
}
117111

118112
export function isInNodeModules(id: string): boolean {

0 commit comments

Comments
 (0)