Bun

interface

BunRegisterPlugin

interface BunRegisterPlugin

Extend Bun's module resolution and loading behavior

Plugins are applied in the order they are defined.

Today, there are two kinds of hooks:

  • onLoad lets you return source code or an object that will become the module's exports
  • onResolve lets you redirect a module specifier to another module specifier. It does not chain.

Plugin hooks must define a filter RegExp and will only be matched if the import specifier contains a "." or a ":".

ES Module resolution semantics mean that plugins may be initialized after a module is resolved. You might need to load plugins at the very beginning of the application and then use a dynamic import to load the rest of the application. A future version of Bun may also support specifying plugins via bunfig.toml.

A YAML loader plugin

Bun.plugin({
 setup(builder) {
  builder.onLoad({ filter: /\.yaml$/ }, ({path}) => ({
    loader: "object",
    exports: require("js-yaml").load(fs.readFileSync(path, "utf8"))
  }));
});

// You can use require()
const {foo} = require("./file.yaml");

// Or import
await import("./file.yaml");

  • clearAll(): void;

    Deactivate all plugins

    This prevents registered plugins from being applied to future builds.