// build-bundles.mjs — run by make editor, output committed. Builds each // bundle in bundles.mjs, minified, into internal/mapa/static/ under a // content hash, so a cached copy can never disagree with the server, and // removes that bundle's earlier files: exactly one file per bundle is // ever committed. The build is byte-reproducible (no sourcemaps, banners, // timestamps or absolute paths; esbuild is pinned), so make editor can // fail on any drift from what is committed. Paths are resolved from this // file, not the caller's directory. Docs' fixtures/build-bundles.mjs is // the model. import { build } from 'esbuild' import { createHash } from 'node:crypto' import { gzipSync } from 'node:zlib' import { readFileSync, writeFileSync, rmSync, readdirSync, mkdirSync } from 'node:fs' import { join, dirname } from 'node:path' import { fileURLToPath } from 'node:url' import { bundles, options, budget } from './bundles.mjs' const here = dirname(fileURLToPath(import.meta.url)) const staticDir = join(here, '..', 'internal', 'mapa', 'static') mkdirSync(staticDir, { recursive: true }) let over = false for (const { name, entry, format = 'esm' } of bundles) { const outfile = join(staticDir, `${name}.js`) await build({ ...options, entryPoints: [join(here, entry)], format, outfile }) const bytes = readFileSync(outfile) rmSync(outfile) const hash = createHash('sha256').update(bytes).digest('hex').slice(0, 16) const stale = new RegExp(`^${name}\\.[0-9a-f]{16}\\.js$`) for (const file of readdirSync(staticDir)) { if (stale.test(file)) rmSync(join(staticDir, file)) } writeFileSync(join(staticDir, `${name}.${hash}.js`), bytes) const gzipped = gzipSync(bytes, { level: 9 }).length console.log(`wrote internal/mapa/static/${name}.${hash}.js ` + `(${bytes.length} bytes, ${(gzipped / 1024).toFixed(1)}KB gzipped; ` + `L12 budget ${budget / 1024}KB)`) if (gzipped > budget) { console.error(`${name}: ${gzipped} bytes gzipped is over L12's ${budget}`) over = true } } if (over) process.exit(1)