/** * Bench UI build — mirrors build.ts but outputs a standalone web app into * ../bench/ui-dist (served by the bench FastAPI server). * * bun run build.bench.ts # dev build * bun run build.bench.ts --release # minified */ import { $ } from "bun"; import path from "path"; import fs from "fs"; import os from "os"; const isRelease = Bun.argv.includes("--release"); const rootDir = path.resolve(import.meta.dir, ".."); const outdir = path.join(rootDir, "bench", "ui-dist"); const project_name = path.basename(rootDir); fs.mkdirSync(outdir, { recursive: true }); function scopeTailwindThemeVariables(css: string, widgetClass: string): string { return css.replace(/:root\s*,\s*:host\s*\{/g, `.${widgetClass}{`); } async function buildCSS(): Promise { const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "moxie-bench-css-")); const cssOut = path.join(tempDir, "globals.css"); const minifyFlag = isRelease ? "--minify" : ""; try { await $`./node_modules/.bin/tailwindcss -i ./src/styles/globals.css -o ${cssOut} ${minifyFlag}`; const widgetClass = project_name.toLowerCase(); const css = fs.readFileSync(cssOut, "utf-8"); return scopeTailwindThemeVariables( css.replaceAll("__CUSTOM_NODE_CLASS__", widgetClass), widgetClass, ); } finally { fs.rmSync(tempDir, { recursive: true, force: true }); } } async function build() { const start = Date.now(); const globalCss = await buildCSS(); const result = await Bun.build({ entrypoints: ["./src/bench/main.tsx"], outdir, naming: { chunk: "chunks/[name]-[hash].[ext]", entry: "[name].[ext]", asset: "assets/[name].[ext]", }, splitting: true, target: "browser", minify: isRelease, define: { "process.env.NODE_ENV": isRelease ? '"production"' : '"development"', "import.meta.env.PROJECT_NAME": `"${project_name}"`, "__COMFY_EASY_MEDIA_GLOBAL_CSS__": JSON.stringify(globalCss), }, css: false, }); if (!result.success) { for (const log of result.logs) { console.error(log); } throw new Error("bench UI build failed"); } // index.html referencing the built entry const entryName = result.outputs .map((o) => o.path) .filter((p) => p.endsWith("main.js") || p.endsWith("main.js.map")) .map((p) => path.basename(p)) .find((p) => p.endsWith(".js")); const html = ` Moxie Multimedia Bench
`; fs.writeFileSync(path.join(outdir, "index.html"), html); console.log( `[build:bench${isRelease ? ":release" : ""}] Done in ${Date.now() - start}ms (${result.outputs.length} outputs) -> ${outdir}`, ); } await build();