Skip to content

Commit

Permalink
Added support for capturing snippets from wasm-bindgen (#3087)
Browse files Browse the repository at this point in the history
This change also adds `*_bg.wasm.d.ts` files to `target = "web"` uses of
`rust_wasm_bindgen` since this was being output by `wasm-bindgen` but
not collected.
  • Loading branch information
UebelAndre authored Dec 16, 2024
1 parent 8cedb93 commit f7be79c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
4 changes: 3 additions & 1 deletion docs/src/rust_wasm_bindgen.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ For additional information, see the [Bazel toolchains documentation][toolchains]
## RustWasmBindgenInfo

<pre>
RustWasmBindgenInfo(<a href="#RustWasmBindgenInfo-js">js</a>, <a href="#RustWasmBindgenInfo-ts">ts</a>, <a href="#RustWasmBindgenInfo-wasm">wasm</a>)
RustWasmBindgenInfo(<a href="#RustWasmBindgenInfo-js">js</a>, <a href="#RustWasmBindgenInfo-root">root</a>, <a href="#RustWasmBindgenInfo-snippets">snippets</a>, <a href="#RustWasmBindgenInfo-ts">ts</a>, <a href="#RustWasmBindgenInfo-wasm">wasm</a>)
</pre>

Info about wasm-bindgen outputs.
Expand All @@ -132,6 +132,8 @@ Info about wasm-bindgen outputs.
| Name | Description |
| :------------- | :------------- |
| <a id="RustWasmBindgenInfo-js"></a>js | Depset[File]: The Javascript files produced by `wasm-bindgen`. |
| <a id="RustWasmBindgenInfo-root"></a>root | str: The path to the root of the `wasm-bindgen --out-dir` directory. |
| <a id="RustWasmBindgenInfo-snippets"></a>snippets | File: The snippets directory produced by `wasm-bindgen`. |
| <a id="RustWasmBindgenInfo-ts"></a>ts | Depset[File]: The Typescript files produced by `wasm-bindgen`. |
| <a id="RustWasmBindgenInfo-wasm"></a>wasm | File: The `.wasm` file generated by `wasm-bindgen`. |

Expand Down
24 changes: 17 additions & 7 deletions extensions/wasm_bindgen/private/wasm_bindgen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,24 @@ def rust_wasm_bindgen_action(*, ctx, toolchain, wasm_file, target_output, flags
if ctx.attr.out_name:
out_name = ctx.attr.out_name

bindgen_wasm_module = ctx.actions.declare_file(out_name + "_bg.wasm")
bindgen_wasm_module = ctx.actions.declare_file("{}/{}_bg.wasm".format(ctx.label.name, out_name))
snippets = ctx.actions.declare_directory("{}/snippets".format(ctx.label.name))

js_out = [ctx.actions.declare_file(out_name + ".js")]
js_out = [ctx.actions.declare_file("{}/{}.js".format(ctx.label.name, out_name))]
ts_out = []
if not "--no-typescript" in flags:
ts_out.append(ctx.actions.declare_file(out_name + ".d.ts"))
ts_out.append(ctx.actions.declare_file("{}/{}.d.ts".format(ctx.label.name, out_name)))

if target_output == "bundler":
js_out.append(ctx.actions.declare_file(out_name + "_bg.js"))
js_out.append(ctx.actions.declare_file("{}/{}_bg.js".format(ctx.label.name, out_name)))
if not "--no-typescript" in flags:
ts_out.append(ctx.actions.declare_file(out_name + "_bg.wasm.d.ts"))
ts_out.append(ctx.actions.declare_file("{}/{}_bg.wasm.d.ts".format(ctx.label.name, out_name)))

outputs = [bindgen_wasm_module] + js_out + ts_out
elif target_output == "web":
if not "--no-typescript" in flags:
ts_out.append(ctx.actions.declare_file("{}/{}_bg.wasm.d.ts".format(ctx.label.name, out_name)))

outputs = [bindgen_wasm_module, snippets] + js_out + ts_out

args = ctx.actions.args()
args.add("--target", target_output)
Expand All @@ -92,6 +97,8 @@ def rust_wasm_bindgen_action(*, ctx, toolchain, wasm_file, target_output, flags
wasm = bindgen_wasm_module,
js = depset(js_out),
ts = depset(ts_out),
snippets = snippets,
root = bindgen_wasm_module.dirname,
)

def _rust_wasm_bindgen_impl(ctx):
Expand All @@ -107,7 +114,10 @@ def _rust_wasm_bindgen_impl(ctx):

providers = [
DefaultInfo(
files = depset([info.wasm], transitive = [info.js, info.ts]),
files = depset(
[info.wasm, info.snippets],
transitive = [info.js, info.ts],
),
),
info,
]
Expand Down
2 changes: 2 additions & 0 deletions extensions/wasm_bindgen/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ RustWasmBindgenInfo = provider(
doc = "Info about wasm-bindgen outputs.",
fields = {
"js": "Depset[File]: The Javascript files produced by `wasm-bindgen`.",
"root": "str: The path to the root of the `wasm-bindgen --out-dir` directory.",
"snippets": "File: The snippets directory produced by `wasm-bindgen`.",
"ts": "Depset[File]: The Typescript files produced by `wasm-bindgen`.",
"wasm": "File: The `.wasm` file generated by `wasm-bindgen`.",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const main = async function (typ, dir) {
"..",
dir,
"test",
"hello_world_" + typ + "_wasm_bindgen",
"hello_world_" + typ + "_wasm_bindgen_bg.wasm",
);
const buf = fs.readFileSync(wasm_file);
Expand Down

0 comments on commit f7be79c

Please sign in to comment.