-
Notifications
You must be signed in to change notification settings - Fork 29
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
Option to generate code which works with other bundlers #14
Comments
Yes, that is correct, since Using For NodeJS support you should set |
ok thx for the response. As far as I understand the My idea was to keep using |
Unfortunately each bundler has their own way of loading static files, so there isn't a way to make it work with all of them. This is also a problem with other things like images and CSS. So you will probably have to give instructions for each bundler on how to load the static file (e.g. Webpack would use asset modules).
You can do that by manually running But I don't recommend that, since it will only work with Webpack (and only with experimental flags enabled). Your best option is to use |
I hacked something that makes it slightly more compatible with bundlers, but it's not polished enough to PR. Right now the wasm-pack output prefix is hardcoded to "index", changing it to "toml.package.name", and then having rollup emit a "filename" instead of a "name" for the wasm file makes it so that the filename in the generated wasm-pack library matches what rollup ends up creating. // change
"--out-name", "index",
// to
"--out-name", toml.package.name,
// change
name: name + ".wasm"
// to
fileName: name + ".wasm"
// change anywhere there is "index" hardcoded in a string to toml.package.name The plugin generated code also contains a default path and dynamic logic that second-level bundlers are unlikely to be able to update, so it needs to be removed (or probably disabled with config for backwards compatibility): // change
export default async (opt = {}) => {
let {importHook, serverPath} = opt;
let path = ${import_wasm};
if (serverPath != null) {
path = serverPath + /[^\\/\\\\]*$/.exec(path)[0];
}
if (importHook != null) {
path = importHook(path);
}
await exports.default(path);
return exports;
};
// to (can probably be cleaned up further)
export default async () => {
await exports.default(path);
return exports;
}; That is enough to allow vite to rebundle without errors or modification. There is an additional hook for resolving import.meta.url which could possibly be used to customize the server path at runtime without breaking bundling by injecting some code the way importHook does, but I haven't figured out a pattern that works better than just using the above fixes: // something like this,
resolveImportMeta(property, {moduleId}){
if(property === "url" && state.tomlPackageNames[moduleId] !== null && options.metaUrlHook)
return options.metaUrlHook(state.tomlPackageNames[moduleId]);
}
// moduleId is the import path for the wasm_pack js file, state.tomlPackageNames[moduleId] is toml.package.name
// whatever code is provided to the metaUrlHook config would replace import.meta.url in the following code.
// I haven't come up with a way for this to be useful to the re-bundler yet, but it seems like it could be a good spot
// for environment config or globals
async function init(input) {
if (typeof input === 'undefined') {
input = new URL('whatever.wasm', import.meta.url); |
@npbenjohnson You might be able to get it working using the new |
Am I correct that there is currently no set of flags/experimental options that would output a wasm module identical to A little background for my slighly weird use case: I wanted to avoid JS and use Rust to create a user script (with inlined base64 wasm), this plugin nicely allowed to do that But then some sites CSP-block wasm instantiation, so as a workaround I disable inlining and transpile Then I modify the glue js files produced by But then this only works if I build the wasm itself with When I use this plugin, the script fails due to unknown Which as far as I understand is because of that hardcoded Or am I missing something simple? |
The point of Since this plugin is being used with Rollup, you're bundling with Rollup, so there's no need for the
I don't think there's much you can do about that. wasm2js won't work forever, because there are many features that are Wasm-only, and so they can't be compiled to JS. That's really something that should be fixed in the user script system, allowing the user script to bypass the page's CSP. I believe Chrome/Firefox extensions can bypass the page's CSP, so it should be possible. What user script system are you using? TamperMonkey?
I don't think you're missing anything, this Rollup plugin just isn't designed for your specific use case. You could create a Rollup plugin that runs |
Thanks for your prompt and detailed response!
Hopefully by then wasm will be a first-class citizen just like like JS so could sidestep the JS swap altogether!
Violentmonkey Afaik there is some Firefox bug that prevents it Though in general I don't understand why I can run JS (when CSP also only allows scripts from a specific website
For now I'd like to try to make a simpler yarn sequential invokation of commands that would do those 2 steps and then call rollup to get the final file |
It's because Wasm is a separate CSP directive: https://www.aaron-powell.com/posts/2019-11-27-using-webassembly-with-csp-headers/ It is very dumb. JS can do everything Wasm can do, so if it's possible to run JS scripts then it should be possible to run Wasm as well. But unfortunately the browsers decided to be ultra-strict for no reason.
Perhaps they could automatically add in the |
Thanks for the links, I've read them while trying to find out some workaround. What I still didn't fully get was - there is no
Indeed, the whole thing didn't make any sense to me. Then there is another "feature" I've read about re Manifest V3 where there is no differentiation between remote wasm code and embedded/hashed
I think this lack of separation is he issue here, right? The browser doesn't treat same-origin wasm the same way as same-origin JS? That's why ↓ is needed
Thanks for the tip, tried to do that with the CSP editing extension https://chrome.google.com/webstore/detail/modheader-modify-http-hea/idgpnmonknjnojddfkpgkljpfnnfcklj/related?hl=en-US, but it doesn't work properly - for some reason there is no "append" functionality for CSPs, so I can either override it to allow running inlined wasm (this works) or manually append, neither of which is a feasible option (interesting that even a dedicated extension doesn't work here) |
But you can't load Wasm with
Thankfully they changed their mind and decided to allow
No, it has to do with arbitrary code execution. By using But the Chrome extension team doesn't want that, because it causes a security risk. For example, the Chrome extension could use So the Chrome team wants to disable However, you can't run Wasm code with That's a different issue than what you are having. That issue is about the CSP for the Chrome extension, but your issue is about the CSP of the website. So your issue will need to be fixed separately. |
so far the target for wasm-pack is hard coded to
web
:rollup-plugin-rust/index.js
Line 148 in 6138501
However, I wanna build and rollup a library which then another one should should bundle. Thus, I need to set the target to
bundler
ornodejs
.It would be great if the plugin supports a simple option for that.
The text was updated successfully, but these errors were encountered: