Skip to main content
Bun includes a fast native bundler that can be used via the bun build CLI command or the Bun.build() JavaScript API. It handles TypeScript, JSX, CSS, JSON, and more out of the box without any configuration.

Why bundle?

Fewer HTTP requests

Combine dozens of modules and dependencies into a small number of self-contained bundles that load with a single request.

Code transforms

TypeScript, JSX, CSS modules, and other formats are all converted to plain JavaScript and CSS without extra tooling.

Tree shaking

Dead code is automatically eliminated. Only the exports that are actually used end up in the bundle.

Full-stack builds

Bundle both server and client code in a single command, including HTML entry points with embedded assets.

Basic example

Given these two files:
Build them with:
Bun writes the bundled output to ./out/index.js, with TypeScript and JSX both transpiled automatically — no tsconfig.json or Babel config required.

Targets

The --target flag controls module resolution and available globals.

Output formats

Bun defaults to ESM ("esm"), with experimental support for CommonJS ("cjs").

Key flags

TypeScript and JSX

TypeScript and JSX are supported natively — no plugins, no configuration.
Bun reads compilerOptions.jsx from your tsconfig.json to determine how to compile JSX. You can also configure it directly in the Bun.build() API:
Bun does not perform type checking. Use tsc --noEmit for type checking.

Code splitting

When multiple entrypoints share dependencies, use --splitting to extract them into shared chunks, reducing duplication.
Shared code is written to a content-hashed chunk file:

Tree shaking

Tree shaking is automatic. The bundler performs dead code elimination on all builds — unused exports and unreachable branches are removed without any configuration.
Use /*@__PURE__*/ annotations to mark side-effect-free function calls for elimination when their result is unused:

Sourcemaps

Environment variables

Use the env option to control how process.env.* references are handled in the bundle.

JavaScript API: Bun.build()

The Bun.build() API returns a Promise<BuildOutput>:
Each item in result.outputs is a BuildArtifact — a Blob with extra properties: If outdir is omitted, files are not written to disk. You can consume them as Blob objects or pass them directly to new Response().

Watch mode

Rebuild automatically when source files change:

esbuild compatibility

Bun’s bundler API is modeled on esbuild. Most esbuild options map directly to Bun equivalents. Key differences:
  • Bun always bundles by default (--bundle is not needed)
  • --platform is renamed to --target
  • --outbase is renamed to --root
  • --asset-names is renamed to --asset-naming
  • No built-in dev server (use Bun.serve() instead)
  • Bun is 1.75x faster than esbuild on the three.js benchmark
Most esbuild flags map directly — see the esbuild documentation for a full flag reference.