Skip to content

Commit a4a6832

Browse files
authored
fix(register): resolve builtin modules without node: protocal (#803)
* fix(register): resolve builtin modules without node: protocal * skip snapshots test on Windows
1 parent 3586b0a commit a4a6832

File tree

7 files changed

+230
-4
lines changed

7 files changed

+230
-4
lines changed

packages/integrate-module/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
"p-timeout": "^6.1.2"
99
},
1010
"devDependencies": {
11+
"@napi-rs/simple-git": "^0.1.16",
1112
"@swc/core": "^1.6.6",
1213
"@swc-node/register": "workspace:*",
1314
"@types/react": "^18.3.3",
1415
"@types/react-dom": "^18.3.0",
1516
"react": "^18.3.1",
1617
"react-dom": "^18.3.1",
18+
"simple-git": "^3.25.0",
1719
"typescript": "^5.5.3"
1820
}
1921
}

packages/integrate-module/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
import assert from 'node:assert'
33
import test from 'node:test'
44

5+
import { RepositoryState } from '@napi-rs/simple-git'
56
import { bar as subBar } from '@subdirectory/bar.mjs'
67
import { supportedExtensions } from 'file-type'
78
import { renderToString } from 'react-dom/server'
9+
import { simpleGit } from 'simple-git'
810

911
import { CompiledClass } from './compiled.js'
1012
import { foo } from './foo.mjs'
@@ -47,3 +49,11 @@ await test('compiled js file with .d.ts', () => {
4749
await test('jsx should work', () => {
4850
assert.equal(renderToString(Component()), '<div>Component</div>')
4951
})
52+
53+
await test('resolve @napi-rs projects', () => {
54+
assert.equal(RepositoryState.ApplyMailbox, 10)
55+
})
56+
57+
await test('resolve simple-git', () => {
58+
assert.ok(simpleGit)
59+
})

packages/integrate/__tests__/sourcemaps/__snapshots__/sourcemaps.spec.ts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Generated by [AVA](https://avajs.dev).
99
> Snapshot 1
1010
1111
`Error: ␊
12-
at /packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts:15:26␊
12+
at /packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts:18:26␊
1313
at Test.callFn (file:///node_modules/.pnpm/[email protected][email protected]/node_modules/ava/lib/test.js:525:26)␊
1414
at Test.run (file:///node_modules/.pnpm/[email protected][email protected]/node_modules/ava/lib/test.js:534:33)␊
1515
at Runner.runSingle (file:///node_modules/.pnpm/[email protected][email protected]/node_modules/ava/lib/runner.js:281:33)␊
Binary file not shown.

packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ interface _Unused8 {}
2222
interface _Unused9 {}
2323

2424
test('should work with sourcemaps', (t) => {
25+
if (process.platform === 'win32') {
26+
return t.pass('Skip on Windows')
27+
}
2528
const projectRoot = join(__dirname, '..', '..', '..', '..')
2629
t.snapshot(
2730
new Error().stack

packages/register/esm.mts

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,62 @@ import { AVAILABLE_TS_EXTENSION_PATTERN, compile } from '../lib/register.js'
1414

1515
const debug = debugFactory('@swc-node')
1616

17+
const builtin = new Set([
18+
'assert',
19+
'assert/strict',
20+
'async_hooks',
21+
'buffer',
22+
'child_process',
23+
'cluster',
24+
'console',
25+
'constants',
26+
'crypto',
27+
'dgram',
28+
'diagnostics_channel',
29+
'dns',
30+
'dns/promises',
31+
'domain',
32+
'events',
33+
'fs',
34+
'fs/promises',
35+
'http',
36+
'http2',
37+
'https',
38+
'inspector',
39+
'inspector/promises',
40+
'module',
41+
'net',
42+
'os',
43+
'path',
44+
'path/posix',
45+
'path/win32',
46+
'perf_hooks',
47+
'process',
48+
'punycode',
49+
'querystring',
50+
'readline',
51+
'readline/promises',
52+
'repl',
53+
'stream',
54+
'stream/consumers',
55+
'stream/promises',
56+
'stream/web',
57+
'string_decoder',
58+
'timers',
59+
'timers/promises',
60+
'tls',
61+
'trace_events',
62+
'tty',
63+
'url',
64+
'util',
65+
'util/types',
66+
'v8',
67+
'vm',
68+
'wasi',
69+
'worker_threads',
70+
'zlib',
71+
])
72+
1773
const tsconfig: ts.CompilerOptions = readDefaultTsConfig()
1874
tsconfig.module = ts.ModuleKind.ESNext
1975

@@ -123,8 +179,6 @@ export const getPackageType = async (url: string) => {
123179
return packageJson?.type ?? undefined
124180
}
125181

126-
const INTERNAL_MODULE_PATTERN = /^(node|nodejs):/
127-
128182
const EXTENSION_MODULE_MAP = {
129183
'.mjs': 'module',
130184
'.cjs': 'commonjs',
@@ -139,7 +193,7 @@ const EXTENSION_MODULE_MAP = {
139193
export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
140194
debug('resolve', specifier, JSON.stringify(context))
141195

142-
if (INTERNAL_MODULE_PATTERN.test(specifier)) {
196+
if (specifier.startsWith('node:') || specifier.startsWith('nodejs:')) {
143197
debug('skip resolve: internal format', specifier)
144198

145199
return addShortCircuitSignal({
@@ -148,6 +202,15 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
148202
})
149203
}
150204

205+
if (builtin.has(specifier)) {
206+
debug('skip resolve: internal format', specifier)
207+
208+
return addShortCircuitSignal({
209+
url: `node:${specifier}`,
210+
format: 'builtin',
211+
})
212+
}
213+
151214
if (specifier.startsWith('data:')) {
152215
debug('skip resolve: data url', specifier)
153216

pnpm-lock.yaml

Lines changed: 148 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)