// Fails if the largest built JS entry chunk exceeds the gzipped budget.
import { readdirSync, readFileSync } from "node:fs";
import { gzipSync } from "node:zlib";
import { join } from "node:path";

const BUDGET_KB = 250;
const dir = "dist/assets";
const jsFiles = readdirSync(dir).filter((f) => f.endsWith(".js"));
if (jsFiles.length === 0) {
  console.error(`no JS files found in ${dir} — was the build skipped?`);
  process.exit(1);
}
let largest = 0;
let largestName = "";
for (const file of jsFiles) {
  const gz = gzipSync(readFileSync(join(dir, file))).length;
  if (gz > largest) { largest = gz; largestName = file; }
}
const kb = (largest / 1024).toFixed(1);
console.log(`largest JS chunk: ${largestName} = ${kb} KB gz (budget ${BUDGET_KB} KB)`);
if (largest > BUDGET_KB * 1024) {
  console.error(`bundle-size budget exceeded: ${kb} KB > ${BUDGET_KB} KB`);
  process.exit(1);
}
