Skip to main content
Bun’s universal plugin API extends both the runtime and the bundler. Plugins intercept imports and perform custom loading logic, such as reading files or transpiling code. They can add support for additional file types, like .scss or .yaml. In the bundler, plugins can implement framework-level features like CSS extraction, macros, and client-server code co-location.

Lifecycle hooks

Plugins register callbacks that run at various points in the lifecycle of a bundle:
  • onStart(): Run once the bundler has started a bundle
  • onResolve(): Run before a module is resolved
  • onLoad(): Run before a module is loaded
  • onBeforeParse(): Run zero-copy native addons in the parser thread before a file is parsed
  • onEnd(): Run after the bundle is complete

Reference

A rough overview of the types (see Bun’s bun.d.ts for the full type definitions):
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bbun.d.ts

Usage

A plugin is a JavaScript object with a name property and a setup function.
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bmyPlugin.ts
Pass it in the plugins array when calling Bun.build.
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bindex.ts

Plugin lifecycle

Namespaces

onLoad and onResolve accept an optional namespace string. Every module has a namespace. Namespaces prefix the import in transpiled code; for example, a loader with a filter: /\.yaml$/ and namespace: "yaml:" transforms an import from ./myfile.yaml into yaml:./myfile.yaml. The default namespace is "file" and you don’t need to specify it: import myModule from "./my-module.ts" is the same as import myModule from "file:./my-module.ts". Other common namespaces are:
  • "bun": for Bun-specific modules ("bun:test", "bun:sqlite")
  • "node": for Node.js modules ("node:fs", "node:path")

onStart

Registers a callback to be run when the bundler starts a new bundle.
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bindex.ts
The callback can return a Promise. After the bundle process has initialized, the bundler waits until all onStart() callbacks have completed before continuing. For example:
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bindex.ts
In this example, Bun waits for both onStart() callbacks to complete: the 10-second sleep and the write to bundle-time.txt.
onStart() callbacks (like every other lifecycle callback) cannot modify the build.config object. To mutate build.config, do so directly in the setup() function.

onResolve

To bundle your project, Bun walks down the dependency tree of all modules in your project. For each imported module, Bun has to find and read that module. The “finding” part is known as “resolving” a module. The onResolve() plugin lifecycle callback configures how a module is resolved. The first argument to onResolve() is an object with a filter and namespace property. The filter is a regular expression run on the import string. Together, these select which modules your custom resolution logic applies to. The second argument to onResolve() is a callback that runs for each module import Bun finds that matches the filter and namespace defined in the first argument. The callback receives the path to the matching module and can return a new path for the module. Bun reads the contents of the new path and parses it as a module. For example, redirecting all imports to images/ to ./public/images/:
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bindex.ts

onLoad

After Bun’s bundler has resolved a module, it reads and parses the module’s contents. The onLoad() plugin lifecycle callback modifies the contents of a module before Bun reads and parses it. Like onResolve(), the first argument to onLoad() selects which modules this invocation of onLoad() applies to. The second argument to onLoad() is a callback that runs for each matching module before Bun loads its contents into memory. The callback receives the path to the matching module, its namespace, its default loader, and a defer function. The callback can return a new contents string for the module as well as a new loader. For example:
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bindex.ts
This plugin transforms all imports of the form import env from "env" into a JavaScript module that exports the current environment variables.

.defer()

One of the arguments passed to the onLoad callback is a defer function. It returns a Promise that resolves once all other modules have been loaded. Await it when a module’s contents depend on other modules.
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bindex.ts
The .defer() function can only be called once per onLoad callback.

Native plugins

Bun’s bundler is written in native code and uses multiple threads to load and parse modules in parallel. JavaScript plugins run on a single thread, because JavaScript itself is single-threaded. Native plugins are NAPI modules that expose lifecycle hooks as C ABI functions. They can run on multiple threads, so they run much faster than JavaScript plugins, and they skip work such as the UTF-8 -> UTF-16 conversion needed to pass strings to JavaScript. These lifecycle hooks are available to native plugins:
  • onBeforeParse(): Called on any thread before a file is parsed by Bun’s bundler.
To create a native plugin, export a C ABI function that matches the signature of the native lifecycle hook you want to implement.

Creating a native plugin in Rust

terminal
Then install this crate:
terminal
Inside lib.rs, use the bun_native_plugin::bun proc macro to define the function that implements your native plugin. Here’s an example implementing the onBeforeParse hook:
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/rust.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=c48e2f9ffc38d0c1d77ef723c617aca8lib.rs
To use it in Bun.build():
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bindex.ts

onBeforeParse

The onBeforeParse() callback runs immediately before Bun’s bundler parses a file. It receives the file’s contents and can optionally return new source code.
Bun can call this callback from any thread, so the NAPI module implementation must be thread-safe.

onEnd

Registers a callback to be run after the bundle is complete. The callback receives the BuildOutput object containing the build results, including output files and any build messages.
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bindex.ts
The callback can return a Promise. The promise returned by Bun.build() does not resolve until all onEnd() callbacks have completed.
https://mintcdn.com/bun-1dd33a4e/JUhaF6Mf68z_zHyy/icons/typescript.svg?fit=max&auto=format&n=JUhaF6Mf68z_zHyy&q=85&s=7ac549adaea8d5487d8fbd58cc3ea35bindex.ts