method
BunPlugin.setup
Called when the plugin is loaded.
This function may be called in the same tick that it is registered, or it may be called later. It may be called multiple times for different targets.
The builder object for registering plugin hooks
Referenced types
interface PluginBuilder
The builder object passed to Bun.plugin
- config: BuildConfig & { plugins: BunPlugin[] }
The config object passed to
Bun.buildas is. Can be mutated. - specifier: string,): this;
Create a lazy-loaded virtual module that can be
imported orrequired from other modules@param specifierThe module specifier to register the callback for
@param callbackThe function to run when the module is imported or required
@returnsthisfor method chainingBun.plugin({ setup(builder) { builder.module("hello:world", () => { return { exports: { foo: "bar" }, loader: "object" }; }); }, }); // sometime later const { foo } = await import("hello:world"); console.log(foo); // "bar" // or const { foo } = require("hello:world"); console.log(foo); // "bar" - ): this;
Register a callback that runs when bundling ends, after all modules have been bundled and the build is complete.
@returnsthisfor method chainingconst plugin: Bun.BunPlugin = { name: "my-plugin", setup(builder) { builder.onEnd((result) => { console.log("bundle just finished!!", result); }); }, }; - ): this;
Register a callback to load imports with a specific import specifier
@param constraintsThe constraints to apply the plugin to
@param callbackThe callback to handle the import
@returnsthisfor method chainingBun.plugin({ setup(builder) { builder.onLoad({ filter: /^hello:world$/ }, (args) => { return { exports: { foo: "bar" }, loader: "object" }; }); }, }); - ): this;
Register a callback to resolve imports matching a filter and/or namespace
@param constraintsThe constraints to apply the plugin to
@param callbackThe callback to handle the import
@returnsthisfor method chainingBun.plugin({ setup(builder) { builder.onResolve({ filter: /^wat$/ }, (args) => { return { path: "/tmp/woah.js" }; }); }, }); - ): this;
Register a callback that runs when bundling starts. With hot module reloading, it runs at the start of each incremental rebuild.
@returnsthisfor method chainingBun.plugin({ setup(builder) { builder.onStart(() => { console.log("bundle just started!!") }); }, });