Skip to content

Commit

Permalink
feat(bazel): add rule for extracting type definitions from targets (#430
Browse files Browse the repository at this point in the history
)

Adds a rule that collects all direct and transitive type definitions of specified targets. The
definitons are then exposed through the `DefaultInfo` provider, allowing for easy
consumption in other rules.

This rule can be helpful if TypeScript typings are shipped along with bundled runtime code.

PR Close #430
  • Loading branch information
devversion authored and josephperrott committed Feb 25, 2022
1 parent b710e5b commit 53786a3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions bazel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ filegroup(
"BUILD.bazel",
"expand_template.bzl",
"extract_js_module_output.bzl",
"extract_types.bzl",
"//bazel/api-golden:files",
"//bazel/benchmark:files",
"//bazel/browsers:files",
Expand Down
33 changes: 33 additions & 0 deletions bazel/extract_types.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
load("@build_bazel_rules_nodejs//:providers.bzl", "DeclarationInfo")

def _extract_types_impl(ctx):
"""Implementation of the `extract_types` rule."""
depsets = []

for dep in ctx.attr.deps:
if DeclarationInfo in dep:
depsets.append(dep[DeclarationInfo].transitive_declarations)

types = depset(transitive = depsets)

return [
DefaultInfo(files = types),
]

extract_types = rule(
implementation = _extract_types_impl,
doc = """
Rule that collects all direct and transitive type definitions of specified targets. The
definitons are then exposed through the `DefaultInfo` provider, allowing for easy
consumption in other rules.
This rule can be helpful if TypeScript typings are shipped along with bundled runtime code.
""",
attrs = {
"deps": attr.label_list(
doc = """List of targets for which all direct and transitive type definitions
should be collected.""",
mandatory = True,
),
},
)

0 comments on commit 53786a3

Please sign in to comment.