Skip to content

Commit 1cbc24d

Browse files
authored
fix: don't resolve builtin Node modules (#2538)
* fix: use "node:" prefix in Vitest imports * chore: add test for prefix * chore: cleanup * chore: use more node protocol * chore: cleanup * chore: cleanup
1 parent 7ae1417 commit 1cbc24d

39 files changed

+112
-60
lines changed

packages/browser/rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const external = [
1010
...Object.keys(pkg.dependencies),
1111
...Object.keys(pkg.peerDependencies || {}),
1212
'worker_threads',
13+
'node:worker_threads',
1314
]
1415

1516
const plugins = [

packages/ui/rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const external = [
1212
...Object.keys(pkg.dependencies),
1313
...Object.keys(pkg.peerDependencies || {}),
1414
'worker_threads',
15+
'node:worker_threads',
1516
'vitest/node',
1617
'vitest',
1718
]

packages/vite-node/rollup.config.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,11 @@ const external = [
2626
'pathe',
2727
'birpc',
2828
'vite',
29-
'url',
30-
'events',
29+
'node:url',
30+
'node:events',
3131
]
3232

3333
const plugins = [
34-
alias({
35-
entries: [
36-
{ find: /^node:(.+)$/, replacement: '$1' },
37-
],
38-
}),
3934
resolve({
4035
preferBuiltins: true,
4136
}),
@@ -68,7 +63,16 @@ export default defineConfig([
6863
chunkFileNames: 'chunk-[name].cjs',
6964
},
7065
external,
71-
plugins,
66+
plugins: [
67+
alias({
68+
entries: [
69+
// cjs in Node 14 doesn't support node: prefix
70+
// can be dropped, when we drop support for Node 14
71+
{ find: /^node:(.+)$/, replacement: '$1' },
72+
],
73+
}),
74+
...plugins,
75+
],
7276
onwarn,
7377
},
7478
{

packages/vite-node/src/client.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { createRequire } from 'module'
1+
import { createRequire } from 'node:module'
22
// we need native dirname, because windows __dirname has \\
3-
// eslint-disable-next-line no-restricted-imports
4-
import { dirname } from 'path'
5-
import { fileURLToPath, pathToFileURL } from 'url'
6-
import vm from 'vm'
3+
import { dirname } from 'node:path'
4+
import { fileURLToPath, pathToFileURL } from 'node:url'
5+
import vm from 'node:vm'
6+
import { isNodeBuiltin } from 'mlly'
77
import { resolve } from 'pathe'
88
import createDebug from 'debug'
99
import { VALID_ID_PREFIX, cleanUrl, isInternalRequest, isPrimitive, normalizeModuleId, normalizeRequestId, slash, toFilePath } from './utils'
@@ -190,8 +190,12 @@ export class ViteNodeRunner {
190190
}
191191
}
192192

193+
shouldResolveId(id: string, _importee?: string) {
194+
return !isInternalRequest(id) && !isNodeBuiltin(id)
195+
}
196+
193197
async resolveUrl(id: string, importee?: string): Promise<[url: string, fsPath: string]> {
194-
if (isInternalRequest(id))
198+
if (!this.shouldResolveId(id))
195199
return [id, id]
196200
// we don't pass down importee here, because otherwise Vite doesn't resolve it correctly
197201
if (importee && id.startsWith(VALID_ID_PREFIX))

packages/vite-node/src/debug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable no-console */
2-
import { existsSync, promises as fs } from 'fs'
2+
import { existsSync, promises as fs } from 'node:fs'
33
import { join, resolve } from 'pathe'
44
import type { TransformResult } from 'vite'
55
import c from 'picocolors'

packages/vite-node/src/externalize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync } from 'fs'
1+
import { existsSync } from 'node:fs'
22
import { isNodeBuiltin, isValidNodeImport } from 'mlly'
33
import type { DepsHandlingOptions } from './types'
44
import { slash } from './utils'

packages/vite-node/src/hmr/emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EventEmitter } from 'events'
1+
import { EventEmitter } from 'node:events'
22
import type { HMRPayload, Plugin } from 'vite'
33

44
export type EventType = string | symbol

packages/vite-node/src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { performance } from 'perf_hooks'
1+
import { performance } from 'node:perf_hooks'
22
import { resolve } from 'pathe'
33
import type { TransformResult, ViteDevServer } from 'vite'
44
import createDebug from 'debug'

packages/vite-node/src/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { fileURLToPath, pathToFileURL } from 'url'
2-
import { existsSync } from 'fs'
1+
import { fileURLToPath, pathToFileURL } from 'node:url'
2+
import { existsSync } from 'node:fs'
33
import { resolve } from 'pathe'
44
import type { Arrayable, Nullable } from './types'
55

@@ -23,7 +23,7 @@ export function normalizeRequestId(id: string, base?: string): string {
2323
.replace(/^\/@id\/__x00__/, '\0') // virtual modules start with `\0`
2424
.replace(/^\/@id\//, '')
2525
.replace(/^__vite-browser-external:/, '')
26-
.replace(/^(node|file):/, '')
26+
.replace(/^file:/, '')
2727
.replace(/^\/+/, '/') // remove duplicate leading slashes
2828
.replace(/\?v=\w+/, '?') // remove ?v= query
2929
.replace(/&v=\w+/, '') // remove &v= query
@@ -50,6 +50,7 @@ export function normalizeModuleId(id: string) {
5050
.replace(/\\/g, '/')
5151
.replace(/^\/@fs\//, isWindows ? '' : '/')
5252
.replace(/^file:\//, '/')
53+
.replace(/^node:/, '')
5354
.replace(/^\/+/, '/')
5455
}
5556

packages/vitest/rollup.config.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import dts from 'rollup-plugin-dts'
66
import nodeResolve from '@rollup/plugin-node-resolve'
77
import commonjs from '@rollup/plugin-commonjs'
88
import json from '@rollup/plugin-json'
9-
import alias from '@rollup/plugin-alias'
109
import license from 'rollup-plugin-license'
1110
import c from 'picocolors'
1211
import fg from 'fast-glob'
@@ -41,6 +40,8 @@ const external = [
4140
...Object.keys(pkg.dependencies),
4241
...Object.keys(pkg.peerDependencies),
4342
'worker_threads',
43+
'node:worker_threads',
44+
'node:fs',
4445
'inspector',
4546
'vite-node/source-map',
4647
'vite-node/client',
@@ -49,14 +50,6 @@ const external = [
4950
]
5051

5152
const plugins = [
52-
alias({
53-
entries: [
54-
{ find: /^node:(.+)$/, replacement: '$1' },
55-
{ find: 'vite-node/server', replacement: resolve(__dirname, '../vite-node/src/server.ts') },
56-
{ find: 'vite-node/client', replacement: resolve(__dirname, '../vite-node/src/client.ts') },
57-
{ find: 'vite-node/utils', replacement: resolve(__dirname, '../vite-node/src/utils.ts') },
58-
],
59-
}),
6053
nodeResolve({
6154
preferBuiltins: true,
6255
}),

0 commit comments

Comments
 (0)