esbuild-wasm-bundler/serve.tsx
2024-06-10 16:23:31 -04:00

38 lines
1.1 KiB
TypeScript

import bundler from "./mod.ts";
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);
console.log(ext);
console.log(url.pathname);
if(ext === "ts")
{
const results = await bundler({
entryPoints:["."+url.pathname],
outfile:"bundle.js",
entryNames: `[dir][name]`,
});
results.outputFiles?.forEach(output=>{
transpiled.set(output.path, output.text);
})
return new Response(`
path: ${results.outputFiles[0].path}
hash: ${results.outputFiles[0].hash}
`, {headers:{"content-type":"application/javascript"}});
}
if(ext == "js")
{
return new Response(transpiled.get(url.pathname)||"---", {headers:{"content-type":"application/javascript"}});
}
}
return new Response(url.pathname);
});