Skip to content

Commit

Permalink
fix(register): resolve builtin modules without node: protocal (#803)
Browse files Browse the repository at this point in the history
* fix(register): resolve builtin modules without node: protocal

* skip snapshots test on Windows
  • Loading branch information
Brooooooklyn authored Jul 5, 2024
1 parent 3586b0a commit a4a6832
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 4 deletions.
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/ava@6.1.3_encoding@0.1.13/node_modules/ava/lib/test.js:525:26)␊
at Test.run (file:///node_modules/.pnpm/ava@6.1.3_encoding@0.1.13/node_modules/ava/lib/test.js:534:33)␊
at Runner.runSingle (file:///node_modules/.pnpm/ava@6.1.3_encoding@0.1.13/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.

0 comments on commit a4a6832

Please sign in to comment.