Set up wasm-bindgen for easy Rust/JavaScript Interoperability

Nik Graf
InstructorNik Graf

Share this video with your friends

Send Tweet

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.

Malachiain
Malachiain
~ 3 years ago

I think export const appendStringtoBody = (value) => { in the transcript should be export const appendStringToBody = (value) => {. I hope this helps someone else.

Giuseppe Cardella
Giuseppe Cardella
~ 2 years ago

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

hkwid
hkwid
~ 2 years ago

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

Nik Graf
Nik Grafinstructor
~ 2 years ago

Thanks Giuseppe & hkwid for the hints!

EdmundsEcho
EdmundsEcho
~ 2 years ago

A recent fix to the "can't load relative module" is to set the attribute raw_module= "../domUtils"

Jeremy Swanborough
Jeremy Swanborough
~ a year ago

raw module binding is the only thing that worked for me, thanks @EdmundsEcho