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`.
This commit is contained in:
Paul Gschwendtner 2025-01-13 15:12:40 +00:00
parent 5660d05874
commit e31be733b6
3 changed files with 21 additions and 1 deletions

View File

@ -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",

View File

@ -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",

View File

@ -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/": "",