oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1// build-bundles.mjs — run by make editor, output committed. Builds each
2// bundle in bundles.mjs, minified, into internal/mapa/static/ under a
3// content hash, so a cached copy can never disagree with the server, and
4// removes that bundle's earlier files: exactly one file per bundle is
5// ever committed. The build is byte-reproducible (no sourcemaps, banners,
6// timestamps or absolute paths; esbuild is pinned), so make editor can
7// fail on any drift from what is committed. Paths are resolved from this
8// file, not the caller's directory. Docs' fixtures/build-bundles.mjs is
9// the model.
10import { build } from 'esbuild'
11import { createHash } from 'node:crypto'
12import { gzipSync } from 'node:zlib'
13import {
14 readFileSync, writeFileSync, rmSync, readdirSync, mkdirSync
15} from 'node:fs'
16import { join, dirname } from 'node:path'
17import { fileURLToPath } from 'node:url'
18import { bundles, options, budget } from './bundles.mjs'
19
20const here = dirname(fileURLToPath(import.meta.url))
21const staticDir = join(here, '..', 'internal', 'mapa', 'static')
22mkdirSync(staticDir, { recursive: true })
23
24let over = false
25for (const { name, entry, format = 'esm' } of bundles) {
26 const outfile = join(staticDir, `${name}.js`)
27 await build({ ...options, entryPoints: [join(here, entry)], format, outfile })
28 const bytes = readFileSync(outfile)
29 rmSync(outfile)
30 const hash = createHash('sha256').update(bytes).digest('hex').slice(0, 16)
31 const stale = new RegExp(`^${name}\\.[0-9a-f]{16}\\.js$`)
32 for (const file of readdirSync(staticDir)) {
33 if (stale.test(file)) rmSync(join(staticDir, file))
34 }
35 writeFileSync(join(staticDir, `${name}.${hash}.js`), bytes)
36 const gzipped = gzipSync(bytes, { level: 9 }).length
37 console.log(`wrote internal/mapa/static/${name}.${hash}.js ` +
38 `(${bytes.length} bytes, ${(gzipped / 1024).toFixed(1)}KB gzipped; ` +
39 `L12 budget ${budget / 1024}KB)`)
40 if (gzipped > budget) {
41 console.error(`${name}: ${gzipped} bytes gzipped is over L12's ${budget}`)
42 over = true
43 }
44}
45if (over) process.exit(1)
46