-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
esm.mts
317 lines (253 loc) · 8.38 KB
/
esm.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import { readFile } from 'node:fs/promises'
import {
createRequire,
type LoadFnOutput,
type LoadHook,
type ResolveFnOutput,
type ResolveHook,
builtinModules,
} from 'node:module'
import { extname, isAbsolute, join } from 'node:path'
import { fileURLToPath, parse as parseUrl, pathToFileURL } from 'node:url'
import debugFactory from 'debug'
import { EnforceExtension, ResolverFactory } from 'oxc-resolver'
import ts from 'typescript'
// @ts-expect-error
import { readDefaultTsConfig } from '../lib/read-default-tsconfig.js'
// @ts-expect-error
import { compile, DEFAULT_EXTENSIONS } from '../lib/register.js'
const debug = debugFactory('@swc-node')
const builtin = new Set(builtinModules)
const tsconfig: ts.CompilerOptions = readDefaultTsConfig()
tsconfig.module = ts.ModuleKind.ESNext
const TSCONFIG_PATH = (function () {
const pathFromEnv =
process.env.SWC_NODE_PROJECT ?? process.env.TS_NODE_PROJECT ?? join(process.cwd(), 'tsconfig.json')
if (!isAbsolute(pathFromEnv)) {
return join(process.cwd(), pathFromEnv)
}
return pathFromEnv
})()
const resolver = new ResolverFactory({
tsconfig: {
configFile: TSCONFIG_PATH,
references: 'auto',
},
conditionNames: ['node', 'import'],
enforceExtension: EnforceExtension.Auto,
extensions: ['.js', '.mjs', '.cjs', '.ts', '.tsx', '.mts', '.cts', '.json', '.wasm', '.node'],
extensionAlias: {
'.js': ['.ts', '.tsx', '.js'],
'.mjs': ['.mts', '.mjs'],
'.cjs': ['.cts', '.cjs'],
},
})
const addShortCircuitSignal = <T extends ResolveFnOutput | LoadFnOutput>(input: T): T => {
return {
...input,
shortCircuit: true,
}
}
interface PackageJson {
name: string
version: string
type?: 'module' | 'commonjs'
main?: string
}
const packageJSONCache = new Map<string, undefined | PackageJson>()
const readFileIfExists = async (path: string) => {
try {
const content = await readFile(path, 'utf-8')
return JSON.parse(content)
} catch (e) {
// eslint-disable-next-line no-undef
if ((e as NodeJS.ErrnoException).code === 'ENOENT') {
return undefined
}
throw e
}
}
const readPackageJSON = async (path: string) => {
if (packageJSONCache.has(path)) {
return packageJSONCache.get(path)
}
const res = (await readFileIfExists(path)) as PackageJson
packageJSONCache.set(path, res)
return res
}
const getPackageForFile = async (url: string) => {
// use URL instead path.resolve to handle relative path
let packageJsonURL = new URL('./package.json', url)
// eslint-disable-next-line no-constant-condition
while (true) {
const path = fileURLToPath(packageJsonURL)
// for special case by some package manager
if (path.endsWith('node_modules/package.json')) {
break
}
const packageJson = await readPackageJSON(path)
if (!packageJson) {
const lastPath = packageJsonURL.pathname
packageJsonURL = new URL('../package.json', packageJsonURL)
// root level /package.json
if (packageJsonURL.pathname === lastPath) {
break
}
continue
}
if (packageJson.type && packageJson.type !== 'module' && packageJson.type !== 'commonjs') {
packageJson.type = undefined
}
return packageJson
}
return undefined
}
export const getPackageType = async (url: string) => {
const packageJson = await getPackageForFile(url)
return packageJson?.type ?? undefined
}
const EXTENSION_MODULE_MAP = {
'.mjs': 'module',
'.cjs': 'commonjs',
'.ts': 'module',
'.tsx': 'module',
'.mts': 'module',
'.cts': 'commonjs',
'.json': 'json',
'.wasm': 'wasm',
'.node': 'commonjs',
} as const
export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
debug('resolve', specifier, JSON.stringify(context))
if (specifier.startsWith('node:') || specifier.startsWith('nodejs:')) {
debug('skip resolve: internal format', specifier)
return addShortCircuitSignal({
url: specifier,
format: 'builtin',
})
}
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)
return addShortCircuitSignal({
url: specifier,
})
}
const parsedUrl = parseUrl(specifier)
// as entrypoint, just return specifier
if (!context.parentURL || parsedUrl.protocol === 'file:') {
debug('skip resolve: absolute path or entrypoint', specifier)
let format: ResolveFnOutput['format'] = null
const specifierPath = fileURLToPath(specifier)
const ext = extname(specifierPath)
if (ext === '.js') {
format = (await getPackageType(specifier)) === 'module' ? 'module' : 'commonjs'
} else {
format = EXTENSION_MODULE_MAP[ext as keyof typeof EXTENSION_MODULE_MAP]
}
return addShortCircuitSignal({
url: specifier,
format,
})
}
// import attributes, support json currently
if (context.importAttributes?.type) {
debug('skip resolve: import attributes', specifier)
return addShortCircuitSignal(await nextResolve(specifier))
}
const { error, path, moduleType } = await resolver.async(
join(fileURLToPath(context.parentURL), '..'),
specifier.startsWith('file:') ? fileURLToPath(specifier) : specifier,
)
if (error) {
throw new Error(`${error}: ${specifier} cannot be resolved in ${context.parentURL}`)
}
// local project file
if (path && isPathNotInNodeModules(path)) {
debug('resolved: typescript', specifier, path)
const url = new URL(join('file://', path))
return addShortCircuitSignal({
...context,
url: url.href,
format:
path.endsWith('cjs') || path.endsWith('cts') || moduleType === 'commonjs' || !moduleType
? 'commonjs'
: moduleType === 'module'
? 'module'
: 'commonjs',
})
}
try {
// files could not resolved by typescript or resolved as dts, fallback to use node resolver
const res = await nextResolve(specifier)
debug('resolved: fallback node', specifier, res.url, res.format)
return addShortCircuitSignal(res)
} catch (resolveError) {
// fallback to cjs resolve as may import non-esm files
try {
const resolution = pathToFileURL(createRequire(process.cwd()).resolve(specifier)).toString()
debug('resolved: fallback commonjs', specifier, resolution)
return addShortCircuitSignal({
format: 'commonjs',
url: resolution,
})
} catch (error) {
debug('resolved by cjs error', specifier, error)
throw resolveError
}
}
}
const tsconfigForSWCNode = {
...tsconfig,
paths: undefined,
baseUrl: undefined,
}
export const load: LoadHook = async (url, context, nextLoad) => {
debug('load', url, JSON.stringify(context))
if (url.startsWith('data:')) {
debug('skip load: data url', url)
return nextLoad(url, context)
}
if (url.includes('/node_modules/')) {
debug('skip load: node_modules', url)
return nextLoad(url, context)
}
if (['builtin', 'json', 'wasm'].includes(context.format)) {
debug('loaded: internal format', url)
return nextLoad(url, context)
}
const { source, format: resolvedFormat } = await nextLoad(url, context)
if (!source) {
debug('No source', url, resolvedFormat)
return {
source,
format: resolvedFormat,
}
}
debug('loaded', url, resolvedFormat)
const code = !source || typeof source === 'string' ? source : Buffer.from(source).toString()
// url may be essentially an arbitrary string, but fixing the binding module, which currently
// expects a real file path, to correctly interpret this doesn't have an obvious solution,
// and would likely be a breaking change anyway. Do a best effort to give a real path
// like it expects, which at least fixes relative input sourcemap paths.
const filename = url.startsWith('file:') ? fileURLToPath(url) : url
const compiled = await compile(code, filename, tsconfigForSWCNode, true)
debug('compiled', url, resolvedFormat)
return addShortCircuitSignal({
// for lazy: ts-node think format would undefined, actually it should not, keep it as original temporarily
format: resolvedFormat,
source: compiled,
})
}
function isPathNotInNodeModules(path: string) {
return (
(process.platform !== 'win32' && !path.includes('/node_modules/')) ||
(process.platform === 'win32' && !path.includes('\\node_modules\\'))
)
}