-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.mjs
37 lines (28 loc) · 1019 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
// Manually register new loaders with `registerLoader`.
import { _transform, _resolvePath } from "./alternative-loader/register-loaders.mjs";
const _resolved = new Map();
export async function load(URLString, context, nextLoad)
{
if (URLString.startsWith("node:")) {
return nextLoad(URLString);
}
const { headers, body } = _resolved.get(URLString);
const { format, source } = await _transform(URLString, { headers, body });
return { format, source, shortCircuit: true };
}
export async function resolve(specifier, context, nextResolve) {
if (specifier.startsWith("node:")) {
return nextResolve(specifier);
}
const [finalURL, result] = await _resolvePath(specifier, context);
if (!finalURL) {
throw new Error(`No loader found for ${specifier}.`);
}
// You can't share custom context between resolve and load.
_resolved.set(finalURL, result);
return {
format: "module",
shortCircuit: true,
url: finalURL
};
}