emily-curry / snowpack-plugin-wasm-pack

Snowpack plugin for rust using wasm-pack 🦀
The Unlicense
3 stars 1 forks source link

Plugin does not work with `optimize.bundle: true` #3

Open emily-curry opened 3 years ago

emily-curry commented 3 years ago

From #1:

The issue seems to be in how the snowpack optimize.bundle flag interacts with this plugin. In particular, this plugin sets a mount section with static: true for the wasm-pack output, which the snowpack docs indicate should mean that those files get written to the disk exactly as-is. However, that's not the behavior that's happening when optimize.bundle is true, the wasm-pack output is still getting included in the bundle, and the import paths are not being properly re-written. All that to say, disabling that flag seems to be the solution for now. Snowpack itself does not yet recommend using the built-in optimize functionality, as it is new, and still recommends other solutions. And I am not certain if it is safe to have a bundler transform the wasm-pack output.

This issue exists to track the issue with snowpack.

trevyn commented 3 years ago

Wait, it doesn't? I think I've been using it...

Is this related to https://github.com/snowpackjs/snowpack/discussions/1053#discussioncomment-217539 ?:

the tldr of current thinking is that it's on the user to load WASM themselves, and we won't officially support any import './foo.wasm' import sugar due to how differently every bundler treats this when you go to optimize your site

Snowpack optimize.bundle flag seems to copy in the index_bg.wasm file directly, and then I have to pass the URL to the wasm-pack init function:

import init, { wasm_function } from "./my_package";

(async () => {
 await init("my_package/index_bg.wasm");
 console.log(wasm_function());
})();

(This may need some tweaking for your particular project directory layout, but I think the general approach works.)

Edit: Also https://www.snowpack.dev/guides/wasm :

To use WASM with Snowpack: Use the browser’s native WebAssembly & fetch APIs to load a WASM file into your application

(Which is basically what the wasm-pack init function does when you pass it a URL as a string, see the generated JS.)

emily-curry commented 3 years ago

This is good to know, thank you for the info!

The issue you linked is somewhat related, in that wasm-pack makes the same argument and generates code that uses the import semantics, but under the hood generates all the WebAssembly.instantiateStreaming... stuff. Link to those docs: https://rustwasm.github.io/docs/wasm-bindgen/reference/deployment.html#bundlers

The real issue there is that if you call init without an arg, it makes the assumption that the wasm file is in the same dir, and creates a URL object that points to it. That URL's path isn't rewritten by the bundler with optimize on (although apparently this does happen in webpack). The workaround you mentioned is probably the easiest solution for consumers of the plugin, so thank you for posting. I'll continue to leave this issue open and pinned to communicate that this is currently unsupported (explicitly so by wasm-pack, see link above).