Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit db22c67

Browse filesBrowse files
committed
fix(vite-node): allow importing node:test
1 parent 0819275 commit db22c67
Copy full SHA for db22c67

4 files changed

+37-19Lines changed: 37 additions & 19 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: packages/vite-node/src/externalize.ts
+2-2Lines changed: 2 additions & 2 deletions
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
Collapse file

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

Copy file name to clipboardExpand all lines: packages/vite-node/src/utils.ts
+26-17Lines changed: 26 additions & 17 deletions
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)
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = Promise.resolve({ value: 42 })
Collapse file

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

Copy file name to clipboard
+8Lines changed: 8 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.