Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(register): resolve builtin modules without node: protocal #803

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/integrate-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
"p-timeout": "^6.1.2"
},
"devDependencies": {
"@napi-rs/simple-git": "^0.1.16",
"@swc/core": "^1.6.6",
"@swc-node/register": "workspace:*",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"simple-git": "^3.25.0",
"typescript": "^5.5.3"
}
}
10 changes: 10 additions & 0 deletions packages/integrate-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import assert from 'node:assert'
import test from 'node:test'

import { RepositoryState } from '@napi-rs/simple-git'
import { bar as subBar } from '@subdirectory/bar.mjs'
import { supportedExtensions } from 'file-type'
import { renderToString } from 'react-dom/server'
import { simpleGit } from 'simple-git'

import { CompiledClass } from './compiled.js'
import { foo } from './foo.mjs'
Expand Down Expand Up @@ -47,3 +49,11 @@ await test('compiled js file with .d.ts', () => {
await test('jsx should work', () => {
assert.equal(renderToString(Component()), '<div>Component</div>')
})

await test('resolve @napi-rs projects', () => {
assert.equal(RepositoryState.ApplyMailbox, 10)
})

await test('resolve simple-git', () => {
assert.ok(simpleGit)
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 1

`Error: ␊
at /packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts:15:26␊
at /packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts:18:26␊
at Test.callFn (file:///node_modules/.pnpm/[email protected][email protected]/node_modules/ava/lib/test.js:525:26)␊
at Test.run (file:///node_modules/.pnpm/[email protected][email protected]/node_modules/ava/lib/test.js:534:33)␊
at Runner.runSingle (file:///node_modules/.pnpm/[email protected][email protected]/node_modules/ava/lib/runner.js:281:33)␊
Expand Down
Binary file not shown.
3 changes: 3 additions & 0 deletions packages/integrate/__tests__/sourcemaps/sourcemaps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ interface _Unused8 {}
interface _Unused9 {}

test('should work with sourcemaps', (t) => {
if (process.platform === 'win32') {
return t.pass('Skip on Windows')
}
const projectRoot = join(__dirname, '..', '..', '..', '..')
t.snapshot(
new Error().stack
Expand Down
69 changes: 66 additions & 3 deletions packages/register/esm.mts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,62 @@ import { AVAILABLE_TS_EXTENSION_PATTERN, compile } from '../lib/register.js'

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

const builtin = new Set([
'assert',
'assert/strict',
'async_hooks',
'buffer',
'child_process',
'cluster',
'console',
'constants',
'crypto',
'dgram',
'diagnostics_channel',
'dns',
'dns/promises',
'domain',
'events',
'fs',
'fs/promises',
'http',
'http2',
'https',
'inspector',
'inspector/promises',
'module',
'net',
'os',
'path',
'path/posix',
'path/win32',
'perf_hooks',
'process',
'punycode',
'querystring',
'readline',
'readline/promises',
'repl',
'stream',
'stream/consumers',
'stream/promises',
'stream/web',
'string_decoder',
'timers',
'timers/promises',
'tls',
'trace_events',
'tty',
'url',
'util',
'util/types',
'v8',
'vm',
'wasi',
'worker_threads',
'zlib',
])

const tsconfig: ts.CompilerOptions = readDefaultTsConfig()
tsconfig.module = ts.ModuleKind.ESNext

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

const INTERNAL_MODULE_PATTERN = /^(node|nodejs):/

const EXTENSION_MODULE_MAP = {
'.mjs': 'module',
'.cjs': 'commonjs',
Expand All @@ -139,7 +193,7 @@ const EXTENSION_MODULE_MAP = {
export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
debug('resolve', specifier, JSON.stringify(context))

if (INTERNAL_MODULE_PATTERN.test(specifier)) {
if (specifier.startsWith('node:') || specifier.startsWith('nodejs:')) {
debug('skip resolve: internal format', specifier)

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

if (builtin.has(specifier)) {
debug('skip resolve: internal format', specifier)

return addShortCircuitSignal({
url: `node:${specifier}`,
format: 'builtin',
})
}

if (specifier.startsWith('data:')) {
debug('skip resolve: data url', specifier)

Expand Down
148 changes: 148 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.