Description
Feature
#3937 contains a good discussion of the problems encountered when trying to compile a program that has any wasm-bindgen
dependencies in a WASI environment. However, this has significant effects on the ability to use wasmtime
, or any environments that use wasmtime
, to render HTML for apps created in any of the common Rust frontend frameworks. This is because virtually the whole Rust web ecosystem including web-sys
and js-sys
depend on wasm-bindgen
, and they are the building blocks for all the Rust frontend web frameworks. For example, it's very common f
If you are building a native server binary, these wasm-bindgen
ecosystem libraries compile fine, but panic if you try to run them, for obvious reasons — you are not running in the browser, so do not have access to web APIs. Frameworks are built to handle this. For example, it's very common to declare an HTML element event listener that depends on one of the web_sys::Event
types. This can be compiled into a native server binary; it simply won't ever be run on the server, so it does not cause an issue.
However, attempting to do the same in wasmtime
causes the error in the issue linked above. See e.g., leptos-rs/leptos#295
Benefit
This would allow people to use wasmtime
for server-side rendering in frameworks like Yew, Sycamore, Leptos, etc. that depend on web-sys
and wasm-bindgen
, just as they can use native binaries for that same task.
Implementation
I don't know enough about how wasm-bindgen
works internally to make a real proposal, but given that none of the wasm-bindgen
code actually needs to run, it seems that the "allow it to compile and panic if called" solution used for building native binaries for other platforms should work just as well. I'm sure the fact that this seems straightforward is a testament to my own ignorance, here, so apologies.
Alternatives
I've laid out a couple alternative approaches for users to this kind of issue in my reply to the issue in the Leptos repo (leptos-rs/leptos#295)
- Leptos mocks and reexports most of
web-sys
, in a way that usesweb-sys
on the client and stubs on the server. Significant maintenance burden for the framework.- Leptos adds a wasi feature. Opting into it means you lose
web-sys
types for this and need to do something like#[cfg(not(feature = "wasi"))]
on all your event listeners and other code that needs platform types. Significant burden on library users.- Use a Wasm-based serverless platform like Cloudflare Workers that's built on V8, so does support wasm-bindgen.
- Use a platform that lets you deploy native binaries so you can build on top of an Actix or Axum server to serve your app.