let clientHtml: string | undefined;
async function loadClientHtml() {
const [{ default: van }, { default: App }] = await Promise.all([
import("npm:mini-van-plate@0.5.6/van-plate"),
import("https://esm.town/v/reosablo/sampleVanIsland"),
]);
const { body, h1, head, script, title } = van.tags;
return van.html(
head(
title("UUID v4 Generator"),
script({ type: "module", src: "/main.js" }),
),
body(
h1("UUID v4 Generator"),
App({ van }),
),
);
}
let clientScript: string | undefined;
async function loadClientScript() {
const [{ build }, { denoPlugins }] = await Promise.all([
import("https://deno.land/x/esbuild@v0.20.2/wasm.js"),
import("https://deno.land/x/esbuild_deno_loader@0.9.0/mod.ts"),
]);
const result = await build({
plugins: [...denoPlugins({ loader: "portable" })],
entryPoints: ["https://esm.town/v/reosablo/sampleVanIsland"],
bundle: true,
format: "esm",
minify: true,
write: false,
absWorkingDir: "/",
});
return result.outputFiles[0].text;
}
export default async function fetch(req: Request) {
try {
const { pathname } = new URL(req.url);
switch (`${req.method} ${pathname}`) {
case "GET /": {
clientHtml ??= await loadClientHtml();
return new Response(clientHtml, {
headers: { "Content-Type": "text/html" },
});
}
case "GET /main.js": {
clientScript ??= await loadClientScript();
return new Response(clientScript, {
headers: { "Content-Type": "text/javascript" },
});
}
default: {
return new Response("Not found", { status: 404 });
}
}
} catch (error) {
return new Response(error.stack, { status: 500 });
}
}