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
For some reasons, I want to use a wasm file from a react component.
Using the crate wasm-bindgen, here a simple example of code I want to compile into wasm :
use wasm_bindgen::prelude::*;#[wasm_bindgen]pubfnhello() -> u32{42}
Then I compile this with the command wasm-pack build --target web.
Then I import and use the compiled "hello" function from my src/index.tsx file like this :
This code works perfectly (the index page from my browser shows the number 42)
But...! if instead of return a u32 I return a String like this :
use wasm_bindgen::prelude::*;#[wasm_bindgen]pubfnhello() -> String{"banana".to_string()}
Then I got this error (from the terminal where I ran tuono dev) if I load the index page from my browser :
<unknown>:46635: Uncaught Error: Failed to construct 'TextDecoder': the 'fatal' option is unsupported.
thread 'tokio-runtime-worker' panicked at /home/okli/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tuono_lib-0.17.6/src/ssr.rs:54:10:
called `Result::unwrap()` on an `Err` value: InvalidJs("Execute your script with d8 to debug")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
And my browser didn't display anything expect the error "The connection was reset" :
I specify that the code works without any issues, whether I use u32 or String, when I use it in a simple React/ViteJS project (i.e., without going through Tuono).
Expected behaviour
I expect to see on my browser the text "banana" in a h1 tag
How to reproduce
create a new tuono project (version 0.17.7) named, by exemple, tuonowasm : tuono new tuonowasm
inside the "tuonowasm" dir, create the lib crate 'sample_wasm_example' : cargo new sample_wasm_example --lib
replace the default code in the tuonowasm/sample_wasm_example/src/lib.rs file by this :
then, remove the old wasm dir + compile the crate "sample_wasm_example" into wasm + re-run the tuono dev server : rm -rf src/wasm && cd sample_wasm_example && wasm-pack build --target web --out-dir ../src/wasm && cd .. && tuono dev then re-open your browser on http://localhost:3000
The browser shows the error page "The connection was reset" and an error appears in the terminal running the dev tuono server :
<unknown>:46635: Uncaught Error: Failed to construct 'TextDecoder': the 'fatal' option is unsupported.
thread 'tokio-runtime-worker' panicked at /home/okli/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tuono_lib-0.17.7/src/ssr.rs:54:10:
called `Result::unwrap()` on an `Err` value: InvalidJs("Execute your script with d8 to debug")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
I have uploaded below the project's source code, which follows the steps above, in case you don't feel like doing them again 😉.
However, make sure to reinstall the npm packages before restarting the Tuono dev server (the wasm folder is already present in the src directory).
I've separately come across this issue when working with wasm myself. Maybe we need to write/find a polyfill for TextDecoder server side? Or perhaps the wasm should only be run in the browser?
Description
For some reasons, I want to use a wasm file from a react component.
Using the crate wasm-bindgen, here a simple example of code I want to compile into wasm :
Then I compile this with the command
wasm-pack build --target web
.Then I import and use the compiled "hello" function from my
src/index.tsx
file like this :This code works perfectly (the index page from my browser shows the number 42)
But...! if instead of return a
u32
I return aString
like this :Then I got this error (from the terminal where I ran
tuono dev
) if I load the index page from my browser :And my browser didn't display anything expect the error "The connection was reset" :
I specify that the code works without any issues, whether I use u32 or String, when I use it in a simple React/ViteJS project (i.e., without going through Tuono).
Expected behaviour
I expect to see on my browser the text "banana" in a h1 tag
How to reproduce
tuono new tuonowasm
cargo new sample_wasm_example --lib
tuonowasm/sample_wasm_example/src/lib.rs
file by this :tuonowasm/sample_wasm_example/Cargo.toml
file by this :wasm-pack
:cargo install wasm-pack
(or with binstall)sample_wasm_example
crate into wasm file :cd sample_wasm_example && wasm-pack build --target web --out-dir ../src/wasm
tuonowasm/src/routes/index.tsx
file by this :npm i
tuono dev
The number 42 is showed, all works fine.
sample_wasm_example/src/lib.rs
file then modify it like this :rm -rf src/wasm && cd sample_wasm_example && wasm-pack build --target web --out-dir ../src/wasm && cd .. && tuono dev
then re-open your browser on http://localhost:3000The browser shows the error page "The connection was reset" and an error appears in the terminal running the dev tuono server :
I have uploaded below the project's source code, which follows the steps above, in case you don't feel like doing them again 😉.
However, make sure to reinstall the npm packages before restarting the Tuono dev server (the wasm folder is already present in the src directory).
tuonowasm.zip
Screenshots
No response
System Info
System info (Rust)
Additional context
No response
The text was updated successfully, but these errors were encountered: