From ddb40c6da506972af7a111da48485107b116e364 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Fri, 10 Jan 2025 12:22:46 +0000 Subject: [PATCH 1/3] build: migrate `@angular/build` to `npm_package` This allows us to use the package in the RJS pnpm workspace. --- packages/angular/build/BUILD.bazel | 37 +++++++++++++++--------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/packages/angular/build/BUILD.bazel b/packages/angular/build/BUILD.bazel index e7be210f97e3..1c16572f4a96 100644 --- a/packages/angular/build/BUILD.bazel +++ b/packages/angular/build/BUILD.bazel @@ -1,7 +1,6 @@ load("@npm//@angular/build-tooling/bazel/api-golden:index.bzl", "api_golden_test_npm_package") load("@npm//@bazel/jasmine:index.bzl", "jasmine_node_test") -load("//tools:defaults.bzl", "pkg_npm") -load("//tools:interop.bzl", "ts_project") +load("//tools:defaults2.bzl", "npm_package", "ts_project") load("//tools:ts_json_schema.bzl", "ts_json_schema") licenses(["notice"]) @@ -28,6 +27,18 @@ ts_json_schema( src = "src/builders/ng-packagr/schema.json", ) +RUNTIME_ASSETS = glob( + include = [ + "src/**/schema.json", + "src/**/*.js", + "src/**/*.mjs", + "src/**/*.html", + ], +) + [ + "builders.json", + "package.json", +] + ts_project( name = "build", srcs = glob( @@ -47,17 +58,7 @@ ts_project( "//packages/angular/build:src/builders/extract-i18n/schema.ts", "//packages/angular/build:src/builders/ng-packagr/schema.ts", ], - data = glob( - include = [ - "src/**/schema.json", - "src/**/*.js", - "src/**/*.mjs", - "src/**/*.html", - ], - ) + [ - "builders.json", - "package.json", - ], + data = RUNTIME_ASSETS, module_name = "@angular/build", deps = [ "//:root_modules/@ampproject/remapping", @@ -223,17 +224,17 @@ genrule( cmd = "cp $(execpath //:LICENSE) $@", ) -pkg_npm( - name = "npm_package", +npm_package( + name = "pkg", pkg_deps = [ "//packages/angular_devkit/architect:package.json", ], tags = ["release-package"], - deps = [ + deps = RUNTIME_ASSETS + [ ":README.md", - ":build", + ":build_rjs", ":license", - "//packages/angular/build/private", + "//packages/angular/build/private:private_rjs", ], ) From 3e9692c47b294910e99f3dafb31486db2e0edbc8 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Fri, 10 Jan 2025 12:26:24 +0000 Subject: [PATCH 2/3] build: migrate `@angular/cli` to `npm_package` This allows this package to be used in the pnpm workspace, and is a step further with the `rules_js` migration. --- packages/angular/cli/BUILD.bazel | 39 ++++++++++++++++---------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/packages/angular/cli/BUILD.bazel b/packages/angular/cli/BUILD.bazel index 259b82cbeba8..b810bb1a36da 100644 --- a/packages/angular/cli/BUILD.bazel +++ b/packages/angular/cli/BUILD.bazel @@ -4,8 +4,7 @@ # found in the LICENSE file at https://angular.dev/license load("@npm//@bazel/jasmine:index.bzl", "jasmine_node_test") -load("//tools:defaults.bzl", "pkg_npm") -load("//tools:interop.bzl", "ts_project") +load("//tools:defaults2.bzl", "npm_package", "ts_project") load("//tools:ng_cli_schema_generator.bzl", "cli_json_schema") load("//tools:ts_json_schema.bzl", "ts_json_schema") @@ -13,6 +12,19 @@ licenses(["notice"]) package(default_visibility = ["//visibility:public"]) +RUNTIME_ASSETS = glob( + include = [ + "bin/**/*", + "src/**/*.md", + "src/**/*.json", + ], + exclude = [ + "lib/config/workspace-schema.json", + ], +) + [ + "//packages/angular/cli:lib/config/schema.json", +] + ts_project( name = "angular-cli", srcs = glob( @@ -28,18 +40,7 @@ ts_project( "//packages/angular/cli:lib/config/workspace-schema.ts", "//packages/angular/cli:src/commands/update/schematic/schema.ts", ], - data = glob( - include = [ - "bin/**/*", - "src/**/*.md", - "src/**/*.json", - ], - exclude = [ - "lib/config/workspace-schema.json", - ], - ) + [ - "//packages/angular/cli:lib/config/schema.json", - ], + data = RUNTIME_ASSETS, module_name = "@angular/cli", deps = [ "//:root_modules/@angular/core", @@ -159,8 +160,8 @@ genrule( cmd = "cp $(execpath //:LICENSE) $@", ) -pkg_npm( - name = "npm_package", +npm_package( + name = "pkg", pkg_deps = [ "//packages/angular_devkit/architect:package.json", "//packages/angular_devkit/build_angular:package.json", @@ -170,11 +171,9 @@ pkg_npm( "//packages/schematics/angular:package.json", ], tags = ["release-package"], - deps = [ + deps = RUNTIME_ASSETS + [ ":README.md", - ":angular-cli", + ":angular-cli_rjs", ":license", - ":src/commands/update/schematic/collection.json", - ":src/commands/update/schematic/schema.json", ], ) From 920451799c36df731bc3df4dfd57bb08d9a3603e Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Mon, 13 Jan 2025 15:12:40 +0000 Subject: [PATCH 3/3] build: support substituting/stamping files other than `package.json` Similart to `pkg_npm` from `rules_nodejs`, we should have a way to make use of the stamp constants/placeholders throughout individual package files. This is not possible at all with `rules_js`'s `npm_package` rule, nor does it support stamp substitutions out of the box at all. We have our own `expand_template` machinery to substitute `package.json` files of npm archives, but we need to expand this to support arbitrary files inside a package. This will be opt-in for explicitly listed files; which is a good compromise for simplicity of supporting this. This commit adds the necessary functionality and demonstrates the feature by fixing `@angular/build`. --- packages/angular/build/BUILD.bazel | 4 ++++ packages/angular/cli/BUILD.bazel | 3 +++ tools/bazel/npm_package.bzl | 15 ++++++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/angular/build/BUILD.bazel b/packages/angular/build/BUILD.bazel index 1c16572f4a96..9f2e2452543f 100644 --- a/packages/angular/build/BUILD.bazel +++ b/packages/angular/build/BUILD.bazel @@ -229,6 +229,10 @@ npm_package( pkg_deps = [ "//packages/angular_devkit/architect:package.json", ], + stamp_files = [ + "src/tools/esbuild/utils.js", + "src/utils/normalize-cache.js", + ], tags = ["release-package"], deps = RUNTIME_ASSETS + [ ":README.md", diff --git a/packages/angular/cli/BUILD.bazel b/packages/angular/cli/BUILD.bazel index b810bb1a36da..dc960bb8276b 100644 --- a/packages/angular/cli/BUILD.bazel +++ b/packages/angular/cli/BUILD.bazel @@ -170,6 +170,9 @@ npm_package( "//packages/angular_devkit/schematics:package.json", "//packages/schematics/angular:package.json", ], + stamp_files = [ + "src/utilities/version.js", + ], tags = ["release-package"], deps = RUNTIME_ASSETS + [ ":README.md", diff --git a/tools/bazel/npm_package.bzl b/tools/bazel/npm_package.bzl index 0d830c5f96b3..137f629e5981 100644 --- a/tools/bazel/npm_package.bzl +++ b/tools/bazel/npm_package.bzl @@ -13,6 +13,7 @@ def npm_package( deps = [], visibility = None, pkg_deps = [], + stamp_files = [], pkg_json = "package.json", **kwargs): if name != "pkg": @@ -74,11 +75,23 @@ def npm_package( stamp_substitutions = get_npm_package_substitutions_for_rjs(), ) + stamp_targets = [] + for f in stamp_files: + expand_template( + name = "stamp_file_%s" % f, + template = f, + out = "substituted/%s" % f, + substitutions = NO_STAMP_PACKAGE_SUBSTITUTIONS, + stamp_substitutions = get_npm_package_substitutions_for_rjs(), + ) + + stamp_targets.append("stamp_file_%s" % f) + _npm_package( name = "npm_package", visibility = visibility, # Note: Order matters here! Last file takes precedence after replaced prefixes. - srcs = deps + [":final_package_json"], + srcs = deps + stamp_targets + [":final_package_json"], replace_prefixes = { "substituted_final/": "", "substituted_with_tars/": "",