Skip to content

Commit 131ad07

Browse files
authored
perf(build): minify dist-experimental esbuild bundle (closes #117) (#119)
## Summary - Enable `minify: true` + `keepNames: true` in the esbuild config that produces `dist-experimental/index.js`. - `dist-experimental/index.js`: **1395K → 683K (–60%)**. - `dist-experimental/` total: **2.7M → 1.79M**, comfortably under the 3M cap. - Loosen the `patchHostDefault()` regex so it survives identifier mangling — both legacy `dist/` (`env=env`) and the new minified `dist-experimental/` (`env=Ue`) get patched correctly. ## Why Issue #117 (`area:tarball`): the experimental bundle ships unminified, paying a ~700K size penalty in every `npx @forgeplan/web init --experimental` install. PRD-014 SC-1 already pins a ≤3M cap on `dist/`; minifying brings the experimental shape closer to a future graduation target with headroom for upgrades. Notes on choices: - `keepNames: true` — preserves `Function.name` / `class.name` so sirv, polka, and SvelteKit error formatters that key off names still resolve. - `legalComments: "none"` was already set, so no license-banner regression. - The 3M cap (`DIST_EXPERIMENTAL_MAX_BYTES`) is left as-is — 1.79M actual leaves 40% headroom for legitimate growth (Svelte/d3/adapter-node bumps). ## Test plan - [x] `npm run build` succeeds, both `dist/` and `dist-experimental/` produced. - [x] `[build] patched HOST default 0.0.0.0 → 127.0.0.1 in ./dist/index.js (env=env)` — legacy still patches. - [x] `[build] patched HOST default 0.0.0.0 → 127.0.0.1 in ./dist-experimental/index.js (env=Ue)` — minified bundle gets the loopback patch with the mangled identifier captured. - [x] `npm run smoke` passes (legacy `dist/` happy path). - [x] Experimental shim smoke (manual): init `--experimental`, start server, probe endpoints — `/api/health` ok, `/api/list` ok, `GET /` returns HTML, all 200. - [x] `dirSizeBytes(DIST_EXPERIMENTAL)` = 1.88M, well below `DIST_EXPERIMENTAL_MAX_BYTES = 3M` cap. Refs: PRD-014, #117 🤖 Generated with [Claude Code](https://claude.com/claude-code)
2 parents a6fcaef + 4292f1d commit 131ad07

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

‎scripts/build.mjs‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,26 @@ function patchHostDefault(root) {
167167
const indexPath = join(root, "index.js");
168168
if (!existsSync(indexPath)) return;
169169
const src = readFileSync(indexPath, "utf8");
170-
const PATTERN = /env\(\s*['"]HOST['"]\s*,\s*['"]0\.0\.0\.0['"]\s*\)/;
171-
if (!PATTERN.test(src)) {
170+
// The unminified form is `env('HOST', '0.0.0.0')`. Issue #117 enabled
171+
// esbuild `minify: true` for dist-experimental/, which renames `env` to a
172+
// short identifier (e.g. `Ue`). Capture whatever identifier the minifier
173+
// chose and reuse it in the replacement so both shapes work.
174+
const PATTERN =
175+
/([A-Za-z_$][A-Za-z0-9_$]*)\(\s*['"]HOST['"]\s*,\s*['"]0\.0\.0\.0['"]\s*\)/;
176+
const m = src.match(PATTERN);
177+
if (!m) {
172178
log(
173179
"patchHostDefault: HOST literal not found — adapter-node may have changed; review build",
174180
);
175181
return;
176182
}
177-
writeFileSync(indexPath, src.replace(PATTERN, "env('HOST', '127.0.0.1')"));
178-
log("patched HOST default 0.0.0.0 → 127.0.0.1 in dist/index.js");
183+
writeFileSync(
184+
indexPath,
185+
src.replace(PATTERN, `${m[1]}('HOST', '127.0.0.1')`),
186+
);
187+
log(
188+
`patched HOST default 0.0.0.0 → 127.0.0.1 in ${indexPath.replace(ROOT, ".")} (env=${m[1]})`,
189+
);
179190
}
180191

181192
function pruneSymlinks(root) {
@@ -290,6 +301,15 @@ async function bundleExperimentalDist() {
290301
logLevel: "warning",
291302
legalComments: "none",
292303
treeShaking: true,
304+
// Issue #117: post-minify the bundle. The unminified ESM bundle is ~1.4M;
305+
// minifying drops it to ~500–700K and shaves the published tarball
306+
// (refs PRD-014 SC-1: dist/ ≤ 3M cap; this brings dist-experimental/
307+
// closer to that target with headroom for future deps).
308+
// `keepNames: true` preserves function/class .name so any runtime
309+
// diagnostics that rely on `Error.stack` / `Function.name` still resolve
310+
// (sirv, polka, kit error formatters).
311+
minify: true,
312+
keepNames: true,
293313
metafile: false,
294314
});
295315

0 commit comments

Comments
 (0)