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}`), ); }, });