Skip to content

Commit 676ce57

Browse files
devversionalan-agius4
authored andcommitted
build: ensure package substitutions work with pnpm workspace:* links
We'll be using `workspace:*` protocol links to link first-party packages together in the pnpm workspace. For this reason, we need to make sure they are properly replaced in `package.json` files, before packaging.
1 parent 7afc051 commit 676ce57

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

Diff for: tools/bazel/npm_package.bzl

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ load("@aspect_rules_js//npm:defs.bzl", _npm_package = "npm_package")
66
load("@rules_pkg//:pkg.bzl", "pkg_tar")
77
load("//tools:link_package_json_to_tarballs.bzl", "link_package_json_to_tarballs")
88
load("//tools:snapshot_repo_filter.bzl", "SNAPSHOT_REPO_JQ_FILTER")
9-
load("//tools:substitutions.bzl", "NO_STAMP_PACKAGE_SUBSTITUTIONS", "get_npm_package_substitutions_for_rjs")
9+
load("//tools:substitutions.bzl", "substitutions")
1010

1111
def npm_package(
1212
name,
@@ -71,8 +71,8 @@ def npm_package(
7171
"//conditions:default": "substituted/package.json",
7272
}),
7373
out = "substituted_final/package.json",
74-
substitutions = NO_STAMP_PACKAGE_SUBSTITUTIONS,
75-
stamp_substitutions = get_npm_package_substitutions_for_rjs(),
74+
substitutions = substitutions["rjs"]["nostamp"],
75+
stamp_substitutions = substitutions["rjs"]["stamp"],
7676
)
7777

7878
stamp_targets = []
@@ -81,8 +81,8 @@ def npm_package(
8181
name = "stamp_file_%s" % f,
8282
template = f,
8383
out = "substituted/%s" % f,
84-
substitutions = NO_STAMP_PACKAGE_SUBSTITUTIONS,
85-
stamp_substitutions = get_npm_package_substitutions_for_rjs(),
84+
substitutions = substitutions["rjs"]["nostamp"],
85+
stamp_substitutions = substitutions["rjs"]["stamp"],
8686
)
8787

8888
stamp_targets.append("stamp_file_%s" % f)

Diff for: tools/defaults.bzl

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ load("@npm//@angular/build-tooling/bazel:extract_js_module_output.bzl", "extract
1010
load("@rules_pkg//:pkg.bzl", "pkg_tar")
1111
load("//tools:link_package_json_to_tarballs.bzl", "link_package_json_to_tarballs")
1212
load("//tools:snapshot_repo_filter.bzl", "SNAPSHOT_REPO_JQ_FILTER")
13-
load("//tools:substitutions.bzl", "NO_STAMP_PACKAGE_SUBSTITUTIONS", "NPM_PACKAGE_SUBSTITUTIONS")
13+
load("//tools:substitutions.bzl", "substitutions")
1414

1515
_DEFAULT_TSCONFIG_NG = "//:tsconfig-build-ng"
1616
_DEFAULT_TSCONFIG_TEST = "//:tsconfig-test.json"
@@ -159,8 +159,8 @@ def pkg_npm(name, pkg_deps = [], use_prodmode_output = False, **kwargs):
159159
package_name = None,
160160
validate = False,
161161
substitutions = select({
162-
"//:stamp": NPM_PACKAGE_SUBSTITUTIONS,
163-
"//conditions:default": NO_STAMP_PACKAGE_SUBSTITUTIONS,
162+
"//:stamp": substitutions["legacy"]["stamp"],
163+
"//conditions:default": substitutions["legacy"]["nostamp"],
164164
}),
165165
visibility = visibility,
166166
nested_packages = nested_packages,
@@ -221,8 +221,8 @@ def ng_package(deps = [], **kwargs):
221221
deps = deps,
222222
license = "//:LICENSE",
223223
substitutions = select({
224-
"//:stamp": NPM_PACKAGE_SUBSTITUTIONS,
225-
"//conditions:default": NO_STAMP_PACKAGE_SUBSTITUTIONS,
224+
"//:stamp": substitutions["legacy"]["stamp"],
225+
"//conditions:default": substitutions["legacy"]["nostamp"],
226226
}),
227227
**kwargs
228228
)

Diff for: tools/package_json_release_filter.jq

+5
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@
3030

3131
# Add engines; versions substituted via pkg_npm
3232
+ {"engines": {"node": "0.0.0-ENGINES-NODE", "npm": "0.0.0-ENGINES-NPM", "yarn": "0.0.0-ENGINES-YARN"}}
33+
34+
# Remove all `workspace:` pnpm prefixes. Afterwards we can conveniently rely on
35+
# substitutions from the stamp values. Note that we are doing it this way because
36+
# substitutions can apply to multiple files, and `workspace:` can't be reliably replaced.
37+
| walk(if type == "string" and startswith("workspace:") then sub("workspace:"; "") else . end)

Diff for: tools/substitutions.bzl

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
load("//:constants.bzl", "RELEASE_ENGINES_NODE", "RELEASE_ENGINES_NPM", "RELEASE_ENGINES_YARN")
22

3-
NPM_PACKAGE_SUBSTITUTIONS = {
3+
_stamp_substitutions = {
44
# Version of the local package being built, generated via the `--workspace_status_command` flag.
55
"0.0.0-PLACEHOLDER": "{STABLE_PROJECT_VERSION}",
66
"0.0.0-EXPERIMENTAL-PLACEHOLDER": "{STABLE_PROJECT_EXPERIMENTAL_VERSION}",
7+
# ---
78
"BUILD_SCM_HASH-PLACEHOLDER": "{BUILD_SCM_ABBREV_HASH}",
89
"0.0.0-ENGINES-NODE": RELEASE_ENGINES_NODE,
910
"0.0.0-ENGINES-NPM": RELEASE_ENGINES_NPM,
@@ -12,15 +13,26 @@ NPM_PACKAGE_SUBSTITUTIONS = {
1213
"\\./(.+)/packages/angular/ssr/third_party/beasties": "../third_party/beasties/index.js",
1314
}
1415

15-
NO_STAMP_PACKAGE_SUBSTITUTIONS = dict(NPM_PACKAGE_SUBSTITUTIONS, **{
16+
_no_stamp_substitutions = dict(_stamp_substitutions, **{
1617
"0.0.0-PLACEHOLDER": "0.0.0",
1718
"0.0.0-EXPERIMENTAL-PLACEHOLDER": "0.0.0",
1819
})
1920

20-
def get_npm_package_substitutions_for_rjs():
21+
def _adjust_substitutions_for_rules_js(subs):
2122
result = {}
22-
for key, value in NPM_PACKAGE_SUBSTITUTIONS.items():
23+
for key, value in subs.items():
2324
# in `rules_js`, or `expand_template` from `bazel-lib`, stamp variables
2425
# can only be retrieved via `{{X}}` syntax.
2526
result[key] = value.replace("{", "{{").replace("}", "}}")
2627
return result
28+
29+
substitutions = {
30+
"legacy": {
31+
"stamp": _stamp_substitutions,
32+
"nostamp": _no_stamp_substitutions,
33+
},
34+
"rjs": {
35+
"stamp": _adjust_substitutions_for_rules_js(_stamp_substitutions),
36+
"nostamp": _adjust_substitutions_for_rules_js(_no_stamp_substitutions),
37+
},
38+
}

0 commit comments

Comments
 (0)