Files
oh-my-pi/packages/coding-agent/scripts/generate-template.ts
T
2026-01-03 14:53:38 +01:00

33 lines
983 B
TypeScript

#!/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/core/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
const template = html
.replace("<template-css/>", `<style>${minifiedCss}</style>`)
.replace("<template-js/>", `<script>${js}</script>`);
// 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");