startup-budget.ts
This file
Path
scripts/startup-budget.ts
154 lines · 4.5 KiB · TypeScript
Last changed by
Merge redesign into mainoscabrielbefore adoptioncaedd21
History of this fileTake it
The walk stopped after 200 commits: lines older than 10c8b54 point at it.
Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
line 1why, one step back1/**Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
lines 2–3why, one step back2 * The limits are regression alarms, not Cloudflare's upload limit. Each Worker3 * has its own, so one Worker's growth never hides in another's headroom.Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
line 4why, one step back4 *Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
lines 5–7why, one step back5 * A dynamic `import()` does not shrink the entry: Wrangler's esbuild build has6 * no code splitting and inlines it, so moving a dependency off the entry takes7 * a module of its own (`find_additional_modules`) or a Worker of its own.Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
line 8why, one step back8 *Merge build/comments-ops into mainoscabrielbefore adoptione4c407e
lines 9–10why, one step back9 * The web's JavaScript counts the Worker's server modules only: chunks only10 * the browser loads are assets.Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 11–21why, one step back11 */12import { BunRuntime, BunServices } from "@effect/platform-bun";13import { Console, Data, Effect, FileSystem, Path, pipe } from "effect";14import { ChildProcess, ChildProcessSpawner } from "effect/process";1516interface Budget {17 readonly entry: number;18 readonly javascript: number;19}2021export const budgets = {Close the wave: openings are heard as landings, and the code store is written downoscabrielbefore adoption2587726
line 22why, one step back22 "apps/platform": { entry: 1_900_000, javascript: 1_900_000 },Close the code browser and drafting wave: its decisions in two ADRs, every rule it added held, and its leftovers folded inoscabrielbefore adoption009640c
line 23why, one step back23 "apps/sandbox": { entry: 656_000, javascript: 656_000 },Merge redesign into mainoscabrielbefore adoptioncaedd21
line 24why, one step back24 "apps/web": { entry: 355_000, javascript: 3_191_000 },Merge branch 'build/906-platform-root-page'oscabrielbefore adoption10c8b54
lines 25–154why, one step back25} as const satisfies Record<string, Budget>;2627export const workers: readonly Worker[] = [28 { dir: "apps/platform", entry: "worker.js" },29 { dir: "apps/sandbox", entry: "worker.js" },30 { build: ["vite", "build"], dir: "apps/web", entry: "index.js" },31];3233interface Worker {34 readonly dir: keyof typeof budgets;35 readonly entry: string;36 readonly build?: readonly [string, ...string[]];37}3839class BuildFailed extends Data.TaggedError("BuildFailed")<{40 readonly worker: string;41 readonly exitCode: number;42}> {}4344class OverBudget extends Data.TaggedError("OverBudget")<{45 readonly worker: string;46 readonly scope: "entry" | "javascript";47 readonly bytes: number;48 readonly budget: number;49}> {}5051const env = { WRANGLER_LOG: "error", WRANGLER_SEND_METRICS: "false" };5253const runBuild = (54 dir: string,55 [command, ...args]: readonly [string, ...string[]]56) =>57 Effect.gen(function* () {58 const path = yield* Path.Path;59 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;6061 const exitCode = yield* spawner.exitCode(62 ChildProcess.make(path.resolve(dir, "node_modules/.bin", command), args, {63 cwd: dir,64 // A production build, whatever the shell says: the development65 // condition would measure the web's dev backend instead.66 env: { ...env, NODE_ENV: "production" },67 extendEnv: true,68 stderr: "inherit",69 stdout: "ignore",70 })71 );7273 return exitCode === 074 ? undefined75 : yield* new BuildFailed({ exitCode, worker: dir });76 });7778const measure = Effect.fnUntraced(function* (worker: Worker) {79 const fs = yield* FileSystem.FileSystem;80 const path = yield* Path.Path;81 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;82 const outdir = yield* fs.makeTempDirectoryScoped();8384 if (worker.build !== undefined) {85 yield* runBuild(worker.dir, worker.build);86 }8788 const exitCode = yield* spawner.exitCode(89 ChildProcess.make(90 path.resolve(worker.dir, "node_modules/.bin/wrangler"),91 ["deploy", "--dry-run", "--outdir", outdir],92 {93 cwd: worker.dir,94 env,95 extendEnv: true,96 stderr: "inherit",97 stdout: "ignore",98 }99 )100 );101102 if (exitCode !== 0) {103 return yield* new BuildFailed({ exitCode, worker: worker.dir });104 }105106 const javascript = (yield* fs.readDirectory(outdir, { recursive: true }))107 .filter((file) => file.endsWith(".js"))108 .toSorted();109110 const sizes = yield* pipe(111 javascript,112 Effect.forEach((file) =>113 fs114 .readFile(path.join(outdir, file))115 .pipe(Effect.map((bytes) => ({ bytes: bytes.length, file })))116 )117 );118119 const entry = sizes.find(({ file }) => file === worker.entry)?.bytes ?? 0;120 const total = sizes.reduce((sum, { bytes }) => sum + bytes, 0);121 const budget: Budget = budgets[worker.dir];122123 yield* Console.log(124 `bytes:check: ${worker.dir}: entry ${entry} of ${budget.entry}; JavaScript ${total} of ${budget.javascript}`125 );126127 if (entry > budget.entry) {128 return yield* new OverBudget({129 budget: budget.entry,130 bytes: entry,131 scope: "entry",132 worker: worker.dir,133 });134 }135136 if (total > budget.javascript) {137 return yield* new OverBudget({138 budget: budget.javascript,139 bytes: total,140 scope: "javascript",141 worker: worker.dir,142 });143 }144145 return { entry, total };146}, Effect.scoped);147148if (import.meta.main) {149 BunRuntime.runMain(150 Effect.forEach(workers, measure, { discard: true }).pipe(151 Effect.provide(BunServices.layer)152 )153 );154}