From e31be733b667ae89f5e91922cef086d9aedd4c83 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Mon, 13 Jan 2025 15:12:40 +0000 Subject: [PATCH] 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 1c16572f4a..9f2e245254 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 b810bb1a36..dc960bb827 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 0d830c5f96..137f629e59 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/": "",