Skip to content

Commit

Permalink
feat: improve deno and bun support (#14379)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Sep 18, 2023
1 parent 830b26e commit 9884308
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
9 changes: 6 additions & 3 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
dynamicImport,
isBuiltin,
isExternalUrl,
isNodeBuiltin,
isObject,
lookupFile,
mergeAlias,
Expand Down Expand Up @@ -1083,13 +1084,15 @@ async function bundleConfigFile(
if (
kind === 'entry-point' ||
path.isAbsolute(id) ||
isBuiltin(id)
isNodeBuiltin(id)
) {
return
}

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

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ async function nodeImport(
resolveOptions: InternalResolveOptionsWithOverrideConditions,
) {
let url: string
if (id.startsWith('node:') || id.startsWith('data:') || isBuiltin(id)) {
if (id.startsWith('data:') || isBuiltin(id)) {
url = id
} else {
const resolved = tryNodeResolve(
Expand Down
40 changes: 17 additions & 23 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,31 +88,25 @@ export const flattenId = (id: string): string =>
export const normalizeId = (id: string): string =>
id.replace(replaceNestedIdRE, ' > ')

//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.
const builtins = new Set([
...builtinModules,
'assert/strict',
'diagnostics_channel',
'dns/promises',
'fs/promises',
'path/posix',
'path/win32',
'readline/promises',
'stream/consumers',
'stream/promises',
'stream/web',
'timers/promises',
'util/types',
'wasi',
])

// Supported by Node, Deno, Bun
const NODE_BUILTIN_NAMESPACE = 'node:'
// Supported by Deno
const NPM_BUILTIN_NAMESPACE = 'npm:'
// Supported by Bun
const BUN_BUILTIN_NAMESPACE = 'bun:'
// Some runtimes like Bun injects namespaced modules here, which is not a node builtin
const nodeBuiltins = builtinModules.filter((id) => !id.includes(':'))

// TODO: Use `isBuiltin` from `node:module`, but Deno doesn't support it
export function isBuiltin(id: string): boolean {
return builtins.has(
id.startsWith(NODE_BUILTIN_NAMESPACE)
? id.slice(NODE_BUILTIN_NAMESPACE.length)
: id,
)
if (process.versions.deno && id.startsWith(NPM_BUILTIN_NAMESPACE)) return true
if (process.versions.bun && id.startsWith(BUN_BUILTIN_NAMESPACE)) return true
return isNodeBuiltin(id)
}

export function isNodeBuiltin(id: string): boolean {
if (id.startsWith(NODE_BUILTIN_NAMESPACE)) return true
return nodeBuiltins.includes(id)
}

export function isInNodeModules(id: string): boolean {
Expand Down

0 comments on commit 9884308

Please sign in to comment.