Interoperability between JavaScript and Rust is limited to numerics and accessing memory directly. Since this can be exhausting and overwhelming to do manually the Rust/Wasm team has created the wasm-bindgen project to facilitate high-level interactions between Rust and JavaScript.
I think export const appendStringtoBody = (value) => {
in the transcript should be
export const appendStringToBody = (value) => {
. I hope this helps someone else.
If you get a relative path error:
error: relative module paths aren't supported yet
5 | #[wasm_bindgen(module="../domUtils")]
Put the domUtils.js
inside of the /pkg
and change the line in lib.rs
from #[wasm_bindgen(module="../domUtils")]
to #[wasm_bindgen(module="domUtils")]
and rerun wasm-pack build
error: relative module paths aren't supported yet 5 | #[wasm_bindgen(module="../domUtils")]
To fix the error of relative path, you need to change [wasm_bindgen(module="../domUtils")]
-> [wasm_bindgen(module="/domUtils")]
. In this case /
means root of the project.
When you move domUtils
, you will face another error from webpack-dev-server
.
[Reference] https://rustwasm.github.io/docs/wasm-bindgen/reference/js-snippets.html?highlight=relative,path#caveats
Thanks Giuseppe & hkwid for the hints!
A recent fix to the "can't load relative module" is to set the attribute raw_module= "../domUtils"
raw module binding is the only thing that worked for me, thanks @EdmundsEcho