用途:Bun Bundler 用于打包 JavaScript/TypeScript 项目,基于 Bun 运行时同一引擎,是 esbuild 兼容的替代方案,具备原生性能。
主要能力:支持多入口打包、代码分割(splitting)、tree shaking、压缩(minify,可细分 whitespace/identifiers/syntax)、source map(none/inline/external/linked)、编译期常量 define、外部依赖 external、自定义 loader 与插件。目标环境可选 browser(默认)、bun、node;输出格式可选 esm、cjs、iife。内置 loader 覆盖 js/jsx/ts/tsx/json/toml/css,以及 text、file、base64、dataurl。
使用方式:CLI 示例:bun build ./src/index.ts --outdir ./dist;生产构建加 --minify。也可用 JavaScript API:await Bun.build({ entrypoints: ["./src/index.ts"], outdir: "./dist" })。注意自 Bun 1.2 起 Bun.build 失败时会 reject(抛出异常),需用 try/catch 处理;传入 { throw: false } 可恢复旧的 resolve 返回 { success, logs } 行为。构建结果可通过 result.outputs 访问,含 path、kind、hash、loader,并可 await output.text() 读取内容。
适用场景:浏览器应用生产构建、Bun/Node 服务端打包、库构建(external: ["*"] 外部化全部依赖)、多入口与共享依赖抽取、构建脚本(bun run build.ts)。
限制:资料未提供收费、价格、平台支持范围与性能基准等细节;常见错误包括 Could not resolve(缺少导入或路径错误)、No matching export(命名导出缺失)、Unexpected token(语法错误)、Target not supported(目标无效)。