• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!-- Generated with Stardoc: http://skydoc.bazel.build -->
2# Rust Wasm Bindgen
3
4* [rust_wasm_bindgen_dependencies](#rust_wasm_bindgen_dependencies)
5* [rust_wasm_bindgen_register_toolchains](#rust_wasm_bindgen_register_toolchains)
6* [rust_wasm_bindgen_toolchain](#rust_wasm_bindgen_toolchain)
7* [rust_wasm_bindgen](#rust_wasm_bindgen)
8* [RustWasmBindgenInfo](#RustWasmBindgenInfo)
9
10
11## Overview
12
13Bazel rules for generating wasm modules for Javascript using [wasm-bindgen][wb].
14
15## Setup
16
17To begin using the `wasm-bindgen` rules, users can load the necessary dependencies
18in their workspace by adding the following to their `WORKSPACE.bazel` file.
19
20```starlark
21load("@rules_rust//wasm_bindgen:repositories.bzl", "rust_wasm_bindgen_dependencies", "rust_wasm_bindgen_register_toolchains")
22
23rust_wasm_bindgen_dependencies()
24
25rust_wasm_bindgen_register_toolchains()
26```
27
28This should enable users to start using the [rust_wasm_bindgen](#rust_wasm_bindgen)
29rule. However, it's common to want to control the version of `wasm-bindgen` in the
30workspace instead of relying on the one provided by `rules_rust`. In this case, users
31should avoid calling `rust_wasm_bindgen_register_toolchains` and instead use the
32[rust_wasm_bindgen_toolchain](#rust_wasm_bindgen_toolchain) rule to define their own
33toolchains to register in the workspace.
34
35### Interfacing with Javascript rules
36
37While it's recommended for users to mantain their own , in the
38`@rules_rust//wasm_bindgen` package there exists interface sub-packages for various
39Javascript Bazel rules. E.g. `build_bazel_rules_nodejs` or `aspect_rules_js`. The
40rules defined there are a more convenient way to use `rust_wasm_bindgen` with the
41associated javascript rules due to the inclusion of additional providers. Each
42directory contains a `defs.bzl` file that defines the different variants of
43`rust_wasm_bindgen`. (e.g. `nodejs_rust_wasm_bindgen` for the `rules_nodejs` submodule).
44
45
46[wb]: https://github.com/rustwasm/wasm-bindgen
47
48
49<a id="rust_wasm_bindgen"></a>
50
51## rust_wasm_bindgen
52
53<pre>
54rust_wasm_bindgen(<a href="#rust_wasm_bindgen-name">name</a>, <a href="#rust_wasm_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#rust_wasm_bindgen-target">target</a>, <a href="#rust_wasm_bindgen-wasm_file">wasm_file</a>)
55</pre>
56
57Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws].
58
59[ws]: https://rustwasm.github.io/docs/wasm-bindgen/
60
61An example of this rule in use can be seen at [@rules_rust//examples/wasm](../examples/wasm)
62
63
64**ATTRIBUTES**
65
66
67| Name  | Description | Type | Mandatory | Default |
68| :------------- | :------------- | :------------- | :------------- | :------------- |
69| <a id="rust_wasm_bindgen-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
70| <a id="rust_wasm_bindgen-bindgen_flags"></a>bindgen_flags |  Flags to pass directly to the bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details.   | List of strings | optional | <code>[]</code> |
71| <a id="rust_wasm_bindgen-target"></a>target |  The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details.   | String | optional | <code>"bundler"</code> |
72| <a id="rust_wasm_bindgen-wasm_file"></a>wasm_file |  The <code>.wasm</code> file or crate to generate bindings for.   | <a href="https://bazel.build/concepts/labels">Label</a> | required |  |
73
74
75<a id="rust_wasm_bindgen_toolchain"></a>
76
77## rust_wasm_bindgen_toolchain
78
79<pre>
80rust_wasm_bindgen_toolchain(<a href="#rust_wasm_bindgen_toolchain-name">name</a>, <a href="#rust_wasm_bindgen_toolchain-bindgen">bindgen</a>)
81</pre>
82
83The tools required for the `rust_wasm_bindgen` rule.
84
85In cases where users want to control or change the version of `wasm-bindgen` used by [rust_wasm_bindgen](#rust_wasm_bindgen),
86a unique toolchain can be created as in the example below:
87
88```python
89load("@rules_rust//bindgen:bindgen.bzl", "rust_bindgen_toolchain")
90
91rust_bindgen_toolchain(
92    bindgen = "//3rdparty/crates:wasm_bindgen_cli__bin",
93)
94
95toolchain(
96    name = "wasm_bindgen_toolchain",
97    toolchain = "wasm_bindgen_toolchain_impl",
98    toolchain_type = "@rules_rust//wasm_bindgen:toolchain_type",
99)
100```
101
102Now that you have your own toolchain, you need to register it by
103inserting the following statement in your `WORKSPACE` file:
104
105```python
106register_toolchains("//my/toolchains:wasm_bindgen_toolchain")
107```
108
109For additional information, see the [Bazel toolchains documentation][toolchains].
110
111[toolchains]: https://docs.bazel.build/versions/master/toolchains.html
112
113
114**ATTRIBUTES**
115
116
117| Name  | Description | Type | Mandatory | Default |
118| :------------- | :------------- | :------------- | :------------- | :------------- |
119| <a id="rust_wasm_bindgen_toolchain-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
120| <a id="rust_wasm_bindgen_toolchain-bindgen"></a>bindgen |  The label of a <code>wasm-bindgen-cli</code> executable.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
121
122
123<a id="RustWasmBindgenInfo"></a>
124
125## RustWasmBindgenInfo
126
127<pre>
128RustWasmBindgenInfo(<a href="#RustWasmBindgenInfo-js">js</a>, <a href="#RustWasmBindgenInfo-ts">ts</a>, <a href="#RustWasmBindgenInfo-wasm">wasm</a>)
129</pre>
130
131Info about wasm-bindgen outputs.
132
133**FIELDS**
134
135
136| Name  | Description |
137| :------------- | :------------- |
138| <a id="RustWasmBindgenInfo-js"></a>js |  Depset[File]: The Javascript files produced by <code>wasm-bindgen</code>.    |
139| <a id="RustWasmBindgenInfo-ts"></a>ts |  Depset[File]: The Typescript files produced by <code>wasm-bindgen</code>.    |
140| <a id="RustWasmBindgenInfo-wasm"></a>wasm |  File: The <code>.wasm</code> file generated by <code>wasm-bindgen</code>.    |
141
142
143<a id="rust_wasm_bindgen_dependencies"></a>
144
145## rust_wasm_bindgen_dependencies
146
147<pre>
148rust_wasm_bindgen_dependencies()
149</pre>
150
151Declare dependencies needed for the `rules_rust` [wasm-bindgen][wb] rules.
152
153[wb]: https://github.com/rustwasm/wasm-bindgen
154
155
156
157**RETURNS**
158
159list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories
160  defined by this macro.
161
162
163<a id="rust_wasm_bindgen_register_toolchains"></a>
164
165## rust_wasm_bindgen_register_toolchains
166
167<pre>
168rust_wasm_bindgen_register_toolchains(<a href="#rust_wasm_bindgen_register_toolchains-register_toolchains">register_toolchains</a>)
169</pre>
170
171Registers the default toolchains for the `rules_rust` [wasm-bindgen][wb] rules.
172
173[wb]: https://github.com/rustwasm/wasm-bindgen
174
175
176**PARAMETERS**
177
178
179| Name  | Description | Default Value |
180| :------------- | :------------- | :------------- |
181| <a id="rust_wasm_bindgen_register_toolchains-register_toolchains"></a>register_toolchains |  Whether or not to register toolchains.   |  `True` |
182
183
184