LinuxCommandLibrary

bun-build

Bundle JavaScript and TypeScript for production

TLDR

Bundle JavaScript/TypeScript file

$ bun build [./src/index.ts] --outdir [./dist]
copy
Create standalone executable
$ bun build [./server.ts] --compile --outfile [server]
copy
Bundle with minification
$ bun build [./src/index.ts] --outdir [./dist] --minify
copy
Generate source maps
$ bun build [./src/index.ts] --outdir [./dist] --sourcemap
copy
Bundle for Node.js target
$ bun build [./src/index.ts] --outdir [./dist] --target [node]
copy
Mark packages as external
$ bun build [./src/index.ts] --outdir [./dist] --external [react]
copy
Bundle multiple entrypoints
$ bun build [./src/index.ts] [./src/worker.ts] --outdir [./dist]
copy

SYNOPSIS

bun build entrypoints [options]

DESCRIPTION

bun build is Bun's fast native bundler for JavaScript and TypeScript. It bundles code for production, generates standalone executables, and supports both server and client code in a single command.
The bundler automatically performs tree-shaking to eliminate unused code. It can target multiple runtimes including browsers, Node.js, and Bun itself. The --compile flag creates self-contained executables with the Bun runtime embedded, enabling distribution without requiring users to install Bun. Code splitting, source maps, and minification are supported through command-line flags or the equivalent JavaScript API via Bun.build().

PARAMETERS

--outdir directory

Output directory for bundled files
--outfile file
Output file path (for single output)
--compile
Create standalone executable with embedded Bun runtime
--minify
Enable minification
--sourcemap
Generate source maps (external, inline, or none)
--target runtime
Target runtime: browser, bun, or node
--external package
Exclude package from bundle
--splitting
Enable code splitting
--format type
Output format: esm or cjs
--define key=value
Define global constants
--loader ext:loader
Configure file type loaders

JAVASCRIPT API

$ await Bun.build({
  entrypoints: ['./src/index.js'],
  outdir: './build',
  minify: true,
})
copy

CAVEATS

Standalone executables include a copy of the Bun runtime, increasing file size. Tree-shaking is always enabled. Some npm packages may not bundle correctly due to dynamic imports.

SEE ALSO

bun(1), bun-run(1), esbuild(1), webpack(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community