-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.js
47 lines (38 loc) · 1.47 KB
/
loader.js
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
import { isBuiltin } from 'node:module'
import { dirname } from 'node:path'
import { promisify } from 'node:util'
import { fileURLToPath, pathToFileURL } from 'node:url'
import resolveCallback from 'resolve'
import { resolve as resolveTs, load } from 'ts-node/esm'
import { loadConfig, createMatchPath } from 'tsconfig-paths'
const resolveAsync = promisify(resolveCallback)
const tsExtensions = new Set(['.tsx', '.ts', '.mts', '.cts'])
const { absoluteBaseUrl, paths } = loadConfig()
const matchPath = createMatchPath(absoluteBaseUrl, paths)
async function resolve(specifier, ctx, defaultResolve) {
const { parentURL = pathToFileURL(absoluteBaseUrl) } = ctx
if (isBuiltin(specifier)) {
return defaultResolve(specifier, ctx)
}
if (specifier.startsWith('file://')) {
specifier = fileURLToPath(specifier)
}
let url
try {
const resolution = await resolveAsync(matchPath(specifier) || specifier, {
basedir: dirname(fileURLToPath(parentURL)),
// For whatever reason, --experimental-specifier-resolution=node doesn't search for .mjs extensions
// but it does search for index.mjs files within directories
extensions: ['.js', '.json', '.node', '.mjs', ...tsExtensions],
})
url = pathToFileURL(resolution).href
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
// Match Node's error code
error.code = 'ERR_MODULE_NOT_FOUND'
}
throw error
}
return resolveTs(url, ctx, defaultResolve)
}
export { resolve, load }