angular/packages/localize/BUILD.bazel
Paul Gschwendtner 1f1039475c feat(bazel): support shared chunks in ng_package (#60241)
Historically we've had to be VERY cautious about the way we import
things between entry-points. That is because the `ng_package` rule
bundling is subject to silently introducing code duplication, breaking
singletons etc. We've had this surface a couple of times already, and
dev-infra tried to help detect such cases by adding safety analysis into
`ng_package`.

Long-term we want to get to an approach where it's easy to simply share
code between chunks. Precisely, with the upcoming `rules_js` migration,
this will be necessary as we will have different import "guidelines"
that would currently, before this commit, result in code duplication, or
trigger our "safety check/lint".

This commit prepares `ng_package` to support relative imports between
entry-points, so that we only need the safety check for cross-package
imports/exports. The result is that `ng_package`/APF is now smartly able
to generate shared chunks for things that are needed between multiple
entry-points. Yay!

Note that those shared chunks still remain private, and are guarded by
our `package.json` "exports"; so no new public API surface is
exposed.

PR Close #60241
2025-03-06 10:29:05 -08:00

93 lines
2.8 KiB
Text

load("//packages/bazel:index.bzl", "types_bundle")
load("//tools:defaults.bzl", "api_golden_test_npm_package", "generate_api_docs", "ng_package", "ts_library")
package(default_visibility = ["//visibility:public"])
ts_library(
name = "localize",
srcs = glob(
[
"*.ts",
"src/**/*.ts",
],
),
module_name = "@angular/localize",
deps = [
"//packages/localize/src/localize",
"//packages/localize/src/utils",
],
)
types_bundle(
name = "api_type_definitions",
entry_point = "localize.d.ts",
output_name = "localize/index.d.ts",
deps = [":localize"],
)
ng_package(
name = "npm_package",
package_name = "@angular/localize",
srcs = [
"package.json",
":api_type_definitions",
],
nested_packages = [
"//packages/localize/schematics:npm_package",
"//packages/localize/tools:npm_package",
],
side_effect_entry_points = [
"@angular/localize/init",
],
skip_type_bundling = [
# For the primary entry-point we disable automatic type bundling because API extractor
# does not properly handle module augmentation.
# To workaround this issue, type bundling is done as a separate step for all types
# except the main `index.d.ts` file which contains the module augmentation. The main
# file is reference in the package.json and also imports the bundle types. The bundled
# types are created via `api_type_definitions` above.
# TODO: Remove once https://github.com/microsoft/rushstack/issues/2090 is solved.
"",
],
tags = [
"release-with-framework",
],
deps = [
":localize",
"//packages/localize/init",
],
)
api_golden_test_npm_package(
name = "localize_api",
data = [
":npm_package",
"//goldens:public-api",
],
golden_dir = "angular/goldens/public-api/localize",
npm_package = "angular/packages/localize/npm_package",
# The logic for the localize function is exported in the primary entry-point, but users
# are not supposed to import it from there. We still want to guard these exports though.
strip_export_pattern = "^ɵ(?!.localize|LocalizeFn|TranslateFn)",
# The tool entry-point uses namespace aliases and API extractor needs to be
# able to resolve `@babel/core` to fully understand the `types` re-export/alias.
types = ["@npm//@types/babel__core"],
)
filegroup(
name = "files_for_docgen",
srcs = glob([
"*.ts",
"src/**/*.ts",
]) + ["PACKAGE.md"],
)
generate_api_docs(
name = "localize_docs",
srcs = [
":files_for_docgen",
"//packages/localize/src/utils:files_for_docgen",
],
entry_point = ":index.ts",
module_name = "@angular/localize",
)