#wasm-module #wasm #utility-macro #compile-time #utility #macro

nightly macro include-wasm-rs

Builds a Rust WebAssembly module at compile time and returns the bytes

4 releases

0.1.3 Feb 28, 2024
0.1.2 Feb 22, 2024
0.1.1 Feb 16, 2024
0.1.0 Dec 13, 2023

#684 in Procedural macros

MIT license

22KB
422 lines

Include Wasm-RS

crates.io docs.rs crates.io

Builds a Rust WebAssembly module at compile time and returns the bytes.

Example

let module = build_wasm!("relative/path/to/module");

This crate provides a wrapper around cargo to build and then include a WebAssembly module at compile time. This is intended for use in unit tests, where the target platform may not be able to invoke cargo itself, for example while using MIRI or when executing the compiled module on the Web.

Toolchain

To use this crate, you must have the wasm32-unknown-unknown nightly toolchain installed, and have the rust-src component. You can install these with the following commands:

rustup target add wasm32-unknown-unknown --toolchain nightly
rustup component add rust-src

Arguments

The build macro allows for an assortment of arguments to be passed to the build command:

let module = build_wasm!{
    path: "relative/path/to/module",
    features: [
        atomics, // Controls if the `atomics` proposal is enabled
        bulk_memory, // Controls if the `bulk-memory` proposal is enabled
        mutable_globals, // Controls if the `mutable-globals` proposal is enabled
    ],
    // Allows additional environment variables to be set while compiling the module.
    env: Env {
        FOO: "bar",
        BAX: 7,
    },
    // Controls if the module should be built in debug or release mode.
    release: true
};

Dependencies

~0.4–0.8MB
~19K SLoC