Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve deno and bun support #14379

Merged
merged 1 commit into from Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/vite/src/node/config.ts
Expand Up @@ -33,6 +33,7 @@ import {
dynamicImport,
isBuiltin,
isExternalUrl,
isNodeBuiltin,
isObject,
lookupFile,
mergeAlias,
Expand Down Expand Up @@ -1081,13 +1082,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
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
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