#How tf do you make a plugin that works without compiling?
1 messages · Page 1 of 1 (latest)
Thanks for tagging me @eternal quiver. I'll look through relevant sources and get back to you shortly.
Answer
Plugins work differently depending on whether you're running directly with bun run vs compiling with bun build --compile.
**For runtime (non-compiled) **, you register plugins using Bun.plugin() typically via a preload script:
(1)
import { plugin } from "bun";
plugin({
name: "Custom loader",
setup(build) {
// implementation
},
}) ;
For compiled executables, plugins must be passed into the plugins array when calling Bun.build() :
(2)
import type { BunPlugin } from "bun";
const envPlugin: BunPlugin = {
name: "env-loader",
setup(build) {
build.onLoad({ filter: /\.env\.json$/ }, async args => {
const env = await Bun.file(args.path) .json() ;
return {
contents: `export default ${JSON.stringify(env) };`,
loader: "js",
};
}) ;
},
};
await Bun.build({
entrypoints: ["./cli.ts"],
compile: {
outfile: "./mycli",
},
plugins: [envPlugin],
}) ;
Key point: As of Bun v1.2.21, Bun.build supports compile: true which unblocks build-time plugins.
(3) , (4)
So to have something work in both scenarios, you'd need:
- A preload script with
Bun.plugin()forbun run - A build script that passes the same plugin logic to
Bun.build({ plugins: [...], compile: true })for compilation
The plugin definition itself can be shared, but the registration mechanism differs between runtime and build-time.
To learn more, see the sources I considered:
[1] Plugins
[2] Single-file executable
[3] Bun v1.2.21
[4] Single file executable does not appear to work with plugins (at least not MDX)
Feel free to tag @zealous lantern with additional questions. I also appreciate feedback, it helps me improve.
ignore that, it doesnt help
to note, I've noticed a mismatch in behaviour for module resolution, it always works when compiled, but never when not compiled
Appreciate the feedback! Feel free to ask me again with additional guidance or context.
Appreciate the feedback! Feel free to ask me again with additional guidance or context.