-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathloader.js
127 lines (107 loc) · 3.29 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
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
import { readAndMergeConfig } from "./lifecycles/config.js";
import { initContext } from "./lifecycles/context.js";
import { mergeResponse } from "./lib/resource-utils.js";
const config = await readAndMergeConfig();
const context = await initContext({ config });
const resourcePlugins = config.plugins
.filter((plugin) => plugin.type === "resource")
.filter(
(plugin) =>
plugin.name !== "plugin-node-modules:resource" && plugin.name !== "plugin-user-workspace",
)
.map((plugin) =>
plugin.provider({
context,
config,
graph: [],
}),
);
async function getCustomLoaderResponse(initUrl, checkOnly = false) {
const headers = {
Accept: "text/javascript",
};
const initResponse = new Response("");
let request = new Request(initUrl, { headers });
let url = initUrl;
let response = initResponse.clone();
let shouldHandle = false;
for (const plugin of resourcePlugins) {
if (
initUrl.protocol === "file:" &&
plugin.shouldResolve &&
(await plugin.shouldResolve(initUrl, request))
) {
shouldHandle = true;
if (!checkOnly) {
url = new URL((await plugin.resolve(initUrl, request)).url);
}
}
}
for (const plugin of resourcePlugins) {
if (plugin.shouldServe && (await plugin.shouldServe(initUrl, request))) {
shouldHandle = true;
if (!checkOnly) {
response = mergeResponse(response, await plugin.serve(initUrl, request));
}
}
}
for (const plugin of resourcePlugins) {
if (
plugin.shouldPreIntercept &&
(await plugin.shouldPreIntercept(url, request, response.clone()))
) {
shouldHandle = true;
if (!checkOnly) {
response = mergeResponse(
response,
await plugin.preIntercept(url, request, response.clone()),
);
}
}
if (plugin.shouldIntercept && (await plugin.shouldIntercept(url, request, response.clone()))) {
shouldHandle = true;
if (!checkOnly) {
response = mergeResponse(response, await plugin.intercept(url, request, response.clone()));
}
}
}
return {
shouldHandle,
response,
};
}
// https://nodejs.org/docs/latest-v18.x/api/esm.html#resolvespecifier-context-nextresolve
export async function resolve(specifier, context, defaultResolve) {
const { parentURL } = context;
const url = specifier.startsWith("file://")
? new URL(specifier)
: specifier.startsWith(".")
? new URL(specifier, parentURL)
: undefined;
if (url) {
const { shouldHandle } = await getCustomLoaderResponse(url, true);
if (shouldHandle) {
return {
url: url.href,
shortCircuit: true,
};
}
}
return defaultResolve(specifier, context, defaultResolve);
}
// https://nodejs.org/docs/latest-v18.x/api/esm.html#loadurl-context-nextload
export async function load(source, context, defaultLoad) {
const extension = source.split(".").pop();
const url = new URL(source);
const { shouldHandle } = await getCustomLoaderResponse(url, true);
if (shouldHandle && extension !== "js") {
const { response } = await getCustomLoaderResponse(url);
const contents = await response.text();
return {
format: "module",
source: contents,
shortCircuit: true,
};
}
return defaultLoad(source, context, defaultLoad);
}