cargo new <NAME> --lib
add the following to Cargo.toml
(or respective versions of the libs):
# ...
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2"
# helpful (but not necessary) dependencies
js-sys = "0.3.65" # JS interface
reqwasm = "0.2" # requests: paired with futures
wasm-bindgen-futures = "0.4.38" # futures / async / await
web-sys = { version = "0.3", features = ['console'] } # console logging
[dev-dependencies]
wasm-bindgen-test = "0.2"
cargo build
wasm-pack build --release --target web --no-typescript --out-dir <OUT_DIR>
.js
file, this is how to initialize and use:
import init, { <RUST_WASM_FUNCTIONS_AND_STRUCTS> } from './pkg/<PROJECT>.js';
// this path is relative to the .js file ^^^^^^^^^^^^^^^^^^^
async function init() {
await init('./pkg/
function example() {
// if rust_print
and it expects a &str
rust_print("Hello!");
}
async function async_example() {
// if rust_request
await rust_request();
}
init(); example(); await async_example();