-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (48 loc) · 1.73 KB
/
index.js
File metadata and controls
55 lines (48 loc) · 1.73 KB
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
const rx_any = /./;
const rx_http = /^https?:\/\//;
const rx_relative_path = /^\.\.?\//;
const rx_absolute_path = /^\//;
function get_loader_from_url(href) {
if (href.endsWith(".ts") || href.endsWith(".mts")) return "ts";
if (href.endsWith(".tsx")) return "tsx";
if (href.endsWith(".js") || href.endsWith(".mjs")) return "js";
if (href.endsWith(".jsx")) return "jsx";
return "js";
}
function load_http_module(href) {
return fetch(href).then(function (response) {
return response.text().then(function (text) {
if (!response.ok) {
return Promise.reject(
new Error("Failed to load module '" + href + "': " + text)
);
}
const loader = get_loader_from_url(href);
return { contents: text, loader };
});
});
}
Bun.plugin({
name: "http_imports",
setup(build) {
build.onResolve({ filter: rx_relative_path }, function (args) {
if (rx_http.test(args.importer)) {
return { path: new URL(args.path, args.importer).href };
}
});
build.onResolve({ filter: rx_absolute_path }, function (args) {
if (rx_http.test(args.importer)) {
return { path: new URL(args.path, args.importer).href };
}
});
build.onResolve({ filter: rx_http }, function (args) {
return { path: args.path };
});
build.onLoad({ filter: rx_any, namespace: "http" }, function (args) {
return load_http_module("http:" + args.path);
});
build.onLoad({ filter: rx_any, namespace: "https" }, function (args) {
return load_http_module("https:" + args.path);
});
},
});