/** * Per-package prebuilt bundles. `bun run build` → dist/: * * dist/.js packages//index.ts(x) through the release pipeline * (scripts/release.ts): "pebble" is bundled IN — a prebuilt * component needs nothing else; polyfill-style lazy dynamic * imports stay split chunks. Bun bundles and minifies, then * scripts/minify.ts mangles internal property names, which * Bun cannot do. Source maps are re-chained onto the output. * dist/.d.ts tsc declarations (tsconfig.build.json), re-exported from * dist/types//. * dist/.css when packages//.css exists: the raw sheet for * zero-JS pages, minified by Bun's CSS engine. * * (The `bun build` CLI ignores plugins, hence a script.) An empty packages/ * is a valid empty dist/. */ import { brotliCompressSync, gzipSync } from "node:zlib"; import { rmSync } from "node:fs"; import { shrinkWithMap } from "./minify"; import { ROOT, discoverPackages, writeEntryShim } from "./packages"; import { bundleRelease } from "./release"; const DIST = `${ROOT}/dist`; rmSync(DIST, { recursive: true, force: true }); rmSync(`${ROOT}/.build`, { recursive: true, force: true }); const packages = await discoverPackages(); if (packages.length === 0) { await Bun.write(`${DIST}/.gitkeep`, ""); console.log("dist/: no packages yet — empty dist"); process.exit(0); } for (const pkg of packages) { const entry = await writeEntryShim(pkg); const result = await bundleRelease([entry], DIST, "linked", `${pkg.name}.js`); for (const out of result.outputs) { let bytes: Uint8Array; if (out.path.endsWith(".js")) { const mapPath = `${out.path}.map`; const mapFile = Bun.file(mapPath); const previous = (await mapFile.exists()) ? await mapFile.text() : undefined; const { code, map } = await shrinkWithMap(await out.text(), previous, `${out.path.split("/").pop()}.map`); await Bun.write(out.path, code); if (map) await Bun.write(mapPath, map); bytes = new TextEncoder().encode(code); } else { bytes = new Uint8Array(await out.arrayBuffer()); } const rel = out.path.replace(`${ROOT}/`, ""); console.log(`${pkg.name}\t${rel}\t${bytes.byteLength} B\t${gzipSync(bytes).byteLength} B gz\t${brotliCompressSync(bytes).byteLength} B br`); } // The raw sheet for zero-JS pages, minified by the same engine as the // component-CSS pipeline (`bun build --minify`) but written as .css. if (pkg.css) { const proc = Bun.spawn(["bun", "build", pkg.css, "--minify", "--target=browser"], { stdout: "pipe", stderr: "pipe" }); const [css, err, code] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text(), proc.exited]); if (code !== 0) throw new Error(`CSS compile failed for ${pkg.css}:\n${err.trim()}`); await Bun.write(`${DIST}/${pkg.name}.css`, `${css.trim()}\n`); console.log(`${pkg.name}\tdist/${pkg.name}.css\t${new TextEncoder().encode(css.trim()).byteLength} B`); } } // Type declarations, one tsc run for all packages (tsconfig.build.json): // dist/types//**, re-exported from dist/.d.ts so a consumer // importing dist/.js picks up its types. const tsc = Bun.spawnSync(["bunx", "tsc", "-p", "tsconfig.build.json"], { cwd: ROOT }); if (tsc.exitCode !== 0) { process.stdout.write(tsc.stdout); process.stderr.write(tsc.stderr); throw new Error("tsc declaration emit failed"); } for (const pkg of packages) { await Bun.write(`${DIST}/${pkg.name}.d.ts`, `export * from "./types/${pkg.name}/index";\n`); } console.log(`dist/: ${packages.map(p => `${p.name}.js`).join(", ")} + declarations`);