import { build } from "esbuild"; import fs from "node:fs"; import path from "node:path"; import url from "node:url"; import zlib from "node:zlib"; const here = path.dirname(url.fileURLToPath(import.meta.url)); const root = path.resolve(here, ".."); const entry = path.join(root, "src", "showcases", "twentyoz-core.js"); const outDir = path.join(root, "public", "work", "twentyoz-corp", "assets"); const outputFile = path.join(outDir, "core.iife.js"); const check = process.argv.includes("--check"); const result = await build({ absWorkingDir: root, entryPoints: [entry], outfile: outputFile, bundle: true, platform: "browser", format: "iife", globalName: "TwentyozCore", target: ["es2020"], minify: true, treeShaking: true, legalComments: "eof", sourcemap: false, write: false, }); const output = result.outputFiles?.find((item) => path.resolve(item.path) === path.resolve(outputFile)); if (!output) throw new Error("twentyoz core bundle output not found"); const generated = Buffer.from(output.contents); const gzipBytes = zlib.gzipSync(generated, { level: 9 }).byteLength; const maxGzipBytes = 200 * 1024; if (gzipBytes > maxGzipBytes) { throw new Error(`twentyoz WebGL bundle ${gzipBytes}B gzip exceeds ${maxGzipBytes}B budget`); } if (check) { if (!fs.existsSync(outputFile)) throw new Error("twentyoz core bundle is missing; run npm run build:showcase-assets"); const current = fs.readFileSync(outputFile); if (!current.equals(generated)) throw new Error("twentyoz core bundle is stale; run npm run build:showcase-assets"); } else { fs.mkdirSync(outDir, { recursive: true }); fs.writeFileSync(outputFile, generated); } console.log(`twentyoz core ${generated.byteLength}B raw · ${gzipBytes}B gzip · budget ${maxGzipBytes}B`);