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.
-
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 web
per 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 web
example. 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 itsinit
functioncopy-webpack-plugin
and awebpack-dev-server
override to copy the files from the crate'spkg
directory (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 theimport
call in the bundled app. By letting the app do the init, I can ensure thatinit_libfoo
runs first.And it does indeed work like the
--target bundler
version 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-bindgen
to 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 web
with a bundler.But...
That feels messy. I don't want to be hanging my module on the global
window
if I can avoid it. And unfortunately, threading support still doesn't work. When I configurecargo.toml
as directed, I see this:which, in this particular case, seems to emanate from
serde-yaml
. Simple functions likeadd
are 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 web
is so far just slower (because it requires dev-server to write to disk) and more clumsy than--target bundler
without benefits.tl;dr: is there a better way to make a module built with
--target web
still 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-bindgen
docs, but I haven't used that package directly myself because I don't use or needrayon
itself. I'll try using that library next, just in case itsinit
functions handle the errors that I'm seeing.Beta Was this translation helpful? Give feedback.
All reactions