LinuxCommandLibrary

bun-build

Build JavaScript/TypeScript applications

TLDR

Bundle an entry point to a single output file

$ bun build [path/to/entry.ts] --outfile [path/to/output.js]
copy

Bundle multiple entry points to an output directory
$ bun build [path/to/entry1.ts path/to/entry2.ts ...] --outdir [path/to/dist]
copy

Bundle with source maps for debugging
$ bun build [path/to/entry.ts] --outfile [path/to/output.js] --sourcemap
copy

Bundle with minification for production
$ bun build [path/to/entry.ts] --outfile [path/to/output.js] --minify
copy

Bundle with a specific target environment
$ bun build [path/to/entry.ts] --outfile [path/to/output.js] --target [browser|bun|node]
copy

Bundle to a standalone executable
$ bun build [path/to/entry.ts] --compile --outfile [path/to/executable]
copy

Watch for file changes and rebuild automatically
$ bun build [path/to/entry.ts] --outfile [path/to/output.js] --watch
copy

Bundle with external dependencies not included in the output
$ bun build [path/to/entry.ts] --outfile [path/to/output.js] [[-e|--external]] [react react-dom]
copy

SYNOPSIS

bun build <entry-point...> [--outdir <dir>] [--outfile <file>] [--target <bun|node|browser>] [--format <esm|cjs|iife>] [options]

PARAMETERS

--outdir, -d <dir>
    Output directory for bundled files (default: current dir)

--outfile <file>
    Single output file path (disables splitting)

--target <bun|node|browser>
    Compilation target (default: bun)

--platform <browser|node|bun>
    Platform assumptions (affects globals like process)

--format <esm|cjs|iife>
    Output format (default: esm for most targets)

--splitting
    Enable code splitting (ESM only)

--external <name>...
    Mark modules as external (e.g., React)

--minify
    Minify output with toplevel dead code elimination

--sourcemap
    Generate inline sourcemaps

--metafile
    Emit JSON metafile for build analysis

--public-path <path>
    Base path for dynamic imports

--loader <ext>:<loader>
    Custom loaders (e.g., .png:file)

--define <name>=<value>
    Global constant replacements

--banner <esm|require>:<code>
    Prepend code to outputs

--jsx-factory <name>
    JSX factory function (default: React.createElement)

--jsx-fragment <name>
    JSX fragment (default: React.Fragment)

--log-level <error|warn|info|debug>
    Verbosity level

--help, -h
    Show help

DESCRIPTION

bun build is a core command in Bun, the all-in-one JavaScript runtime, bundler, and package manager designed for speed. It bundles JavaScript, TypeScript, JSX, and CSS files into optimized output formats, outperforming tools like esbuild, webpack, and Rollup by up to 10-100x in benchmarks.

Key features include native TypeScript/JSX support without config, code splitting, tree shaking, minification, sourcemaps, and metafile generation for analysis. It targets platforms like bun, node, or browser, producing ESM, CJS, or IIFE bundles. External modules (e.g., node_modules) can be excluded, and loaders handle assets like images or JSON.

Usage is simple: point to entry files, specify outputs, and run. Ideal for web apps, libraries, or server-side code. Bun's Zig-based engine enables blazing builds, often completing in milliseconds. It's cross-platform, including Linux, and integrates seamlessly with bun install for dependencies.

While production-ready for many use cases, some advanced webpack features are absent, focusing on core bundling efficiency.

CAVEATS

Requires Bun installed (curl -fsSL https://bun.sh/install | bash). Not a standard apt/yum package. Some Node.js bundler features (e.g., advanced plugins) unsupported. Watch mode via bun --watch build. Outputs are deterministic but verify compatibility for browser polyfills.

EXAMPLE

bun build ./src/index.tsx --outdir ./dist --minify --sourcemap --target=bun
bun build ./app.js --outfile ./bundle.js --format=iife --external:react

PERFORMANCE

Builds 20MB+ apps in <100ms on modern Linux hardware; uses 4-5x less memory than alternatives.

HISTORY

Developed by Jarred Sumner as part of Bun (initial release Sep 2022, v0.1.0). Reached 1.0 in 2023 with stable bundler. Focuses on JavaScriptCore speed; now v1.1+ with Linux arm64/x64 support, surpassing esbuild in benchmarks.

SEE ALSO

bun(1), esbuild(1)

Copied to clipboard