-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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 onResolve
doesn't filter non-file:
protocol imports
#9863
Comments
In theory this could be resolved by supporting import maps. Then we wouldn't need to use plugins. |
That's interesting. I found #4458 talking about adding import map support to the bundler. But I wonder how one would define import maps for the runtime? Bunfig? |
Ultimately my interest lies in being able to make up a module from thin air when using a custom protocol in the import. Ideally |
Like this https://github.com/guest271314/webbundle/blob/main/deno.json
Then The idea for me to get away from runtime-specific inventions, so we can run the same code in |
This is related to Edit: turns out mutating import maps at runtime is not possible in browsers, I mis-remembered: WICG/import-maps#92. |
For your use case you should be able to use a Data URL or a Bob URL without having to create a custom protocol. See Deno dynamic import("./exports") throws module not found for "exports.js" dynamically created in the script
then use dynamic
Deno's dynamic |
I am not attempting to create a custom protocol, I have an existing custom protocol which I am looking to bridge Bun into supporting. It is possible that runtime plugins are intentionally locked into the Node has experimental support for custom loaders which I believe are roughly equivalent to Bun's runtime plugins and AFAICT it should be possible to create a custom loader which matches the URL by protocol, here demonstrated on a rudimentary HTTP/HTTPS loader: Whether it is by |
Why is the custom protocol necessary? Can't you just do
? |
Whatever is on the other side of the import is dynamic, I can't bake it in or use a static |
Reproduced on Bun version 1.0.36 (bun-linux-x64-baseline) re I was able to get the expected work using
|
This alone works too
|
I'm not sure how that issue is related. Bun doesn't implement import maps so we can't test import maps at all in Technically this is possible
And possible for
for the capability to to use Then you can use dynamic |
Should be
to create a Blob URL. Which also gets around |
This is how
or
See |
Using import-map.json
|
Thanks for the PR, I noted in the repro repo README and in my initial post in this issue that I can't use As far as import maps go, I was just pondering how it could look, I am aware Bun doesn't support import maps at the moment, but should it, I would hope for an implementation where the import map can be passed to I am aware of the Node loader support, I've linked it previously. This is something I am hoping to do an equivalent of in Bun, because it seems like it can support custom protocols as the HTTP/HTTPS loader demonstrates. I believe if I've been able to build Bun locally and while I am not at al proficient in Zig, I think I've gathered enough context by now to maybe be able to contribute regex support to |
Sure you can use The Node loader and Bun plugin systems are roughly the same from my perspective. Import maps are by far the least amount of code and less cumbersome. |
I cannot write out all the specifiers I am expecting as the specifiers are dynamic, not known at compile time. |
You have to be able to write them out at some point in the process, else you will be getting "Module not found" errors. |
With |
Yes, there is. How else are you going to resolve some arbitrary specifier to a real path? That doesn't matter. There MUST be a mapping between the specifiers you are expecting and URL's. You MUST already know what arbitrary specifiers map to which local or remote paths. There is no escaping these technical facts. From my perspective both Node.js loader and Bun plugin systems are clucky and less ideal than import maps - but that's all |
Then how are you going to write a |
Here's the simplest case I can think of to demonstrate
|
As I suggested above you can use a Blob URL or Data URL for that. Even simpler is to just write out the |
I see what you are trying to do now. You are correct, Bun doesn't have the specifier parameter string as a parameter exposed in the function that returns content and loader. That's a reasonable feature request. The |
I'll try t figure out a way or two to achieve what you are tryiing to do with what we already have exposed. |
Here's one way to achieve the requirement, including still using We can do this without
|
The script file name passed to plugin2.ts
|
Substituting
then we can do
and get this result
|
To not hard code any specifier we can use
|
Then you can include something like this in the script
|
I don't think |
There is a way to do this using
prints
|
What version of Bun is running?
1.1.0+5903a6141
What platform is your computer?
Darwin 23.4.0 arm64 arm
What steps can reproduce the bug?
See my repro repo: https://github.com/TomasHubelbauer/bun-runtime-plugin-onResolve-custom-protocol
Make a runtime plugin with an
onResolve
hook:Register it in
bunfig.toml
:preload = ["./plugin.ts"]
.Run a script with an import that should get captured by the plugin:
bun index.ts
:So far everything works as expected:
Change the plugin to filter for a custom protocol:
Change the import and run the script again:
Now we see a problem:
Notice that
onResolve2
was never even called.This might be a design decision, not a real bug, if I learn as much, I will contribute a documentation change explaining this.
What is the expected behavior?
What do you see instead?
A runtime error in module resolution not preceded by the custom
onResolve
hook call.Additional information
This might not be a bug but a design decision. I ended up trying this, because
build.module
doesn't accept a regex for the module name (so that I could virtualize all modules on a custom protocol) so I ended up looking atonResolve
. Ultimately I would prefer a change tobuild.module
that allows for regexes, but this might also be worth supporting IMO.The text was updated successfully, but these errors were encountered: