Single-executable self-hosting: compiling the Node prod server with bun build --compile (umbrella + one design question)
What I'm trying to do
Ship a vinext app as one relocatable binary for scale-to-zero hosting (Knative), where cold start is the metric that matters. The approach that needs the fewest moving parts is to compile vinext's own Node prod server:
vinext build
bun build --compile --bytecode --format=esm --minify --compile-autoload-package-json \
server-entry.mjs dist/server/index.jswhere server-entry.mjs is ~40 lines that call startProdServer({ outDir, rscEntryPath }) with
paths derived from process.execPath, and the app's server bundle is passed as a second entrypoint
so it is embedded (bytecode) rather than loaded from disk.
What works today (1.0.0-beta.12, Bun 1.4.2)
- The compile succeeds with no plugins or patches (121 vinext runtime modules; with the second
entrypoint, the app's server JS is served from the binary: deleting all 159
dist/server/*.jsfiles changes nothing on 10 probed routes). - The binary serves App and Pages routes, public files, rewrites,
/_next/data, not-found, and prerendered output, because it is the samestartProdServerpath your nightly deploy suite runs. - On our run of vinext's own
scripts/e2e-deploy.shcorpus (778 Next.js e2e cases) this shape passes 746, versus 717 for the same app compiled through the Nitro bun preset. 32 files flip to green, 3 to red (all three are the externals caveat below). startProdServeralready acceptsrscEntryPath/serverEntryPath, and$bunfspaths work for both. So no new API is needed for embedding; documenting those options as stable (#3445's second suggestion) is enough.
The small things that had to be worked around (already filed, listed here so the set is visible)
| issue | |
|---|---|
generated server.js hard-codes import.meta.dirname → binary is not relocatable |
#3445 |
generated server.js does not default NODE_ENV=production → dev React bundle at runtime |
#3444 |
output: "standalone" exits before the prerender phase |
#3442 |
standalone dep copier dedupes by name, first come → wrong react in a monorepo |
#3443 |
Node prod server ignores setImageOptimizer() (serves originals) |
#3446 |
None of them needs a design decision; each is a few lines.
The one design question: opt-in inlining of server externals (this issue)
The remaining failures come from what is NOT in the vite server bundle. vite leaves server
dependencies external (react, react-dom, @opentelemetry/*, pg, …) and resolves them from
node_modules at runtime. When Bun compiles the binary it re-bundles those externals itself, with
Bun's export conditions, not vite's:
react-version(e2e) fails becauseserverFavoringBrowser/serverFavoringEdgeresolve tonode.- Passing them as
--externalinstead breaks boot: an ESM bare import from a$bunfsmodule cannot resolve tonode_moduleson disk (Cannot find package 'react' from '/$bunfs/root/…';NODE_PATHdoes not help). That part is a Bun limitation, which I'll take to Bun separately.
Proposal: an opt-in build option (say vinext build --inline-server-externals, or a config key
under the standalone options) that makes vite bundle the server-side externals into
dist/server/* with vite's own resolution and conditions. The output would then be self-contained:
no node_modules needed at runtime, no second resolver involved, and the compiled binary carries
everything except static client assets. Off by default, so the normal vinext start path is
untouched.
Questions for you:
- Is a single-executable self-hosting shape something you'd accept in the Node prod server path at all, or should it wait for the deployment-adapter API (#80) and live as an adapter?
- If the former, is an opt-in externals-inlining build option acceptable in principle? I'm happy to do the PR; I'd rather ask before writing it since it touches the build config.
Measurements, for context (not the ask)
Local darwin arm64, noisy host, time-to-first-200 over 12 rounds: compiled prod server with the
embedded app bundle 183 ms (health) / 193 ms (SSR), vs 241 / 237 ms for the Nitro-bun binary of the
same app, vs 296 / 434 ms for bun server.js uncompiled. On a Kubernetes scale-from-zero A/B
(n=10 each) the two binaries were not separable (p = 0.16), so I'm not claiming a cold-start win
yet; the compat delta above is the solid part.
Source: cloudflare/vinext