diff options
Diffstat (limited to 'cgithub/scripts/compile-views.ts')
| -rw-r--r-- | cgithub/scripts/compile-views.ts | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/cgithub/scripts/compile-views.ts b/cgithub/scripts/compile-views.ts new file mode 100644 index 0000000..785efc1 --- /dev/null +++ b/cgithub/scripts/compile-views.ts @@ -0,0 +1,29 @@ +// Precompiles views/*.eta into plain functions for the Cloudflare Workers +// build (src/views.generated.ts). Workers disallow runtime code generation +// (`new Function`), so Eta cannot compile templates there at request time. +import { Eta } from "eta"; +import fs from "node:fs"; +import path from "node:path"; + +const root = path.join(import.meta.dirname, ".."); +const viewsDir = path.join(root, "views"); +const eta = new Eta({ views: viewsDir }); + +const names = fs + .readdirSync(viewsDir) + .filter((name) => name.endsWith(".eta")) + .sort(); + +let out = `// @ts-nocheck +// Generated by scripts/compile-views.ts -- do not edit. +export const views = { +`; +for (const name of names) { + const source = fs.readFileSync(path.join(viewsDir, name), "utf8"); + const body = eta.compileToString(source); + out += ` ${JSON.stringify(name)}: function (it, options) {\n${body}\n },\n`; +} +out += "};\n"; + +fs.writeFileSync(path.join(root, "src", "views.generated.ts"), out); +console.log(`Compiled ${names.length} templates to src/views.generated.ts`); |
