-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bun Runtime plugin onLoad
method is not being called
#9446
Comments
Related: #9373 |
This was caused by my not reading the documentation properly. I was trying to use a runtime plugin's I learnt about virtual modules, which seems like what I want and I was able to make one work, but the specifiers there need to be strings, not regexes, which foils my plan of adding a loader for a custom protocol. I see a note about virtual modules not being available for bundler plugins and the possibility of using a combination of import { plugin } from "bun";
plugin({
name: "test",
setup(build) {
console.log("Plugin setup was called!", build);
build.onResolve({ filter: /some:/ }, (args) => {
console.log("Plugin onResolve was called!", args);
return { path: './some.thing' };
});
build.onLoad({ filter: /\.thing$/ }, (args) => {
console.log("Plugin onLoad was called!", args);
return { contents: JSON.stringify(args) };
});
},
}); However it looks like Could any maintainers confirm my findings here, please? If I am correct, I will create a feature request ticket for either loosening the requirement for the module/package to exist prior to the loader being applied to it or perhaps for a new loader hook whose regex will be applied against the bare specifier string not the path. I am imagining there should be a way to do something like this to make import { plugin } from "bun";
plugin({
name: "test",
setup(build) {
console.log("Plugin setup was called!", build);
build.onLoad({ filter: /proto:/ }, async (args) => {
console.log("Plugin onLoad was called!", args);
await …;
return { contents: 'export default "yay!";', loader: 'ts' };
});
},
}); Or potentially this? import Bun, { plugin } from "bun";
import fs from 'fs';
plugin({
name: "test",
setup(build) {
console.log("Plugin setup was called!", build);
build.onResolve({ filter: /proto:/ }, async (args) => {
console.log("Plugin onResolve was called!", args);
// Drop a temporary file here
await Bun.write('./some.thing', 'YAY');
return { path: './some.thing' };
});
build.onLoad({ filter: /\.thing$/ }, (args) => {
console.log("Plugin onLoad was called!", args);
// Delete the temporary file (use it or not)
await fs.promises.unlink('./some.thing');
// Return whatever virtual content I want for the `proto:col` URL here
return { contents: JSON.stringify(args) };
});
},
}); Or maybe the simplest? This would be the best actually, I think. import { plugin } from "bun";
plugin({
name: "test",
setup(build) {
console.log("Plugin setup was called!", build);
build.module(/some:/, async () => {
console.log("Plugin module was called!");
await …;
return { contents: 'export default "yay!";', loader: 'ts' };
});
},
}); Thanks for the feedback! |
What version of Bun is running?
1.0.31+e25675121
What platform is your computer?
Darwin 23.4.0 arm64 arm
What steps can reproduce the bug?
Follow the Runtime Plugins example to create a catch-all custom plugin:
Register the plugin in
bunfig.toml
:Run a test script to see what it does:
bun -e "import test from './hi.txt'; console.log(test);"
What is the expected behavior?
onResolve
to be called if it is specified and matches,onLoad
to also be called if it is specified and matches.My ultimate goal is to create a plugin for web import support, so I will change the
filter
tohttps?:\/\/
once this issue is solved and then will attempt tofetch
the URL and return{ sourcecode }
in theonLoad
hook.What do you see instead?
onResolve
is called:onLoad
is not called.Additional information
When I remove the
onResolve
from my plugin and leave onlyonLoad
, it is not called:Run:
bun -e "import test from './hi.txt'; console.log(test);"
:The text was updated successfully, but these errors were encountered: