网页
综合
更新时间 2026-11-16 06:55:45
Bundler | Bun Docs
自动蜘蛛
bun.sh
正文约 5149 字
Bun's fast native bundler for JavaScript, TypeScript, JSX, and more
Use Bun's native bundler through the bun build CLI command or the Bun.build() JavaScript API.
It's fast. The following numbers are from esbuild's three.js benchmark.
Bundlers solve several problems:
bun build checks types only when you pass --check, or check: true to Bun.build. It does not generate type declarations. Use tsc to produce .d.ts files.
Build your first bundle. You have the following two files, which implement a client-side rendered React app.
Here, index.tsx is the "entrypoint" to the application: the file the bundler starts from. Commonly, this is a script that performs some side effect, like starting a server or, in this case, initializing a React root. Because these files use TypeScript and JSX, the code must be bundled before it can be sent to the browser.
To create the bundle:
For each file specified in entrypoints, Bun generates a new bundle and writes it to the ./out directory (as resolved from the current working directory). After running the build, the file system looks like this:
The contents of out/index.js look something like this:
Like the runtime and test runner, the bundler supports watch mode natively.
Like the Bun runtime, the bundler supports a range of file types by default. The following table lists the bundler's standard "loaders". See loaders.
If the bundler encounters an import with an unrecognized extension, it treats the imported file as an external file. The bundler copies the referenced file as-is into outdir and resolves the import as a path to the file.
The exact behavior of the file loader also depends on naming and publicPath.
Plugins can override or extend the behavior described in this table. See loaders.
An array of paths corresponding to the entrypoints of your application. Bun generates one bundle per entrypoint.
A map of file paths to their contents for in-memory bundling: bundle virtual files that don't exist on disk, or override the contents of files that do. This option is only available in the JavaScript API.
You can provide file contents as a string, Blob, TypedArray, or ArrayBuffer.
You can bundle code without any files on disk by providing all sources in files:
When all entrypoints are in the files map, Bun uses the current working directory as the root.
In-memory files take priority over files on disk, so you can override specific files while keeping the rest of your codebase unchanged:
Real files on disk can import virtual files, and virtual files can import real files:
Use this for code generation, injecting build-time constants, or testing with mock modules.
The directory where output files are written.
If you don't pass outdir to the JavaScript API, Bun does not write bundled code to disk. It returns the bundled files in an array of BuildArtifact objects. These objects are Blobs with extra properties; see Outputs.
When outdir is set, the path property on a BuildArtifact is the absolute path it was written to.
The intended execution environment for the bundle.
Depending on the target, Bun applies different module resolution rules and optimizations.
Default. For bundles that run in a browser. Prioritizes the "browser" export condition when resolving imports. Importing built-in modules like node:events or node:path works, but calling some functions, like fs.readFile, does not.
For bundles that run in the Bun runtime. In many cases, it isn't necessary to bundle server-side code; you can directly execute the source code without modification. However, bundling your server code can reduce startup times and improve running performance. Use this target for full-stack applications with build-time HTML imports, where server and client code are bundled together.
All bundles generated with target: "bun" are marked with a // @bun pragma, which tells the Bun runtime that there's no need to re-transpile the file before execution.
If any entrypoint contains a Bun shebang (#!/usr/bin/env bun), the bundler defaults to target: "bun" instead of "browser".
When you use target: "bun" and format: "cjs" together, the bundler adds the // @bun @bun-cjs pragma, and the CommonJS wrapper function is not compatible with Node.js.
For bundles that run in Node.js. Prioritizes the "node" export condition when resolving imports. Bun does not polyfill the Bun global or the built-in bun:* modules.
Specifies the module format of the generated bundles.
Bun defaults to "esm", and provides experimental support for "cjs" and "iife".
The default format. Supports ES Module syntax, including top-level await and import.meta.
To use ES Module syntax in browsers, set format to "esm" and load the bundle with a <script type="module"> tag.
To build a CommonJS module, set format to "cjs". When you choose "cjs", the default target changes from "browser" (esm) to "node" (cjs). CommonJS modules transpiled with format: "cjs", target: "node" run in both Bun and Node.js (assuming both support the APIs in use).
To build an IIFE bundle, set format to "iife". Bun wraps the bundle in an immediately invoked function expression and does not support exposing its exports under a global name.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。