xref: /aosp_15_r20/external/bazelbuild-rules_rust/wasm_bindgen/rules_js/defs.bzl (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1"""Rust WASM-bindgen rules for interfacing with aspect-build/rules_js"""
2
3load("@aspect_rules_js//js:providers.bzl", "js_info")
4load("//wasm_bindgen/private:wasm_bindgen.bzl", "WASM_BINDGEN_ATTR", "rust_wasm_bindgen_action")
5
6def _js_rust_wasm_bindgen_impl(ctx):
7    toolchain = ctx.toolchains[Label("//wasm_bindgen:toolchain_type")]
8
9    info = rust_wasm_bindgen_action(
10        ctx = ctx,
11        toolchain = toolchain,
12        wasm_file = ctx.attr.wasm_file,
13        target_output = ctx.attr.target,
14        bindgen_flags = ctx.attr.bindgen_flags,
15    )
16
17    # Return a structure that is compatible with the deps[] of a ts_library.
18    declarations = info.ts
19    es5_sources = info.js
20
21    return [
22        DefaultInfo(
23            files = depset([info.wasm], transitive = [info.js, info.ts]),
24        ),
25        info,
26        js_info(
27            declarations = declarations,
28            sources = es5_sources,
29            transitive_declarations = declarations,
30            transitive_sources = es5_sources,
31        ),
32    ]
33
34js_rust_wasm_bindgen = rule(
35    doc = """\
36Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws] that interface with [aspect-build/rules_js][abjs].
37
38[ws]: https://rustwasm.github.io/docs/wasm-bindgen/
39[abjs]: https://github.com/aspect-build/rules_js
40
41An example of this rule in use can be seen at [@rules_rust//examples/wasm_bindgen/rules_js](../examples/wasm_bindgen/rules_js)
42""",
43    implementation = _js_rust_wasm_bindgen_impl,
44    attrs = WASM_BINDGEN_ATTR,
45    toolchains = [
46        str(Label("//wasm_bindgen:toolchain_type")),
47    ],
48)
49