-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathesm_loader.mjs
More file actions
32 lines (27 loc) · 942 Bytes
/
esm_loader.mjs
File metadata and controls
32 lines (27 loc) · 942 Bytes
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
import { createRequire } from 'node:module'
import { delimiter } from 'node:path'
import { pathToFileURL } from 'node:url'
const extraNodePaths = (process.env.NODE_PATH || '')
.split(delimiter)
.filter(Boolean)
export async function resolve(specifier, context, defaultResolve) {
try {
return await defaultResolve(specifier, context, defaultResolve)
} catch (originalError) {
if (specifier.startsWith('.') || specifier.startsWith('/') || specifier.match(/^node:/)) {
// Don't handle relative, absolute, or node: specifiers
throw originalError
}
for (const basePath of extraNodePaths) {
const require = createRequire(pathToFileURL(basePath).href)
try {
const resolved = require.resolve(specifier)
return { url: pathToFileURL(resolved).href }
} catch {
// Skip and try next
}
}
// Re-throw original error if not found
throw originalError
}
}