Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for capturing snippets from wasm-bindgen #3087

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading