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] --external [react react-dom]
copy

SYNOPSIS

bun build [entrypoint...] [options]
Example: bun build src/index.ts --outfile dist/bundle.js --minify
Note: bun-build is invoked as a subcommand of the bun CLI.

PARAMETERS

[entrypoint...]
    One or more input files to start the bundling process.

--compile
    (Experimental) Compiles the project into a standalone executable.

--external
    Marks a module as external, preventing it from being bundled.

--minify
    Enables minification of the output bundle, reducing its size.

--outfile
    Specifies the path for the single output bundle file.

--outdir
    Specifies the directory for the output bundles and assets.

--target
    Sets the target runtime environment (e.g., browser, node, bun).

--format
    Defines the output module format (e.g., esm, cjs, iife).

--sourcemap
    Generates source maps to aid in debugging bundled code.

--watch
    Watches for file changes and automatically rebuilds the bundle.

-d, --development
    Builds for development, typically without minification.

-p, --production
    Builds for production, enabling optimizations like minification.

--define
    Replaces global identifiers with specified values during bundling.

--splitting
    Enables code splitting to generate multiple output chunks for better loading performance.

--public-path
    Sets the base public path for all assets in the output.

--asset-prefix
    Prepends a prefix to all asset URLs.

--jsx-factory
    Specifies a custom function for JSX element creation.

--jsx-fragment
    Specifies a custom function for JSX fragment creation.

DESCRIPTION

bun-build is the lightning-fast, built-in JavaScript and TypeScript bundler, minifier, and transpiler provided by the Bun runtime. Designed for speed, it leverages the high-performance Zig language, offering significantly faster build times compared to traditional bundlers like Webpack or Rollup. It supports a wide array of features including ES modules, CommonJS, JSX, TSX, CSS, and various asset types. Developers can use bun-build to bundle web applications, Node.js projects, or Bun-specific projects into optimized, production-ready bundles or development-friendly outputs with features like source maps and tree-shaking. Its API and CLI are designed to be intuitive and compatible with modern web development workflows, making it a powerful tool for modern JavaScript and TypeScript development.

CAVEATS

bun-build is a relatively new and rapidly evolving tool. While highly performant, its feature set and configuration options may differ from more mature bundlers like Webpack. Some advanced bundling scenarios or niche plugins might not yet be fully supported. Compatibility with all existing build tool configurations might require adaptation.

INTEGRATED WORKFLOW

bun-build is part of the larger Bun ecosystem, seamlessly integrating with Bun's package manager (bun install) and test runner (bun test). This creates a unified and highly performant developer workflow for JavaScript and TypeScript projects, from dependency management to building and testing.

PERFORMANCE ADVANTAGE

Built in Zig, bun-build leverages low-level optimizations to achieve significantly faster build times compared to other bundlers. This performance boost is particularly noticeable in large projects and CI/CD pipelines, where build duration can heavily impact development cycles.

HISTORY

Bun was initially announced in 2021 by Jarred Sumner, with a focus on delivering an all-in-one JavaScript runtime, bundler, and package manager. bun-build emerged as a core component of this vision, designed from the ground up to offer unparalleled build speeds by being written in Zig. It aims to streamline the developer experience by integrating bundling capabilities directly into the runtime, reducing the need for separate build tool installations and configurations, thus simplifying modern JavaScript project setups. Its development has focused on performance and API compatibility with existing standards.

Copied to clipboard