1
1
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'
3
10
import { extname , join } from 'path'
4
11
import { fileURLToPath , parse as parseUrl , pathToFileURL } from 'url'
5
12
@@ -14,73 +21,7 @@ import { compile } from '../lib/register.js'
14
21
15
22
const debug = debugFactory ( '@swc-node' )
16
23
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 )
84
25
85
26
const tsconfig : ts . CompilerOptions = readDefaultTsConfig ( )
86
27
tsconfig . module = ts . ModuleKind . ESNext
@@ -263,7 +204,7 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
263
204
return addShortCircuitSignal ( await nextResolve ( specifier ) )
264
205
}
265
206
266
- const { error, path } = await resolver . async (
207
+ const { error, path, moduleType } = await resolver . async (
267
208
join ( fileURLToPath ( context . parentURL ) , '..' ) ,
268
209
specifier . startsWith ( 'file:' ) ? fileURLToPath ( specifier ) : specifier ,
269
210
)
@@ -283,7 +224,12 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
283
224
return addShortCircuitSignal ( {
284
225
...context ,
285
226
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' ,
287
233
} )
288
234
}
289
235
0 commit comments