To preload plugins before running code with Bun, you can configure the `preload` option in your `bunfig.toml` file. Here's how to do it:
1. Define your plugin as a JavaScript object with a `name` and `setup` function:
javascript
import { BunPlugin } from "bun";
const myPlugin: BunPlugin = {
name: "my-plugin",
setup(build) {
// Plugin implementation
},
};
2. In your `bunfig.toml` file, add the path to your plugin under the `preload` option:
toml
preload = ["./path/to/plugin.ts"]
This will execute your plugin before running any code with `bun run`.
3. Alternatively, you can preload plugins before running tests with `bun test`:
toml
[test]
preload = ["./path/to/plugin.ts"]
This will preload the plugin before executing your tests.
4. If you have multiple plugins, simply add them as separate entries in the `preload` array:
toml
preload = [
"./plugin1.ts",
"./plugin2.ts",
"./plugin3.ts"
]
Bun will execute the plugins in the order they are listed.
By configuring the `preload` option in your `bunfig.toml`, you can ensure that your plugins are loaded and ready to extend Bun's functionality before running your code or tests[1][3].
Citations:[1] https://bun.sh/docs/runtime/plugins
[2] https://betterstack.com/community/guides/scaling-nodejs/introduction-to-bun-for-nodejs-users/
[3] https://bun.sh/docs/runtime/bunfig
[4] https://github.com/oven-sh/bun/issues/6297
[5] https://bun.sh/docs/bundler/plugins
[6] https://blog.stackademic.com/unveiling-buns-potential-with-typescript-1ca01e150595?gi=034abd9f12d6
[7] https://socket.dev/npm/package/bun-coffeescript
[8] https://b-nova.com/en/home/content/bun-and-its-innovations/