Bundler
Build deployable artifacts for browsers, Bun, or Node.js
Last updated on
Decide whether you need a bundle
- Server-side TypeScript under Bun: direct execution may be enough.
- Browser code: bundle, split, and process assets.
- Single-file CLI or service: bundling simplifies distribution, subject to dynamic assets and native dependencies.
- Framework application: keep the framework's official build command unless it explicitly supports Bun's bundler.
CLI
bun build ./src/index.ts --outdir ./dist --target bun
bun build ./src/browser.tsx --outdir ./dist --target browser --splitting
bun build ./src/cli.ts --outfile ./dist/cli.js --target node --minify --sourcemapJavaScript API
const result = await Bun.build({
entrypoints: ['./src/index.ts'],
outdir: './dist',
target: 'bun',
minify: true,
sourcemap: 'linked',
});
if (!result.success) {
for (const log of result.logs) console.error(log);
process.exit(1);
}Target matrix
| Runtime | target | Constraint |
|---|---|---|
| Browser | browser | Never expose server environment variables or secrets |
| Bun | bun | Bun-specific APIs may remain |
| Node.js | node | Source must not require Bun.* at runtime |
Mark packages as external when they should stay outside the bundle, then ensure the deployment environment provides them. Native binaries and dynamic runtime loading need explicit verification.
Single-file executables
Compile a CLI, agent tool, or small fixed-platform service into a standalone executable:
bun build src/index.ts \
--compile \
--target=bun-linux-x64 \
--outfile=dist/serviceCross-compilation is not target-platform validation. Start and test dynamic libraries, CA certificates, timezone data, subprocesses, external files, and native dependencies on the target OS and CPU. The project still owns signing, SBOMs, and release verification.
bun build ./src/index.ts --outdir ./dist --target bun --sourcemap
bun ./dist/index.jsRun the output on the same OS, architecture, and Bun version as production. A successful build is not a runtime test.
Official references: Bundler and single-file executables.