Skip to content

Commit e31be73

Browse files
committed
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`.
1 parent 5660d05 commit e31be73

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

packages/angular/build/BUILD.bazel

+4
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ npm_package(
229229
pkg_deps = [
230230
"//packages/angular_devkit/architect:package.json",
231231
],
232+
stamp_files = [
233+
"src/tools/esbuild/utils.js",
234+
"src/utils/normalize-cache.js",
235+
],
232236
tags = ["release-package"],
233237
deps = RUNTIME_ASSETS + [
234238
":README.md",

packages/angular/cli/BUILD.bazel

+3
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ npm_package(
170170
"//packages/angular_devkit/schematics:package.json",
171171
"//packages/schematics/angular:package.json",
172172
],
173+
stamp_files = [
174+
"src/utilities/version.js",
175+
],
173176
tags = ["release-package"],
174177
deps = RUNTIME_ASSETS + [
175178
":README.md",

tools/bazel/npm_package.bzl

+14-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def npm_package(
1313
deps = [],
1414
visibility = None,
1515
pkg_deps = [],
16+
stamp_files = [],
1617
pkg_json = "package.json",
1718
**kwargs):
1819
if name != "pkg":
@@ -74,11 +75,23 @@ def npm_package(
7475
stamp_substitutions = get_npm_package_substitutions_for_rjs(),
7576
)
7677

78+
stamp_targets = []
79+
for f in stamp_files:
80+
expand_template(
81+
name = "stamp_file_%s" % f,
82+
template = f,
83+
out = "substituted/%s" % f,
84+
substitutions = NO_STAMP_PACKAGE_SUBSTITUTIONS,
85+
stamp_substitutions = get_npm_package_substitutions_for_rjs(),
86+
)
87+
88+
stamp_targets.append("stamp_file_%s" % f)
89+
7790
_npm_package(
7891
name = "npm_package",
7992
visibility = visibility,
8093
# Note: Order matters here! Last file takes precedence after replaced prefixes.
81-
srcs = deps + [":final_package_json"],
94+
srcs = deps + stamp_targets + [":final_package_json"],
8295
replace_prefixes = {
8396
"substituted_final/": "",
8497
"substituted_with_tars/": "",

0 commit comments

Comments
 (0)