Skip to content

Commit 9cdb86c

Browse files
authored
Added out_name attribute to rust_wasm_bindgen. (bazelbuild#3086)
1 parent 7743935 commit 9cdb86c

File tree

3 files changed

+19
-33
lines changed

3 files changed

+19
-33
lines changed

docs/src/rust_wasm_bindgen.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ for the `rules_js` submodule).
4747
## rust_wasm_bindgen
4848

4949
<pre>
50-
rust_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-target_arch">target_arch</a>, <a href="#rust_wasm_bindgen-wasm_file">wasm_file</a>)
50+
rust_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-out_name">out_name</a>, <a href="#rust_wasm_bindgen-target">target</a>, <a href="#rust_wasm_bindgen-target_arch">target_arch</a>, <a href="#rust_wasm_bindgen-wasm_file">wasm_file</a>)
5151
</pre>
5252

5353
Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws].
@@ -62,7 +62,8 @@ An example of this rule in use can be seen at [@rules_rust//examples/wasm](../ex
6262
| Name | Description | Type | Mandatory | Default |
6363
| :------------- | :------------- | :------------- | :------------- | :------------- |
6464
| <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 | |
65-
| <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 | `[]` |
65+
| <a id="rust_wasm_bindgen-bindgen_flags"></a>bindgen_flags | Flags to pass directly to the wasm-bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details. | List of strings | optional | `[]` |
66+
| <a id="rust_wasm_bindgen-out_name"></a>out_name | Set a custom output filename (Without extension. Defaults to target name). | String | optional | `""` |
6667
| <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 | `"bundler"` |
6768
| <a id="rust_wasm_bindgen-target_arch"></a>target_arch | The target architecture to use for the wasm-bindgen command line option. | String | optional | `"wasm32"` |
6869
| <a id="rust_wasm_bindgen-wasm_file"></a>wasm_file | The `.wasm` file or crate to generate bindings for. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |

docs/src/rust_wasm_bindgen_rules_js.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Rust WASM-bindgen rules for interfacing with aspect-build/rules_js
77
## js_rust_wasm_bindgen
88

99
<pre>
10-
js_rust_wasm_bindgen(<a href="#js_rust_wasm_bindgen-name">name</a>, <a href="#js_rust_wasm_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#js_rust_wasm_bindgen-target">target</a>, <a href="#js_rust_wasm_bindgen-target_arch">target_arch</a>, <a href="#js_rust_wasm_bindgen-wasm_file">wasm_file</a>)
10+
js_rust_wasm_bindgen(<a href="#js_rust_wasm_bindgen-name">name</a>, <a href="#js_rust_wasm_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#js_rust_wasm_bindgen-out_name">out_name</a>, <a href="#js_rust_wasm_bindgen-target">target</a>, <a href="#js_rust_wasm_bindgen-target_arch">target_arch</a>, <a href="#js_rust_wasm_bindgen-wasm_file">wasm_file</a>)
1111
</pre>
1212

1313
Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws] that interface with [aspect-build/rules_js][abjs].
@@ -22,6 +22,7 @@ Generates javascript and typescript bindings for a webassembly module using [was
2222
| :------------- | :------------- | :------------- | :------------- | :------------- |
2323
| <a id="js_rust_wasm_bindgen-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
2424
| <a id="js_rust_wasm_bindgen-bindgen_flags"></a>bindgen_flags | Flags to pass directly to the wasm-bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details. | List of strings | optional | `[]` |
25+
| <a id="js_rust_wasm_bindgen-out_name"></a>out_name | Set a custom output filename (Without extension. Defaults to target name). | String | optional | `""` |
2526
| <a id="js_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 | `"bundler"` |
2627
| <a id="js_rust_wasm_bindgen-target_arch"></a>target_arch | The target architecture to use for the wasm-bindgen command line option. | String | optional | `"wasm32"` |
2728
| <a id="js_rust_wasm_bindgen-wasm_file"></a>wasm_file | The `.wasm` file or crate to generate bindings for. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |

extensions/wasm_bindgen/private/wasm_bindgen.bzl

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,28 @@ def rust_wasm_bindgen_action(*, ctx, toolchain, wasm_file, target_output, flags
5353
wasm_file,
5454
))
5555

56-
bindgen_wasm_module = ctx.actions.declare_file(ctx.label.name + "_bg.wasm")
56+
out_name = ctx.label.name
57+
if ctx.attr.out_name:
58+
out_name = ctx.attr.out_name
5759

58-
js_out = [ctx.actions.declare_file(ctx.label.name + ".js")]
60+
bindgen_wasm_module = ctx.actions.declare_file(out_name + "_bg.wasm")
61+
62+
js_out = [ctx.actions.declare_file(out_name + ".js")]
5963
ts_out = []
6064
if not "--no-typescript" in flags:
61-
ts_out.append(ctx.actions.declare_file(ctx.label.name + ".d.ts"))
65+
ts_out.append(ctx.actions.declare_file(out_name + ".d.ts"))
6266

6367
if target_output == "bundler":
64-
js_out.append(ctx.actions.declare_file(ctx.label.name + "_bg.js"))
68+
js_out.append(ctx.actions.declare_file(out_name + "_bg.js"))
6569
if not "--no-typescript" in flags:
66-
ts_out.append(ctx.actions.declare_file(ctx.label.name + "_bg.wasm.d.ts"))
70+
ts_out.append(ctx.actions.declare_file(out_name + "_bg.wasm.d.ts"))
6771

6872
outputs = [bindgen_wasm_module] + js_out + ts_out
6973

7074
args = ctx.actions.args()
7175
args.add("--target", target_output)
7276
args.add("--out-dir", bindgen_wasm_module.dirname)
73-
args.add("--out-name", ctx.label.name)
77+
args.add("--out-name", out_name)
7478
args.add_all(flags)
7579
args.add(input_file)
7680

@@ -120,6 +124,9 @@ WASM_BINDGEN_ATTR = {
120124
"bindgen_flags": attr.string_list(
121125
doc = "Flags to pass directly to the wasm-bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details.",
122126
),
127+
"out_name": attr.string(
128+
doc = "Set a custom output filename (Without extension. Defaults to target name).",
129+
),
123130
"target": attr.string(
124131
doc = "The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details.",
125132
default = "bundler",
@@ -150,30 +157,7 @@ Generates javascript and typescript bindings for a webassembly module using [was
150157
151158
An example of this rule in use can be seen at [@rules_rust//examples/wasm](../examples/wasm)
152159
""",
153-
attrs = {
154-
"bindgen_flags": attr.string_list(
155-
doc = "Flags to pass directly to the bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details.",
156-
),
157-
"target": attr.string(
158-
doc = "The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details.",
159-
default = "bundler",
160-
values = ["web", "bundler", "nodejs", "no-modules", "deno"],
161-
),
162-
"target_arch": attr.string(
163-
doc = "The target architecture to use for the wasm-bindgen command line option.",
164-
default = "wasm32",
165-
values = ["wasm32", "wasm64"],
166-
),
167-
"wasm_file": attr.label(
168-
doc = "The `.wasm` file or crate to generate bindings for.",
169-
allow_single_file = True,
170-
cfg = wasm_bindgen_transition,
171-
mandatory = True,
172-
),
173-
"_allowlist_function_transition": attr.label(
174-
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
175-
),
176-
},
160+
attrs = WASM_BINDGEN_ATTR,
177161
toolchains = [
178162
str(Label("//:toolchain_type")),
179163
],

0 commit comments

Comments
 (0)