oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/build-bundles.mjs — run by a person, output committed.
2// Docs' script, adopted (oficina/docs/fixtures/build-bundles.mjs).
3// The app performs no build step (spec §3.1: "one esbuild bundle
4// committed hashed"); this produces the artifact that makes that true:
5// a single, minified, content-hashed JS bundle committed to
6// internal/slides/static/, embedded into the Go binary by render.go's
7// //go:embed and resolved at init by internal/slides/assets_embed.go.
8//
9// internal/slides/static/, not a top-level static/: Go's //go:embed
10// cannot reach outside the directory of the .go file doing the
11// embedding, so the bundle lives beside the stylesheets that tree
12// already holds.
13//
14// Paths are resolved from this file's own location, so
15// `node build-bundles.mjs` works the same from fixtures/ or anywhere.
16// The gate runs it twice and requires a clean tree: the output is a
17// pure function of the sources and the pinned toolchain.
18import { build } from 'esbuild'
19import { createHash } from 'node:crypto'
20import { readFileSync, writeFileSync, rmSync, readdirSync, mkdirSync } from 'node:fs'
21import { join, dirname } from 'node:path'
22import { fileURLToPath } from 'node:url'
23
24const here = dirname(fileURLToPath(import.meta.url))
25const staticDir = join(here, '..', 'internal', 'slides', 'static')
26
27mkdirSync(staticDir, { recursive: true })
28
29// The names here are the names assets_embed.go's bundleKinds expects;
30// that file panics at init if one of them has no committed file, so a
31// build that silently stopped emitting one cannot start.
32const bundles = [
33 { name: 'editor', entry: 'editor-entry.js' },
34 // The font gate: every slide surface, before the editor
35 // (fonts-gate.mjs, internal/slides/fontgate.go).
36 { name: 'fontsgate', entry: 'fonts-gate.mjs' },
37 // The hold: a classic, blocking script in <head>, so an IIFE.
38 { name: 'fontshold', entry: 'fonts-hold.js', format: 'iife' },
39 // The presenter window and the slide window, moving at once
40 // (presenter-entry.mjs, internal/slides/presenter_window.go).
41 { name: 'presenter', entry: 'presenter-entry.mjs' }
42]
43
44for (const { name, entry, format = 'esm' } of bundles) {
45 const outfile = join(staticDir, `${name}.js`)
46 await build({
47 entryPoints: [join(here, entry)],
48 bundle: true,
49 format,
50 minify: true,
51 target: 'es2020',
52 outfile
53 })
54
55 // Content hash in the filename so the asset is immutable-cacheable
56 // and a stale browser cache cannot serve a bundle that disagrees
57 // with the server.
58 const bytes = readFileSync(outfile)
59 const hash = createHash('sha256').update(bytes).digest('hex').slice(0, 16)
60
61 // Exactly one file per bundle must ever be committed — assets_embed.go
62 // panics at init if it finds more or less than one — so a rebuild
63 // removes every previous hashed file for THIS name before writing
64 // the new one. Anchored to the name so a second bundle, if one ever
65 // arrives, is never deleted by the first's rebuild.
66 const stale = new RegExp(`^${name}\\.[0-9a-f]{16}\\.js$`)
67 for (const f of readdirSync(staticDir)) {
68 if (stale.test(f)) rmSync(join(staticDir, f))
69 }
70 writeFileSync(join(staticDir, `${name}.${hash}.js`), bytes)
71 rmSync(outfile)
72
73 console.log(`wrote internal/slides/static/${name}.${hash}.js (${bytes.length} bytes)`)
74}
75