interface
BunRegisterPlugin
interface BunRegisterPlugin
Extend Bun's module resolution and loading behavior
Plugins are applied in the order they are defined.
There are two kinds of hooks:
onLoadreturns source code or an object that becomes the module's exportsonResolveredirects a module specifier to another module specifier. It does not chain.
Plugin hooks must define a filter RegExp and only match when 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 in 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");Deactivate all plugins
This prevents registered plugins from being applied to future builds.