diff --git a/packages/compiler-cli/BUILD.bazel b/packages/compiler-cli/BUILD.bazel index 149c9f57790..7ed45e5dca5 100644 --- a/packages/compiler-cli/BUILD.bazel +++ b/packages/compiler-cli/BUILD.bazel @@ -1,5 +1,6 @@ load("//tools:defaults.bzl", "api_golden_test", "pkg_npm", "ts_config", "ts_library") load("@npm//@bazel/esbuild:index.bzl", "esbuild", "esbuild_config") +load(":extract_typings_rule.bzl", "extract_typings") # Load ng_perf_flag explicitly from ng_perf.bzl as it's private API, and not exposed to other # consumers of @angular/bazel. @@ -105,6 +106,11 @@ ts_library( ], ) +extract_typings( + name = "api_type_definitions", + deps = PUBLIC_TARGETS, +) + pkg_npm( name = "npm_package", srcs = [ @@ -119,11 +125,10 @@ pkg_npm( "//integration:__pkg__", "//packages/compiler-cli/integrationtest:__pkg__", ], - # Note: We add `PUBLIC_TARGETS` here to pull in the type definitions into - # the NPM package. This also brings in the devmode ES5 sources currently. - # These are not referenced anywhere but we would want to only include types. - # TODO(devversion): add support for including only type definitions here. - deps = [":bundles"] + PUBLIC_TARGETS, + deps = [ + ":api_type_definitions", + ":bundles", + ], ) api_golden_test( diff --git a/packages/compiler-cli/extract_typings_rule.bzl b/packages/compiler-cli/extract_typings_rule.bzl new file mode 100644 index 00000000000..0cb80c1d509 --- /dev/null +++ b/packages/compiler-cli/extract_typings_rule.bzl @@ -0,0 +1,25 @@ +"""Starlark file that exposes a rule for extracting type definitions of dependencies.""" + +load("@build_bazel_rules_nodejs//:providers.bzl", "DeclarationInfo") + +def _extract_typings_rule_impl(ctx): + """Implementation of the `extract_typings` rule.""" + transitive_depsets = [] + + for dep in ctx.attr.deps: + # Based on whether declarations should be collected, extract direct + # and transitive declaration files using the `DeclarationInfo` provider. + if DeclarationInfo in dep: + transitive_depsets.append(dep[DeclarationInfo].transitive_declarations) + + return [DefaultInfo(files = depset(transitive = transitive_depsets))] + +extract_typings = rule( + implementation = _extract_typings_rule_impl, + doc = """Rule that extracts all transitive typings of dependencies""", + attrs = { + "deps": attr.label_list( + allow_files = True, + ), + }, +)