-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathloader.mjs
39 lines (29 loc) · 926 Bytes
/
loader.mjs
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
import { stat } from "node:fs/promises";
async function resolutionAttempt(specifier, context, nextResolve, ext) {
try {
const path = `${specifier}${ext}`;
await stat(new URL(path, context.parentURL));
return nextResolve(path, context);
} catch (e) {
return false;
}
}
export async function resolve(specifier, context, nextResolve) {
if (!specifier.startsWith(".") && !specifier.startsWith("/") && !specifier.startsWith("file://")) {
return nextResolve(specifier, context);
}
let loaded;
loaded = await resolutionAttempt(specifier, context, nextResolve, "");
if (loaded) {
return loaded;
}
loaded = await resolutionAttempt(specifier, context, nextResolve, ".ts");
if (loaded) {
return loaded;
}
loaded = await resolutionAttempt(specifier, context, nextResolve, ".js");
if (loaded) {
return loaded;
}
throw new Error(`Module not found: ${specifier}`);
}