2026-03-11 10:41:12 +01:00
|
|
|
import { readFile } from "node:fs/promises";
|
|
|
|
|
import path from "node:path";
|
|
|
|
|
|
|
|
|
|
// At runtime (dist/render.js), __dirname is packages/email/dist/
|
|
|
|
|
const PKG_ROOT = path.join(__dirname, "..");
|
|
|
|
|
const TEMPLATES_ROOT = path.join(PKG_ROOT, "templates");
|
2026-03-11 10:56:30 +01:00
|
|
|
const CSS_PATH = path.join(PKG_ROOT, "email.css");
|
|
|
|
|
|
2026-03-11 10:41:12 +01:00
|
|
|
const BASE_URL = process.env.PUBLIC_URL ?? "https://sexy.pivoine.art";
|
|
|
|
|
|
|
|
|
|
export interface RenderOptions {
|
|
|
|
|
url: string;
|
|
|
|
|
[key: string]: unknown;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 11:36:18 +01:00
|
|
|
export async function renderTemplate(name: string, locals: RenderOptions): Promise<string> {
|
2026-03-11 10:41:12 +01:00
|
|
|
// Dynamic import: @maizzle/framework v6 is ESM-only
|
|
|
|
|
const { render } = await import("@maizzle/framework");
|
|
|
|
|
|
|
|
|
|
const html = await readFile(path.join(TEMPLATES_ROOT, `${name}.html`), "utf8");
|
|
|
|
|
|
|
|
|
|
const { html: rendered } = await render(html, {
|
|
|
|
|
components: {
|
|
|
|
|
root: TEMPLATES_ROOT,
|
|
|
|
|
folders: ["layouts"],
|
|
|
|
|
},
|
2026-03-11 10:56:30 +01:00
|
|
|
// Override PostCSS `from` so @import "@maizzle/tailwindcss" resolves
|
|
|
|
|
// from this package's node_modules (defu gives our value priority).
|
2026-03-11 10:41:12 +01:00
|
|
|
postcss: {
|
|
|
|
|
options: {
|
2026-03-11 10:56:30 +01:00
|
|
|
from: CSS_PATH,
|
2026-03-11 10:41:12 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
locals: {
|
2026-03-11 11:36:18 +01:00
|
|
|
cssPath: CSS_PATH, // layout uses {{ cssPath }} in <link href="{{ cssPath }}" inline>
|
2026-03-11 10:41:12 +01:00
|
|
|
baseUrl: BASE_URL,
|
|
|
|
|
...locals,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return rendered;
|
|
|
|
|
}
|