diff --git a/docs/src/rust_wasm_bindgen.md b/docs/src/rust_wasm_bindgen.md index 8f5cc409f3..b1dda7509b 100644 --- a/docs/src/rust_wasm_bindgen.md +++ b/docs/src/rust_wasm_bindgen.md @@ -121,7 +121,7 @@ For additional information, see the [Bazel toolchains documentation][toolchains] ## RustWasmBindgenInfo
-RustWasmBindgenInfo(js, ts, wasm)
+RustWasmBindgenInfo(js, root, snippets, ts, wasm)
 
Info about wasm-bindgen outputs. @@ -132,6 +132,8 @@ Info about wasm-bindgen outputs. | Name | Description | | :------------- | :------------- | | 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`. | diff --git a/extensions/wasm_bindgen/private/wasm_bindgen.bzl b/extensions/wasm_bindgen/private/wasm_bindgen.bzl index 667b254506..9d421e536f 100644 --- a/extensions/wasm_bindgen/private/wasm_bindgen.bzl +++ b/extensions/wasm_bindgen/private/wasm_bindgen.bzl @@ -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) @@ -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): @@ -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, ] diff --git a/extensions/wasm_bindgen/providers.bzl b/extensions/wasm_bindgen/providers.bzl index 06e434e894..e7eb8d40a4 100644 --- a/extensions/wasm_bindgen/providers.bzl +++ b/extensions/wasm_bindgen/providers.bzl @@ -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`.", }, diff --git a/extensions/wasm_bindgen/rules_js/test/hello_world_wasm_test.js b/extensions/wasm_bindgen/rules_js/test/hello_world_wasm_test.js index 6d3f80c7d9..61fc383955 100644 --- a/extensions/wasm_bindgen/rules_js/test/hello_world_wasm_test.js +++ b/extensions/wasm_bindgen/rules_js/test/hello_world_wasm_test.js @@ -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);