Skip to content

Commit 80813ca

Browse files
committed
build: add import maps extract_api_to_json
In order to get the correct types for the aria entries in the API json, we need to map external packages. fixes #angular/angular#65566
1 parent 21cc21e commit 80813ca

File tree

10 files changed

+68
-20
lines changed

10 files changed

+68
-20
lines changed

src/aria/accordion/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
load("//tools:defaults.bzl", "ng_project", "ng_web_test_suite")
2-
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")
1+
load("//tools:defaults.bzl", "extract_api_to_json", "ng_project", "ng_web_test_suite")
32

43
package(default_visibility = ["//visibility:public"])
54

src/aria/combobox/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
load("//tools:defaults.bzl", "ng_project", "ng_web_test_suite", "ts_project")
2-
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")
1+
load("//tools:defaults.bzl", "extract_api_to_json", "ng_project", "ng_web_test_suite", "ts_project")
32

43
package(default_visibility = ["//visibility:public"])
54

src/aria/grid/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
load("//tools:defaults.bzl", "ng_project")
2-
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")
1+
load("//tools:defaults.bzl", "extract_api_to_json", "ng_project")
32

43
package(default_visibility = ["//visibility:public"])
54

src/aria/listbox/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
load("//tools:defaults.bzl", "ng_project", "ng_web_test_suite")
2-
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")
1+
load("//tools:defaults.bzl", "extract_api_to_json", "ng_project", "ng_web_test_suite")
32

43
package(default_visibility = ["//visibility:public"])
54

src/aria/menu/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
load("//tools:defaults.bzl", "ng_project", "ng_web_test_suite")
2-
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")
1+
load("//tools:defaults.bzl", "extract_api_to_json", "ng_project", "ng_web_test_suite")
32

43
package(default_visibility = ["//visibility:public"])
54

src/aria/tabs/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
load("//tools:defaults.bzl", "ng_project", "ng_web_test_suite")
2-
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")
1+
load("//tools:defaults.bzl", "extract_api_to_json", "ng_project", "ng_web_test_suite")
32

43
package(default_visibility = ["//visibility:public"])
54

src/aria/toolbar/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
load("//tools:defaults.bzl", "ng_project", "ng_web_test_suite", "ts_project")
2-
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")
1+
load("//tools:defaults.bzl", "extract_api_to_json", "ng_project", "ng_web_test_suite", "ts_project")
32

43
package(default_visibility = ["//visibility:public"])
54

src/aria/tree/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
load("//tools:defaults.bzl", "ng_project", "ng_web_test_suite")
2-
load("//tools/adev-api-extraction:extract_api_to_json.bzl", "extract_api_to_json")
1+
load("//tools:defaults.bzl", "extract_api_to_json", "ng_project", "ng_web_test_suite")
32

43
package(default_visibility = ["//visibility:public"])
54

tools/adev-api-extraction/extract_api_to_json.bzl

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("@aspect_rules_js//js:providers.bzl", "JsInfo")
2+
13
def _extract_api_to_json(ctx):
24
"""Implementation of the extract_api_to_json rule"""
35

@@ -35,11 +37,43 @@ def _extract_api_to_json(ctx):
3537
# specifying them
3638
# https://github.com/bazelbuild/rules_nodejs/blob/5.x/internal/linker/link_node_modules.bzl#L236
3739
path_map = {}
40+
import_map_files = []
3841
for target, path in ctx.attr.import_map.items():
3942
files = target.files.to_list()
40-
if len(files) != 1:
41-
fail("Expected a single file in import_map target %s" % target.label)
42-
path_map[path] = files[0].path
43+
44+
# Include transitive declarations if available in JsInfo
45+
if JsInfo in target:
46+
files.extend(target[JsInfo].transitive_types.to_list())
47+
48+
import_map_files.extend(files)
49+
if len(files) == 1:
50+
path_map[path] = files[0].path
51+
else:
52+
found_path = None
53+
for f in files:
54+
if f.path.endswith("/node_modules/" + path):
55+
found_path = f.path
56+
break
57+
58+
# Handle @angular package subentries
59+
if path.startswith("@angular/"):
60+
parts = path.split("/")
61+
if len(parts) > 2:
62+
pkg_name = "/".join(parts[:2])
63+
if f.path.endswith("/node_modules/" + pkg_name):
64+
subentry = parts[-1]
65+
found_path = f.path + "/types/" + subentry + ".d.ts"
66+
break
67+
68+
if not found_path:
69+
candidates = [f for f in files if f.path.endswith("/index.d.ts")]
70+
sorted_candidates = sorted(candidates, key = lambda f: len(f.path))
71+
found_path = sorted_candidates[0].path
72+
73+
if found_path:
74+
path_map[path] = found_path
75+
else:
76+
fail("Expected a single file in import_map target %s, but found %s. Could not determine entry point. Files: %s" % (target.label, len(files), [f.path for f in files]))
4377
args.add(json.encode(path_map))
4478

4579
# Pass the set of (optional) extra entries
@@ -48,7 +82,7 @@ def _extract_api_to_json(ctx):
4882
# Define an action that runs the nodejs_binary executable. This is
4983
# the main thing that this rule does.
5084
ctx.actions.run(
51-
inputs = depset(ctx.files.srcs + ctx.files.extra_entries),
85+
inputs = depset(ctx.files.srcs + ctx.files.extra_entries + import_map_files),
5286
executable = ctx.executable._extract_api_to_json,
5387
outputs = [json_output],
5488
arguments = [args],

tools/defaults.bzl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ load("@rules_pkg//:pkg.bzl", "pkg_tar")
1414
load("@rules_sass//src:index.bzl", _sass_binary = "sass_binary", _sass_library = "sass_library")
1515
load("//:packages.bzl", "NO_STAMP_NPM_PACKAGE_SUBSTITUTIONS", "NPM_PACKAGE_SUBSTITUTIONS")
1616
load("//:pkg-externals.bzl", "PKG_EXTERNALS")
17+
load("//tools/adev-api-extraction:extract_api_to_json.bzl", _extract_api_to_json = "extract_api_to_json")
1718
load("//tools/bazel:ng_package_interop.bzl", "ng_package_interop")
1819
load("//tools/bazel:web_test_suite.bzl", _ng_web_test_suite = "ng_web_test_suite")
1920
load("//tools/extract-tokens:index.bzl", _extract_tokens = "extract_tokens")
@@ -247,3 +248,24 @@ def protractor_web_test_suite(name, deps, **kwargs):
247248
],
248249
**kwargs
249250
)
251+
252+
def extract_api_to_json(**kwargs):
253+
if "core_interop" not in native.existing_rules():
254+
native.alias(
255+
name = "core_interop",
256+
actual = "//:node_modules/@angular/core",
257+
)
258+
259+
_extract_api_to_json(
260+
# We need to specify import mappings for Angular packages that import other Angular
261+
# packages.
262+
import_map = {
263+
# We only need to specify top-level entry-points, and only those that
264+
# are imported from other packages.
265+
"//:node_modules/@angular/core": "@angular/core",
266+
":core_interop": "@angular/core/rxjs-interop",
267+
"//src/cdk/bidi": "@angular/cdk/bidi",
268+
"//src/aria/private": "@angular/aria/private",
269+
},
270+
**kwargs
271+
)

0 commit comments

Comments
 (0)