esbuild-wasm-bundler/serve.tsx

135 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-06-07 16:54:12 -04:00
import bundler from "./mod.ts";
2024-06-12 14:48:53 -04:00
import mime from "https://esm.sh/mime@4.0.3";
import { parseArgs } from "jsr:@std/cli/parse-args";
2024-06-07 16:54:12 -04:00
2024-06-10 22:51:23 -04:00
2024-06-12 14:48:53 -04:00
if(Deno.mainModule == import.meta.url)
{
serve(parseArgs(Deno.args) as ServeArgs);
}
2024-06-10 22:51:23 -04:00
2024-06-12 14:48:53 -04:00
type ServeArgs = {path?:string, html?:string, port?:string|number};
2024-06-12 15:51:50 -04:00
function serve(settings:ServeArgs):void
2024-06-12 14:48:53 -04:00
{
let basePath = "";
const SetDirectory =(inPath:string)=>{
if(inPath.startsWith("http://")||inPath.startsWith("https://")||inPath.startsWith("file://"))
{
basePath = inPath;
}
else
{
basePath = new URL(inPath, new URL(`file://${Deno.cwd().replaceAll("\\", "/")}/`).toString()).href;
}
if(!basePath.endsWith("/"))
{
basePath = basePath + "/";
}
console.log("Base Path:", basePath);
}
SetDirectory(settings.path||"./");
const transpiled:Map<string, string> = new Map();
const ServeJSCode =(inText:string)=> new Response(inText, {headers:{"content-type":"application/javascript"}});
const ServeStatic =async(inPath:string)=>
2024-06-07 16:54:12 -04:00
{
2024-06-12 14:48:53 -04:00
try
{
const handle = await fetch(basePath+inPath.substring(1));
return new Response(handle.body, {headers:{"content-type": mime.getType(inPath)??""}})
}
catch(_e)
{
return resp404;
}
};
const resp404 = new Response("404", {status:404});
let respIndex:false|Response|Promise<Response> = false;
if(settings.html)
{
if(settings.html.indexOf("<") != -1)
{
respIndex= new Response(settings.html, {headers:{"content-type":"text-html"}});
}
else
{
respIndex = ServeStatic(settings.html);
}
}
Deno.serve({port:parseInt(settings.port as string)||8000}, async(req)=>{
const url = new URL(req.url);
const index = url.pathname.lastIndexOf(".");
if(index > -1)
2024-06-10 16:23:31 -04:00
{
2024-06-12 14:48:53 -04:00
const ext = url.pathname.substring(index+1);
if(ext === "ts" || ext == "tsx" || ext == "js" || ext == "jsx")
2024-06-10 22:51:23 -04:00
{
2024-06-12 14:48:53 -04:00
if(ext == "js")
2024-06-10 22:51:23 -04:00
{
2024-06-12 14:48:53 -04:00
const lookup = transpiled.get(url.pathname);
if(lookup)
{
return ServeJSCode(lookup);
}
2024-06-10 22:51:23 -04:00
}
2024-06-12 14:48:53 -04:00
else
{
const lookup = transpiled.get(url.pathname.substring(0, index)+".js");
if(lookup)
{
return ServeJSCode(lookup);
}
}
try
2024-06-10 22:51:23 -04:00
{
2024-06-12 14:48:53 -04:00
const results = await bundler({
entryPoints:[basePath+url.pathname],
outdir:"/",
entryNames: `[dir][name]`,
splitting: true
});
if(results.outputFiles)
{
results.outputFiles.forEach(output=>transpiled.set(output.path, output.text))
return ServeJSCode(results.outputFiles[0].text);
}
else
{
throw(new Error("no output"));
}
}
catch(_e)
{
return resp404;
2024-06-10 22:51:23 -04:00
}
}
2024-06-12 14:48:53 -04:00
else
2024-06-10 22:51:23 -04:00
{
2024-06-12 14:48:53 -04:00
return ServeStatic(url.pathname);
2024-06-10 22:51:23 -04:00
}
2024-06-12 14:48:53 -04:00
}
if(respIndex)
{
return respIndex;
}
else
{
let indexLookup = url.pathname;
if(!indexLookup.endsWith("/"))
2024-06-10 22:51:23 -04:00
{
2024-06-12 14:48:53 -04:00
indexLookup = indexLookup+"/";
2024-06-10 22:51:23 -04:00
}
2024-06-12 14:48:53 -04:00
return ServeStatic(indexLookup+"index.html");
2024-06-10 16:23:31 -04:00
}
2024-06-12 14:48:53 -04:00
});
}
2024-06-10 16:23:31 -04:00
2024-06-12 14:48:53 -04:00
export default serve