From e28fe9dc7b05bd63d8e530383b8a38e2538c1b9a Mon Sep 17 00:00:00 2001 From: imbytecat Date: Sat, 25 Apr 2026 14:05:01 +0800 Subject: [PATCH] =?UTF-8?q?perf(compile):=20=E5=90=AF=E7=94=A8=20bytecode?= =?UTF-8?q?=20+=20minify=20+=20inline=20sourcemap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Bun 官方 bytecode caching:中型应用 startup ~2x(docs.bun.sh/docs/bundler/bytecode) - minify:减小 bytecode 体积,二进制仅 +2MB sourcemap - sourcemap inline:嵌入二进制,保证错误堆栈可读,并在 compile.ts 清理 bundler 残留的 *.js.map --- AGENTS.md | 8 ++++++++ compile.ts | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/AGENTS.md b/AGENTS.md index 97f642e..b54c1e3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -67,6 +67,14 @@ Add a subcommand: drop a file in `src/cli/` that default-exports `defineCommand( **Deploy flow is always migrate-then-serve.** The compiled binary bundles neither the `./drizzle/` SQL files nor the app schema migrations at runtime — they're read from disk next to the binary. Dockerfile copies `drizzle/` alongside `./server`, and `compose.yaml` models this with a one-shot `migrate` service that `app` `depends_on: service_completed_successfully`. On k8s, run `./server migrate` as an initContainer or a Helm `pre-upgrade` Job; run `./server` (= `./server serve`) as the main container. +## Compile flags + +`compile.ts` builds with `--minify --bytecode --sourcemap=inline`: + +- **`bytecode`** — pre-compiles JS to bytecode and embeds it in the binary; ~2x startup on app-sized binaries (Bun docs benchmark). Requires `--compile`; top-level `await` must live inside `async` functions (it already does in this repo). +- **`minify`** — shrinks the binary and the bytecode it derives from. +- **`sourcemap: 'inline'`** — embeds the source map in the binary so error stack traces stay decodable. Bun also writes a residual `out/bin.js.map` next to the output; `compile.ts` removes it so the binary is the only artifact. + ## ORPC Contract → Router → Handler → Client, all type-safe from a single contract. diff --git a/compile.ts b/compile.ts index 0adc2d1..301bf65 100644 --- a/compile.ts +++ b/compile.ts @@ -49,12 +49,18 @@ const main = async () => { entrypoints: [ENTRYPOINT], outdir: OUTDIR, compile: { outfile, target }, + minify: true, + bytecode: true, + sourcemap: 'inline', }) if (!result.success) { throw new Error(result.logs.map(String).join('\n')) } + // Bun bundler still writes *.js.map next to the binary even with inline sourcemap. + await rm(`${OUTDIR}/${ENTRYPOINT.replace(/\.ts$/, '')}.js.map`, { force: true }) + console.log(`✓ ${target} → ${OUTDIR}/${outfile}`) }