Workbench / The TiffinBox Build
S30 · V2·30
What Ships, and Can We Rebuild It?
Volume 2, chapter 30. The frontend has grown to three views, a service worker, a generated contract and two TypeScript versions, built by a script that only one person ran. This stage makes the build a thing you can check. The Angular package becomes a workspace with one lockfile (npm keeps its TypeScript 6 nested and hoists the rest, which let the root import @angular/core without declaring it, now an architecture rule). One script builds web/ and writes a manifest of every shipped file's hash; two clean builds in different directories, time zones, locales and umasks are bit-for-bit identical, once the script stopped leaving out the two hand-written files in web/. A bundle report shows react-dom is 85% of the React view; a lockfile check lists the six packages that run code at install. And the contract gets optional fields, because an order from an older log had been breaking the whole kitchen display.
Stage 30: what ships, and can we rebuild it?
The brief
What you'll build.
- a workspace: the root
package.jsonlistsweb-src/angularinworkspaces; onepackage-lock.json; the Angular package's own lockfile is gone; web-tests/build-web.sh(npm run build): the only wayweb/is built, ending inbuild/web-manifest.json(each file's SHA-256, size and gzip size, and the tool versions from the lockfile);check-js.shruns it;web-tests/repro.sh: two clean builds fromnpm ciin different directories, time zones, locales and umasks; their manifests must match each other and the committed one;web-tests/bundles.mjs: the React bundle's composition by package (rebuilt with a metafile and proved byte-identical to the shipped file) and a search of every shipped script for development builds, machine paths, source map links and secret-like strings;web-tests/lockfile.mjs: every package from the registry with asha512integrity, and install scripts only from a reviewed list;- a phantom-dependency rule in
boundaries.mjs; - contract v2: optional fields (
"name?"),Order.pricing(discountRulesas a list,deliveryRuleoptional), and the old flat fields kept, optional and deprecated.
Why a real company needs this. "What exactly is running in production, and can we produce it again?" is the first question in an incident, an audit and a supply-chain scare. Without a manifest you can't say; without reproducible builds you can't tell a tampered artifact from a different build; without lockfile rules a new dependency can start running code on every developer's machine unnoticed.
Why now. Chapter 30 is build tooling: package managers and lockfiles, workspaces, module systems, bundlers, source maps, linting and monorepos. Stage 23 left the Angular view as a separate package "until stage 30", and stage 25 moved the contract changes here.
What you'll have at the end.
- one install, one build script, and proof the build is reproducible;
- reports of what's in the bundles and where every dependency comes from;
- a contract that can change without breaking older clients;
- ADR-0032: the build and the contract's change rules.
Theory to own
- Lockfiles. What does
package-lock.jsonpin thatpackage.jsondoesn't, and what doesnpm cido differently fromnpm install? (The chapter, "Lockfiles"; Companion, Explain it differently 2.) - Hoisting. Where does npm put a dependency in a workspace, and what can go wrong? (The chapter, "Workspaces"; Companion, Code walkthrough.)
- Reproducible builds. What makes two builds of one commit differ? (Companion, Gaps 2.)
- Evolving an API's shape. How do you change a field that clients read, when you can't update all clients at once? (Companion, Gaps 1.)
- What a bundle contains. How do you find out, and what should never be in it? (The chapter's lab, Experiment 1; P4, P6.)
Build it
Step 1: the workspace
Add "workspaces": ["web-src/angular"] to the root package.json, delete the Angular package's lockfile and node_modules, and run npm install once at the root. Check where each TypeScript went, and whether the build still passes.
Checkpoint. One lockfile; check-js.sh passes; each package compiles with its own TypeScript.
Step 2: one build script and its manifest
Move the build steps out of check-js.sh into build-web.sh, ending with manifest.mjs: walk web/ in sorted order, hash each file, gzip it at level 9 for its size, and take tool versions from the lockfile (not node -v, which differs between machines without changing the output). Commit build/web-manifest.json. manifest.mjs --check compares web/ with it.
Checkpoint. build/web-manifest.json: 22 files, 135.0 KB gzipped; --check passes.
Step 3: reproducible
Write repro.sh: copy the working tree (without the built .js files in web/) into two directories with different names and depths; in each, set a different TZ, LANG and umask, run npm ci and build-web.sh; compare the two manifests, then compare with the committed one.
Checkpoint. two clean builds: identical, and identical to the committed build/web-manifest.json.
Step 4: what's inside, and where it came from
Write bundles.mjs and lockfile.mjs and add both to check-js.sh. In the bundle report, rebuild the React view in memory with a metafile and refuse to report unless it's byte-identical to the shipped file.
Checkpoint. Both pass; the reports name the biggest packages and every install script.
Step 5: a contract that can change
Teach gen.mjs optional fields: a name ending in ? becomes an optional property (with exactOptionalPropertyTypes, "may be missing", never undefined), and the parser checks it only when it's present. Teach ContractTest the same. Make the four flat pricing fields optional and deprecated, and add Order.pricing. Write a test with an order from an older log.
Checkpoint. 36 server tests and 38 JS tests pass; the old order parses.
Senior and architect lens
1. A build you can't reproduce is a build you can't trust. If a bundle in production differs from what the commit builds to, either the build is non-deterministic or something changed it. With repro.sh passing, a difference means the second. Reproducibility is cheap to keep and expensive to recover.
2. ADR-0032: the build and the contract. Write it with these points:
- Build: one script (
npm run build), fromnpm ci; outputs recorded inbuild/web-manifest.json;repro.shbefore every release; nothing hand-edited in outputs. - Dependencies: one lockfile for the workspace; registry-only,
sha512; install scripts reviewed; every import declared by its own package; bundle composition reviewed when a dependency is added. - Contract: optional fields are explicit; changes are expand-then-contract; a deprecated field has a removal release; the server test and the generated parsers read the same file.
3. Your own code is the small part. 9% of the React view is TiffinBox. Choices about dependencies (React vs the plain view's 8 KB) matter more than micro-optimising your own modules.
4. Workspaces trade isolation for convenience. One install and one lockfile, at the cost of hoisting surprises; pnpm's strict layout or a rule like boundaries.mjs gets the isolation back.
What breaks first, and when it's addressed:
| Pressure | What breaks | Where it's addressed |
|---|---|---|
sources and outputs share web/ |
stale outputs, scripts that must know which is which | stage 31 (separate output directory, hashed names) |
| assets keep their names across deploys | caches can't tell versions apart; deprecated fields can't be removed safely | stage 31 |
| no source maps | production errors point at minified code | stage 32 (source maps uploaded, not published) |
| repro runs by hand | a non-deterministic change could ship | stage 33 (in the release check) |
| more packages (a shared UI kit) | relative imports across package roots | Volume 3 (@tiffinbox/contract package if a second client appears) |
Evidence
- One
package-lock.json;check-js.shpasses (38 JS tests, bundles and lockfile checks). repro.shreports two identical clean builds, identical to the committed manifest.- The phantom import is refused by
boundaries.mjs. - 36 server tests pass, including the older-log order; the suite passes (39).
- ADR-0032 is committed.
Carry forward
build/web-manifest.jsonis stage 31's input for hashed asset names and the service worker's precache list.bundles.mjsgains a source-map rule in stage 32 (maps built, uploaded, never served).repro.shjoins the release check in stage 33.- Optional fields and
$deprecatedare how every later contract change is made.