Add support for processors over http
This commit is contained in:
parent
b5136c5d02
commit
3682a7a763
7 changed files with 52 additions and 12 deletions
40
src/preload/http-plugin.js
Normal file
40
src/preload/http-plugin.js
Normal file
|
@ -0,0 +1,40 @@
|
|||
const rx_any = /./;
|
||||
const rx_http = /^https?:\/\//;
|
||||
const rx_relative_path = /^\.\.?\//;
|
||||
const rx_absolute_path = /^\//;
|
||||
|
||||
async function load_http_module(href) {
|
||||
return fetch(href).then((response) =>
|
||||
response
|
||||
.text()
|
||||
.then((text) =>
|
||||
response.ok
|
||||
? { contents: text, loader: "js" }
|
||||
: Promise.reject(
|
||||
new Error(`Failed to load module '${href}'': ${text}`),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Bun.plugin({
|
||||
name: "http_imports",
|
||||
setup(build) {
|
||||
build.onResolve({ filter: rx_relative_path }, (args) => {
|
||||
if (rx_http.test(args.importer)) {
|
||||
return { path: new URL(args.path, args.importer).href };
|
||||
}
|
||||
});
|
||||
build.onResolve({ filter: rx_absolute_path }, (args) => {
|
||||
if (rx_http.test(args.importer)) {
|
||||
return { path: new URL(args.path, args.importer).href };
|
||||
}
|
||||
});
|
||||
build.onLoad({ filter: rx_any, namespace: "http" }, (args) =>
|
||||
load_http_module(`http:${args.path}`),
|
||||
);
|
||||
build.onLoad({ filter: rx_any, namespace: "https" }, (args) =>
|
||||
load_http_module(`https:${args.path}`),
|
||||
);
|
||||
},
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue