-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Description
What version of Bun is running?
1.3.10+30e609e08
What platform is your computer?
Darwin 25.2.0 arm64 arm
What steps can reproduce the bug?
const entryFilter = /tiny-module\.js$/;
Bun.plugin({
name: "tiny-module-patch",
setup(build) {
// do not apply filter
build.onLoad({ filter: entryFilter }, () => {
return {
loader: "js",
contents: `
module.exports.greet = function greet(name) { return "patched hello " + name; }
module.exports.add = function add(a, b) { return a + b + a; }
`,
};
});
},
});
In a seperate file create the following CJS tiny-module.js
module.exports.greet = function greet(name) {
return `hello ${name}`;
}
module.exports.add = function add(a, b) {
return a + b;
}
require and run the above module in a seperate file (bun --preload=location-of-plugin run filename )
const { greet, add } = require('./tiny-module');
console.log(greet('world'));
console.log(add(1, 2));
What is the expected behavior?
The Bun documentation never states that Bun plugins are intended solely for ESM. Either it should properly document this limitation, or plugins should be able to differentiate between CommonJS and ESM and output the code accordingly.
The above code should be properly patched and should not break CJS code.
The expected behavior should be that the CJS module now returns the contents returned by the Bun plugin, in this case the example should log
"patched hello world"
4
What do you see instead?
The above breaks CJS code
1 | const { greet, add } = require('./tiny-module');
2 |
3 | console.log(greet('world'));
^
TypeError: greet is not a function. (In 'greet("world")', 'greet' is undefined)
at /Users/user/tests/bun-tests/run-demo.js:3:13
at loadAndEvaluateModule (2:1)
Bun v1.3.10 (macOS arm64)
Additional information
No response