This page documents the programmatic JavaScript APIs for Bun's transpiler and bundler. These APIs allow JavaScript code to perform code transformation, bundling, and module analysis operations. For runtime module loading and resolution, see Module System and Resolution. For CLI bundling, see Build Command and Compilation.
Bun exposes two primary APIs for code transformation:
Bun.build() - A comprehensive bundler API that processes multiple entry points, performs tree-shaking, code-splitting, and outputs optimized bundlesBun.Transpiler - A lower-level transpiler API for transforming individual files without bundlingBoth APIs share the same underlying parser and printer infrastructure but differ in scope and capabilities.
Sources: src/bun.js/api/JSBundler.zig1-264 packages/bun-types/bun.d.ts1274-1743
Figure 1: Bun.build() execution pipeline
Sources: src/bun.js/api/JSBundler.zig266-698 src/bundler/bundle_v2.zig107-157
The Bun.build() function accepts a BuildConfig object with the following structure:
Figure 2: BuildConfig structure
Key configuration parsing occurs in parseConfig():
| Option | Type | Parsed In | Description |
|---|---|---|---|
entrypoints | string[] | JSBundler.zig294-366 | Entry points for bundling |
target | 'browser'|'node'|'bun' | JSBundler.zig368-389 | Runtime target environment |
outdir | string | JSBundler.zig391-411 | Output directory path |
format | 'esm'|'cjs'|'iife' | JSBundler.zig413-440 | Module format |
splitting | boolean | JSBundler.zig442-453 | Enable code splitting |
plugins | BunPlugin[] | JSBundler.zig455-474 | Custom plugins |
minify | boolean|object | JSBundler.zig476-517 | Minification options |
sourcemap | string | JSBundler.zig519-540 | Source map mode |
external | string[] | JSBundler.zig542-563 | External modules |
Sources: src/bun.js/api/JSBundler.zig266-698 packages/bun-types/bun.d.ts1274-1554
The Bun.build() function returns a Promise<BuildOutput>:
The output construction occurs in OutputFileListBuilder:
.map files linked to code outputsSources: src/bundler/bundle_v2.zig534-855 packages/bun-types/bun.d.ts1395-1452
The files option enables bundling without filesystem access:
Figure 3: FileMap resolution flow
The FileMap provides virtual filesystem capabilities:
Sources: src/bun.js/api/JSBundler.zig8-164 src/bundler/bundle_v2.zig121-124
The Bun.Transpiler class provides lower-level file transformation:
Figure 4: Transpiler API flow
The transpiler is initialized as a lazy property on the Bun global object and configures the underlying parser/printer without bundler-specific features like tree-shaking across modules.
Sources: packages/bun-types/bun.d.ts1556-1743 src/bun.js/api/BunObject.zig70
| Method | Async | Returns | Description |
|---|---|---|---|
transform(code, loader?) | Yes | Promise<string> | Transform code asynchronously |
transformSync(code, loader?) | No | string | Transform code synchronously |
scan(code) | No | ScanResult | Extract imports/exports without transformation |
scanImports(code) | No | ScannedImport[] | Extract only imports |
The ScanResult interface provides module metadata:
Sources: packages/bun-types/bun.d.ts1622-1743
Both Bun.build() and Bun.Transpiler support plugins with lifecycle hooks:
Figure 5: Plugin execution model
The plugin system uses two hook types defined in BunPlugin:
onResolve Hook:
onLoad Hook:
Sources: packages/bun-types/bun.d.ts743-983 src/bun.js/api/JSBundler.zig700-1107
Plugin hooks are executed in the bundler thread:
plugins array in configsetup() function is called with a PluginBuilder instanceonResolve and onLoad callbacks are stored with their filter patternsonResolve hooks execute in registration orderonLoad hooks executeThe plugin system integrates with the bundler at two key points:
Sources: src/bun.js/api/JSBundler.zig700-1562
Figure 6: Full implementation stack
| Component | File | Responsibility |
|---|---|---|
JSBundler | JSBundler.zig1-264 | JavaScript API entry point, config parsing |
BundleV2 | bundle_v2.zig107-533 | Main bundler orchestration |
Graph | bundle_v2.zig857-1394 | Dependency graph construction |
Transpiler | options.zig1-900 | Configuration and options |
js_parser | js_parser.zig1-1000 | Parse source to AST |
js_printer | js_printer.zig1-1000 | Generate code from AST |
LinkerContext | LinkerContext.zig1-500 | Tree-shaking and linking |
Plugin | JSBundler.zig700-1562 | Plugin hook execution |
Sources: src/bun.js/api/JSBundler.zig1-264 src/bundler/bundle_v2.zig107-533 src/js_parser.zig1-1000 src/js_printer.zig1-1000
The bundler uses multiple threads for performance:
Communication between threads uses:
JSBundleCompletionTask - Delivers results back to JS threadPollableRef - Allows JS thread to wait for bundler completionEventLoop.enqueueTaskConcurrent()Sources: src/bundler/bundle_v2.zig464-482 src/bun.js/api/JSBundler.zig1726-1879
When compile: true is set, the bundler produces standalone executables:
Figure 7: Compile mode process
The StandaloneModuleGraph format stores:
/$bunfs/ prefix)At runtime, the compiled executable bypasses normal file I/O and uses the embedded module graph directly.
Sources: src/StandaloneModuleGraph.zig1-100 src/cli/build_command.zig100-200
Refresh this wiki