You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using wasm-pack to build libraries is nice, but binaries are also good to have, both in the npm ecosystem and for browser usage (see also rustwasm/wasm-bindgen#1630 and rust-gamedev/wg#51). However, wasm-pack asserts in a few places that all crates being built are libraries.
💻 Basic example
A crate with the following code in src/main.rs should build and run as intended with wasm-pack build --target web:
#[cfg(target_arch = "wasm32")]use wasm_bindgen::prelude::*;#[cfg(target_arch = "wasm32")]fndisplay(text:&str){// Use `web_sys`'s global `window` function to get a handle on the global// window object.let window = web_sys::window().expect("no global `window` exists");let document = window.document().expect("should have a document on window");let body = document.body().expect("document should have a body");// Manufacture the element we're gonna appendlet val = document.create_element("p").expect("failed to create a <p>");
val.set_inner_html(text);
body.append_child(&val).expect("failed to append child to body");}#[cfg(not(target_arch = "wasm32"))]fndisplay(text:&str){println!("{}", text);}fnmain(){display("Hello from Rust!");}
(I've made the changes needed in wasm-bindgen already; they're included in v0.2.54. I have a basic implementation for this feature already, but it needs additional tuning to behave as expected in all cases.)
The text was updated successfully, but these errors were encountered:
💡 Feature description
Using
wasm-pack
to build libraries is nice, but binaries are also good to have, both in the npm ecosystem and for browser usage (see also rustwasm/wasm-bindgen#1630 and rust-gamedev/wg#51). However,wasm-pack
asserts in a few places that all crates being built are libraries.💻 Basic example
A crate with the following code in
src/main.rs
should build and run as intended withwasm-pack build --target web
:(I've made the changes needed in wasm-bindgen already; they're included in v0.2.54. I have a basic implementation for this feature already, but it needs additional tuning to behave as expected in all cases.)
The text was updated successfully, but these errors were encountered: