Open emily-curry opened 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.)
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).
From #1:
This issue exists to track the issue with snowpack.