Replies: 1 comment 6 replies
-
This comes down to threading support requiring
Unfortunately I know nothing about
Generally speaking, even though threading support in Rust+Wasm+ |
Beta Was this translation helpful? Give feedback.
-
This comes down to threading support requiring
Unfortunately I know nothing about
Generally speaking, even though threading support in Rust+Wasm+ |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This is half a question and half a possible answer for others. It addresses a use case that is missed by the docs (AFAICT).
Backdrop: I've had a react + rust-via-wasm app happily running for a long time using the default
wasm-pack build [--target bundler]. I'd like to add threading support to the WASM binary which explicitly needs--target webper this. I'd like to keep using Webpack (and CRA) for everything else; I don't want to move DOM manipulation into Rust (yet), something that seems to be implied by the--target webexample. I'd like the bundled JS app to still be able to call the WASM bindings.Rather than
init()ing the wasm module in the<script>tag like the example shows, I found I had to do the following:libfoo, to thewindow, along with itsinitfunctioncopy-webpack-pluginand awebpack-dev-serveroverride to copy the files from the crate'spkgdirectory (whereas before, in bundler mode, I was able to rely onyarn link)Like so:
index.html
wasm_context.tsx
config-overrides.js
This setup prevents a race condition between the
await init()call in the<script>and theimportcall in the bundled app. By letting the app do the init, I can ensure thatinit_libfooruns first.And it does indeed work like the
--target bundlerversion did - most of the time. There appears to still be a race condition in initialization, and some page loads break withTypeError: window.init_sched is not a function. That is not an issue forwasm-bindgento handle, it's a browser/react thing for me to investigate. But it does reinforce that the above is not the "right" way to mix--target webwith a bundler.But...
That feels messy. I don't want to be hanging my module on the global
windowif I can avoid it. And unfortunately, threading support still doesn't work. When I configurecargo.tomlas directed, I see this:which, in this particular case, seems to emanate from
serde-yaml. Simple functions likeaddare callable without error.I have not yet investigated that one well enough to know if that is a distinct issue or related to how I'm initializing this. But it means that
--target webis so far just slower (because it requires dev-server to write to disk) and more clumsy than--target bundlerwithout benefits.tl;dr: is there a better way to make a module built with
--target webstill usable from code bundled bywebpack? And is there something I did above that prevents thread support from working?FYI, I also used the docs for wasm-bindgen-rayon as a reference, since they seem to be more recent than the relevant
wasm-bindgendocs, but I haven't used that package directly myself because I don't use or needrayonitself. I'll try using that library next, just in case itsinitfunctions handle the errors that I'm seeing.Beta Was this translation helpful? Give feedback.
All reactions