rules_rust_wasm_bindgen
Bazel rules for generating wasm modules for Javascript using wasm-bindgen.
Rules
Setup
To begin using the wasm-bindgen rules, users can load the necessary dependencies
in their workspace by adding the following to their WORKSPACE.bazel file.
load("@rules_rust_wasm_bindgen//:repositories.bzl", "rust_wasm_bindgen_dependencies", "rust_wasm_bindgen_register_toolchains")
rust_wasm_bindgen_dependencies()
rust_wasm_bindgen_register_toolchains()
This should enable users to start using the rust_wasm_bindgen
rule. However, it's common to want to control the version of wasm-bindgen in the
workspace instead of relying on the one provided by rules_rust. In this case, users
should avoid calling rust_wasm_bindgen_register_toolchains and instead use the
rust_wasm_bindgen_toolchain rule to define their own
toolchains to register in the workspace.
Interfacing with Javascript rules
Rules for doing so can be found at rules_js_rust_wasm_bindgen
rust_wasm_bindgen
load("@rules_rust_wasm_bindgen//:defs.bzl", "rust_wasm_bindgen")
rust_wasm_bindgen(name, bindgen_flags, out_name, target, target_arch, wasm_file)
Generates javascript and typescript bindings for a webassembly module using wasm-bindgen.
An example of this rule in use can be seen at @rules_rust//examples/wasm
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| bindgen_flags | Flags to pass directly to the wasm-bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details. | List of strings | optional | [] |
| out_name | Set a custom output filename (Without extension. Defaults to target name). | String | optional | "" |
| target | The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details. | String | optional | "bundler" |
| target_arch | The target architecture to use for the wasm-bindgen command line option. | String | optional | "wasm32" |
| wasm_file | The .wasm crate to generate bindings for. | Label | required |
rust_wasm_bindgen_toolchain
load("@rules_rust_wasm_bindgen//:defs.bzl", "rust_wasm_bindgen_toolchain")
rust_wasm_bindgen_toolchain(name, browser, browser_type, wasm_bindgen_cli, wasm_bindgen_test,
wasm_bindgen_test_runner, webdriver, webdriver_args, webdriver_json)
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_wasm_bindgen//:defs.bzl", "rust_wasm_bindgen_toolchain")
rust_wasm_bindgen_toolchain(
wasm_bindgen_cli = "//3rdparty/crates:wasm_bindgen_cli__bin",
)
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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| browser | The browser entrypoint. | Label | optional | None |
| browser_type | The type of browser provided. | String | optional | "" |
| wasm_bindgen_cli | The label of a wasm-bindgen-cli executable. | Label | optional | None |
| wasm_bindgen_test | The label of a wasm-bindgen-test crate. | Label | optional | None |
| wasm_bindgen_test_runner | The label of a wasm-bindgen-test-runner binary. | Label | optional | None |
| webdriver | The webdriver to use. | Label | optional | None |
| webdriver_args | Arguments to pass to the webdriver binary. | List of strings | optional | [] |
| webdriver_json | The webdriver.json config file for wasm-bindgen-test. | Label | optional | None |
RustWasmBindgenInfo
load("@rules_rust_wasm_bindgen//:defs.bzl", "RustWasmBindgenInfo")
RustWasmBindgenInfo(js, root, snippets, ts, wasm)
Info about wasm-bindgen outputs.
FIELDS
rust_wasm_bindgen_test
load("@rules_rust_wasm_bindgen//:defs.bzl", "rust_wasm_bindgen_test")
rust_wasm_bindgen_test(*, name, aliases, compile_data, crate_features, data, edition, env,
env_inherit, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags,
target_arch, version, wasm, tags, **kwargs)
"A test rule for running wasm-bindgen tests."
PARAMETERS