Skip to content

Commit 9aad5b0

Browse files
authored
fix(register): resolve internal cjs module (#811)
1 parent 31c6043 commit 9aad5b0

File tree

10 files changed

+210
-116
lines changed

10 files changed

+210
-116
lines changed

packages/core/index.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Options as SwcOptions, ReactConfig, Config, JscTarget } from '@swc/core'
2+
export interface Options {
3+
target?: JscTarget
4+
module?: 'commonjs' | 'umd' | 'amd' | 'es6'
5+
sourcemap?: Config['sourceMaps']
6+
jsx?: boolean
7+
experimentalDecorators?: boolean
8+
emitDecoratorMetadata?: boolean
9+
useDefineForClassFields?: boolean
10+
dynamicImport?: boolean
11+
esModuleInterop?: boolean
12+
keepClassNames?: boolean
13+
externalHelpers?: boolean
14+
react?: Partial<ReactConfig>
15+
baseUrl?: string
16+
paths?: {
17+
[from: string]: [string]
18+
}
19+
swc?: SwcOptions
20+
ignoreDynamic?: boolean
21+
}
22+
export declare function transformSync(source: string, path: string, options?: Options): import('@swc/types').Output
23+
export declare function transformJest(source: string, path: string, options?: Options): import('@swc/types').Output
24+
export declare function transform(source: string, path: string, options?: Options): Promise<import('@swc/types').Output>

packages/core/index.js

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

packages/core/index.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare const exports: {
2+
default: () => string
3+
}
4+
5+
export default exports
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// this undesirable output was commonly generated by babel w browser-breaking
2+
// defineProperty and default.default definitions
3+
4+
/* eslint-disable */
5+
'use strict'
6+
7+
Object.defineProperty(exports, '__esModule', {
8+
value: true,
9+
})
10+
exports['default'] = void 0
11+
12+
function _typeof(obj) {
13+
'@babel/helpers - typeof'
14+
if (typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol') {
15+
_typeof = function _typeof(obj) {
16+
return typeof obj
17+
}
18+
} else {
19+
_typeof = function _typeof(obj) {
20+
return obj && typeof Symbol === 'function' && obj.constructor === Symbol && obj !== Symbol.prototype
21+
? 'symbol'
22+
: typeof obj
23+
}
24+
}
25+
return _typeof(obj)
26+
}
27+
28+
var _default = function _default() {
29+
return 'default.default'
30+
}
31+
32+
exports['default'] = _default
33+
/* eslint-enable */
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"description": "problematic cjs file type generated by babel",
3+
"main": "./index.js",
4+
"types": "./index.d.ts"
5+
}

packages/integrate-module/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { renderToString } from 'react-dom/server'
99
import { simpleGit } from 'simple-git'
1010

1111
import { CompiledClass } from './compiled.js'
12+
import cjs from './cjs'
1213
import { foo } from './foo.mjs'
1314
import { bar } from './subdirectory/bar.mjs'
1415
import { baz } from './subdirectory/index.mjs'
@@ -57,3 +58,7 @@ await test('resolve @napi-rs projects', () => {
5758
await test('resolve simple-git', () => {
5859
assert.ok(simpleGit)
5960
})
61+
62+
await test('resolve local cjs module', () => {
63+
assert.equal(cjs.default(), 'default.default')
64+
})

packages/register/esm.mts

Lines changed: 16 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { readFile } from 'fs/promises'
2-
import { createRequire, type LoadFnOutput, type LoadHook, type ResolveFnOutput, type ResolveHook } from 'node:module'
2+
import {
3+
createRequire,
4+
type LoadFnOutput,
5+
type LoadHook,
6+
type ResolveFnOutput,
7+
type ResolveHook,
8+
builtinModules,
9+
} from 'node:module'
310
import { extname, join } from 'path'
411
import { fileURLToPath, parse as parseUrl, pathToFileURL } from 'url'
512

@@ -14,73 +21,7 @@ import { compile } from '../lib/register.js'
1421

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

17-
const builtin = new Set([
18-
'_http_agent',
19-
'_http_client',
20-
'_http_common',
21-
'_http_incoming',
22-
'_http_outgoing',
23-
'_http_server',
24-
'_stream_duplex',
25-
'_stream_passthrough',
26-
'_stream_readable',
27-
'_stream_transform',
28-
'_stream_wrap',
29-
'_stream_writable',
30-
'_tls_common',
31-
'_tls_wrap',
32-
'assert',
33-
'assert/strict',
34-
'async_hooks',
35-
'buffer',
36-
'child_process',
37-
'cluster',
38-
'console',
39-
'constants',
40-
'crypto',
41-
'dgram',
42-
'diagnostics_channel',
43-
'dns',
44-
'dns/promises',
45-
'domain',
46-
'events',
47-
'fs',
48-
'fs/promises',
49-
'http',
50-
'http2',
51-
'https',
52-
'inspector',
53-
'module',
54-
'net',
55-
'os',
56-
'path',
57-
'path/posix',
58-
'path/win32',
59-
'perf_hooks',
60-
'process',
61-
'punycode',
62-
'querystring',
63-
'readline',
64-
'repl',
65-
'stream',
66-
'stream/consumers',
67-
'stream/promises',
68-
'stream/web',
69-
'string_decoder',
70-
'sys',
71-
'timers',
72-
'timers/promises',
73-
'tls',
74-
'trace_events',
75-
'tty',
76-
'url',
77-
'util',
78-
'util/types',
79-
'v8',
80-
'vm',
81-
'worker_threads',
82-
'zlib',
83-
])
24+
const builtin = new Set(builtinModules)
8425

8526
const tsconfig: ts.CompilerOptions = readDefaultTsConfig()
8627
tsconfig.module = ts.ModuleKind.ESNext
@@ -263,7 +204,7 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
263204
return addShortCircuitSignal(await nextResolve(specifier))
264205
}
265206

266-
const { error, path } = await resolver.async(
207+
const { error, path, moduleType } = await resolver.async(
267208
join(fileURLToPath(context.parentURL), '..'),
268209
specifier.startsWith('file:') ? fileURLToPath(specifier) : specifier,
269210
)
@@ -283,7 +224,12 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
283224
return addShortCircuitSignal({
284225
...context,
285226
url: url.href,
286-
format: 'module',
227+
format:
228+
moduleType === 'module'
229+
? 'module'
230+
: path.endsWith('cjs') || path.endsWith('cts') || moduleType === 'commonjs' || !moduleType
231+
? 'commonjs'
232+
: 'module',
287233
})
288234
}
289235

packages/register/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@swc-node/sourcemap-support": "^0.5.1",
4545
"colorette": "^2.0.20",
4646
"debug": "^4.3.5",
47-
"oxc-resolver": "^1.9.2",
47+
"oxc-resolver": "^1.10.0",
4848
"pirates": "^4.0.6",
4949
"tslib": "^2.6.3"
5050
},

0 commit comments

Comments
 (0)