Skip to content

Commit 075ab35

Browse files
authored
fix: keep built-in id as is in bun and deno (#9117)
1 parent ddf2d89 commit 075ab35

File tree

3 files changed

+65
-20
lines changed

3 files changed

+65
-20
lines changed

packages/vitest/src/node/pools/rpc.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { RuntimeRPC } from '../../types/rpc'
22
import type { TestProject } from '../project'
33
import type { ResolveSnapshotPathHandlerContext } from '../types/config'
44
import { existsSync, mkdirSync } from 'node:fs'
5-
import { isBuiltin } from 'node:module'
65
import { fileURLToPath } from 'node:url'
76
import { cleanUrl } from '@vitest/utils/helpers'
7+
import { isBuiltin, toBuiltin } from '../../utils/modules'
88
import { handleRollupError } from '../environments/fetchModule'
99
import { normalizeResolvedIdToUrl } from '../environments/normalizeUrl'
1010

@@ -69,9 +69,10 @@ export function createMethodsRPC(project: TestProject, methodsOptions: MethodsOp
6969
if (resolved.external) {
7070
return {
7171
file,
72-
// TODO: use isBuiltin defined in the environment
73-
url: !resolved.id.startsWith('node:') && isBuiltin(resolved.id)
74-
? `node:${resolved.id}`
72+
// this is only used by the module mocker and it always
73+
// standardizes the id to mock "node:url" and "url" at the same time
74+
url: isBuiltin(resolved.id)
75+
? toBuiltin(resolved.id)
7576
: resolved.id,
7677
id: resolved.id,
7778
}

packages/vitest/src/runtime/moduleRunner/startModuleRunner.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import type { Traces } from '../../utils/traces'
55
import type { ExternalModulesExecutor } from '../external-executor'
66
import type { CreateImportMeta } from './moduleRunner'
77
import fs from 'node:fs'
8-
import { isBuiltin } from 'node:module'
98
import { isBareImport } from '@vitest/utils/helpers'
9+
import { isBrowserExternal, isBuiltin, toBuiltin } from '../../utils/modules'
1010
import { getCachedVitestImport } from './cachedResolver'
1111
import { listenForErrors } from './errorCatcher'
1212
import { unwrapId, VitestModuleEvaluator } from './moduleEvaluator'
@@ -15,9 +15,6 @@ import { VitestModuleRunner } from './moduleRunner'
1515

1616
const { readFileSync } = fs
1717

18-
const browserExternalId = '__vite-browser-external'
19-
const browserExternalLength = browserExternalId.length + 1 // 1 is ":"
20-
2118
export const VITEST_VM_CONTEXT_SYMBOL: string = '__vitest_vm_context__'
2219

2320
export interface ContextModuleRunnerOptions {
@@ -124,7 +121,11 @@ export function startVitestModuleRunner(options: ContextModuleRunnerOptions): Vi
124121
}
125122
}
126123

127-
if (isBuiltin(rawId) || rawId.startsWith(browserExternalId)) {
124+
if (isBuiltin(rawId)) {
125+
return { externalize: rawId, type: 'builtin' }
126+
}
127+
128+
if (isBrowserExternal(rawId)) {
128129
return { externalize: toBuiltin(rawId), type: 'builtin' }
129130
}
130131

@@ -184,14 +185,3 @@ export function startVitestModuleRunner(options: ContextModuleRunnerOptions): Vi
184185

185186
return moduleRunner
186187
}
187-
188-
export function toBuiltin(id: string): string {
189-
if (id.startsWith(browserExternalId)) {
190-
id = id.slice(browserExternalLength)
191-
}
192-
193-
if (!id.startsWith('node:')) {
194-
id = `node:${id}`
195-
}
196-
return id
197-
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// copied from vite
2+
// https://github.com/vitejs/vite/blob/814120f2ad387ca3d1e16c7dd403b04ca4b97f75/packages/vite/src/node/utils.ts#L106
3+
import { builtinModules } from 'node:module'
4+
5+
// Supported by Node, Deno, Bun
6+
const NODE_BUILTIN_NAMESPACE = 'node:'
7+
// Supported by Deno
8+
const NPM_BUILTIN_NAMESPACE = 'npm:'
9+
// Supported by Bun
10+
const BUN_BUILTIN_NAMESPACE = 'bun:'
11+
// Some runtimes like Bun injects namespaced modules here, which is not a node builtin
12+
const nodeBuiltins = builtinModules.filter(id => !id.includes(':'))
13+
14+
// TODO: Use `isBuiltin` from `node:module`, but Deno doesn't support it
15+
export function isBuiltin(id: string): boolean {
16+
if (process.versions.deno && id.startsWith(NPM_BUILTIN_NAMESPACE)) {
17+
return true
18+
}
19+
if (process.versions.bun && id.startsWith(BUN_BUILTIN_NAMESPACE)) {
20+
return true
21+
}
22+
return isNodeBuiltin(id)
23+
}
24+
25+
export function isNodeBuiltin(id: string): boolean {
26+
if (id.startsWith(NODE_BUILTIN_NAMESPACE)) {
27+
return true
28+
}
29+
return nodeBuiltins.includes(id)
30+
}
31+
32+
const browserExternalId = '__vite-browser-external'
33+
const browserExternalLength = browserExternalId.length + 1 // 1 is ":"
34+
35+
export function isBrowserExternal(id: string): boolean {
36+
return id.startsWith(browserExternalId)
37+
}
38+
39+
export function toBuiltin(id: string): string {
40+
if (id.startsWith(browserExternalId)) {
41+
id = id.slice(browserExternalLength)
42+
}
43+
if (
44+
id.startsWith(NPM_BUILTIN_NAMESPACE)
45+
|| id.startsWith(BUN_BUILTIN_NAMESPACE)
46+
|| id.startsWith(NODE_BUILTIN_NAMESPACE)
47+
) {
48+
return id
49+
}
50+
if (process.versions.deno || process.versions.bun) {
51+
return id
52+
}
53+
return `node:${id}`
54+
}

0 commit comments

Comments
 (0)