- This is supposed to be a minimal example of using rust_xlsxwriter, but it is quite a large project to be considered minimal. This will be difficult to fix, though, since most files are present to replace unimplemented-std-methods with browser/javascript-vm functions.
- Node and Deno crash javascript-side whenever an io error occurs (instead of returning a Result that we could catch in Rust).
This repo uses cargo-make to manage all the different compilation targets. It is by no means neccessary to create a WASM application, but using it reduces complexity, since most targets are built by repeating similar commands.
All rules can be found in Makefile.toml, and each contains a
profile called release that selects the rustc compilation flag and calls
wasm-opt
when enabled.
Remember that profiles in cargo-make must be selected
before the rule
(e.g. cargo make -p release all
instead of ).cargo make all -p release
cargo install cargo-make
rustup target add wasm32-unknown-unknown
cargo install wasm-bindgen-cli wasm-opt
Warning
The wasm-bindgen
command may fail with a linkage error if the
wasm-bindgen-cli cargo-installed-package doesn't match the wasm-bindgen
cargo-added-package. If this happens to you, just cargo update
or read the
error thrown in stderr.
rustup target add wasm32-wasip1
You can also use the experimental
wasm32-wasip1-threads
target, or the stabilized-yet-experimental
wasm32-wasip2
target. Both of them implement the std fully, meaning they can use
std::thread::spawn
, which wasm32-wasip1 can't.
If you end up choosing one of the above instead of wasm32-wasip1, change the
variable WASM_WASI
in Makefile.toml accordingly. Also, since
they are experimental, you may need to use nightly instead of stable, so change
the value in rust-toolchain.toml accordingly.
rustup target add wasm32-wasip1-threads
rustup target add wasm32-wasip2
cargo make browser
- Open a localhost server in pkg/browser.
For example, with
npm i -g http-server
then
http-server -c 1 -p 8080 pkg/browser
- Open a browser and go to http://localhost:8080/index.html
cargo make nodejs
node pkg/nodejs/rust_xlsx_wasm_example.js
cargo make deno
deno run --allow-write --allow-read pkg/deno/rust_xlsx_wasm_example.js
In order to run the .wasm file, you will need to have
Wasmtime installed.
--dir=.
allows WASM to preopen the current directory (essentially giving it
access to it, which it normally can't since WASM is sandboxed).
cargo make wasi
wasmtime --dir=. pkg/wasi/rust_xlsx_wasm_example.wasm
cargo make wasi
wasmtime --wasi threads --dir=. pkg/wasi/rust_xlsx_wasm_example.wasm
To build all targets:
cargo make all
To run both cargo clean
and rm -rf pkg
(but platform independent):
cargo make clean