Skip to content

Commit db22c67

Browse files
committedJul 4, 2023
fix(vite-node): allow importing node:test
1 parent 0819275 commit db22c67

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed
 

‎packages/vite-node/src/externalize.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { existsSync } from 'node:fs'
2-
import { isNodeBuiltin, isValidNodeImport } from 'mlly'
2+
import { isValidNodeImport } from 'mlly'
33
import { join } from 'pathe'
44
import type { DepsHandlingOptions } from './types'
5-
import { slash } from './utils'
5+
import { isNodeBuiltin, slash } from './utils'
66

77
const KNOWN_ASSET_TYPES = [
88
// images

‎packages/vite-node/src/utils.ts

+26-17
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,31 @@ export function isInternalRequest(id: string): boolean {
5050
return internalRequestRegexp.test(id)
5151
}
5252

53+
const prefixedBuiltins = new Set([
54+
'node:test',
55+
])
56+
57+
const builtins = new Set([
58+
...builtinModules,
59+
'assert/strict',
60+
'diagnostics_channel',
61+
'dns/promises',
62+
'fs/promises',
63+
'path/posix',
64+
'path/win32',
65+
'readline/promises',
66+
'stream/consumers',
67+
'stream/promises',
68+
'stream/web',
69+
'timers/promises',
70+
'util/types',
71+
'wasi',
72+
])
73+
5374
export function normalizeModuleId(id: string) {
75+
// unique id that is not available as "test"
76+
if (prefixedBuiltins.has(id))
77+
return id
5478
return id
5579
.replace(/\\/g, '/')
5680
.replace(/^\/@fs\//, isWindows ? '' : '/')
@@ -91,25 +115,10 @@ export function toFilePath(id: string, root: string): { path: string; exists: bo
91115
}
92116
}
93117

94-
const builtins = new Set([
95-
...builtinModules,
96-
'assert/strict',
97-
'diagnostics_channel',
98-
'dns/promises',
99-
'fs/promises',
100-
'path/posix',
101-
'path/win32',
102-
'readline/promises',
103-
'stream/consumers',
104-
'stream/promises',
105-
'stream/web',
106-
'timers/promises',
107-
'util/types',
108-
'wasi',
109-
])
110-
111118
const NODE_BUILTIN_NAMESPACE = 'node:'
112119
export function isNodeBuiltin(id: string): boolean {
120+
if (prefixedBuiltins.has(id))
121+
return true
113122
return builtins.has(
114123
id.startsWith(NODE_BUILTIN_NAMESPACE)
115124
? id.slice(NODE_BUILTIN_NAMESPACE.length)

‎test/core/src/cjs/promise-export.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = Promise.resolve({ value: 42 })

‎test/core/test/builtin.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import testModule, { run } from 'node:test'
2+
import { expect, it } from 'vitest'
3+
4+
it('node:test works correctly', () => {
5+
expect(run).toBeTypeOf('function')
6+
expect(testModule).toBeTypeOf('function')
7+
expect(testModule.run).toBeTypeOf('function')
8+
})

0 commit comments

Comments
 (0)
Please sign in to comment.