#!/usr/bin/env bun
/**
* Pre-compute the HTML template at publish time.
* This avoids the Bun macro restriction in node_modules.
*/
const dir = new URL("../src/export/html/", import.meta.url).pathname;
// Read all files
const html = await Bun.file(`${dir}template.html`).text();
const css = await Bun.file(`${dir}template.css`).text();
const js = await Bun.file(`${dir}template.js`).text();
// Minify CSS
const minifiedCss = css
.replace(/\/\*[\s\S]*?\*\//g, "")
.replace(/\s+/g, " ")
.replace(/\s*([{}:;,])\s*/g, "$1")
.trim();
// Inline everything; use function replacements so `$'`, `$&`, `$$`, etc. inside
// the embedded CSS/JS are not interpreted as substitution patterns.
const template = html
.replace("", () => ``)
.replace("", () => ``);
// Write generated file
const output = `// Auto-generated by scripts/generate-template.ts - DO NOT EDIT
export const TEMPLATE = ${JSON.stringify(template)};
`;
await Bun.write(`${dir}template.generated.ts`, output);
console.log("Generated template.generated.ts");