Rust Wasm Bindgen

Overview

To build a rust_binary for wasm32-unknown-unknown target add the --platforms=@rules_rust//rust/platform:wasm flag.

bazel build @examples//hello_world_wasm --platforms=@rules_rust//rust/platform:wasm

To build a rust_binary for wasm32-wasi target add the --platforms=@rules_rust//rust/platform:wasi flag.

bazel build @examples//hello_world_wasm --platforms=@rules_rust//rust/platform:wasi

rust_wasm_bindgen will automatically transition to the wasm platform and can be used when building WebAssembly code for the host target.

rust_wasm_bindgen

Generates javascript and typescript bindings for a webassembly module using wasm-bindgen.

To use the Rust WebAssembly bindgen rules, add the following to your WORKSPACE file to add the external repositories for the Rust bindgen toolchain (in addition to the Rust rules setup):

load("@rules_rust//wasm_bindgen:repositories.bzl", "rust_wasm_bindgen_repositories")

rust_wasm_bindgen_repositories()

For more details on rust_wasm_bindgen_repositories, see here.

An example of this rule in use can be seen at @rules_rust//examples/wasm

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
bindgen_flagsFlags to pass directly to the bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details.List of stringsoptional[]
targetThe type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details.Stringoptional“bundler”
wasm_fileThe .wasm file or crate to generate bindings for.Labelrequired

rust_wasm_bindgen_toolchain

The tools required for the rust_wasm_bindgen rule.

In cases where users want to control or change the version of wasm-bindgen used by rust_wasm_bindgen, a unique toolchain can be created as in the example below:

load("@rules_rust//bindgen:bindgen.bzl", "rust_bindgen_toolchain")

rust_bindgen_toolchain(
    bindgen = "//my/cargo_raze:cargo_bin_wasm_bindgen",
)

toolchain(
    name = "wasm_bindgen_toolchain",
    toolchain = "wasm_bindgen_toolchain_impl",
    toolchain_type = "@rules_rust//wasm_bindgen:toolchain_type",
)

Now that you have your own toolchain, you need to register it by inserting the following statement in your WORKSPACE file:

register_toolchains("//my/toolchains:wasm_bindgen_toolchain")

For additional information, see the Bazel toolchains documentation.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
bindgenThe label of a wasm-bindgen-cli executable.LabeloptionalNone

rust_wasm_bindgen_dependencies

Declare dependencies needed for the rules_rust wasm-bindgen rules.

rust_wasm_bindgen_register_toolchains

Registers the default toolchains for the rules_rust wasm-bindgen rules.

PARAMETERS

NameDescriptionDefault Value
register_toolchainsWhether or not to register toolchains.True