esbuild-wasm-bundler/serve.tsx

30 lines
948 B
TypeScript
Raw Normal View History

2024-06-07 16:54:12 -04:00
import bundler from "./mod.ts";
import Introspect, {Root} from "./introspect.tsx";
const Config = await Introspect();
console.log(Config.imports);
const transpiled:Map<string, string> = new Map();
Deno.serve(async(req)=>{
const url = new URL(req.url);
const index = url.pathname.lastIndexOf(".");
if(index > -1)
{
const ext = url.pathname.substring(index+1, index+3);
if(ext === "ts")
{
const results = await bundler({imports:Config.imports}, {
entryPoints:[Root+url.pathname],
outfile:"bundle.js",
entryNames: `[dir][name]`,
});
results.outputFiles?.forEach(output=>{
transpiled.set(output.path, output.text);
})
return new Response(results.outputFiles[0].text, {headers:{"content-type":"application/javascript"}});
}
}
return new Response(Root+url.pathname);
});