Skip to content

Commit

Permalink
build: ensure package substitutions work with pnpm workspace:* links
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
devversion authored and alan-agius4 committed Jan 20, 2025
1 parent 7afc051 commit 676ce57
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
10 changes: 5 additions & 5 deletions tools/bazel/npm_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ load("@aspect_rules_js//npm:defs.bzl", _npm_package = "npm_package")
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("//tools:link_package_json_to_tarballs.bzl", "link_package_json_to_tarballs")
load("//tools:snapshot_repo_filter.bzl", "SNAPSHOT_REPO_JQ_FILTER")
load("//tools:substitutions.bzl", "NO_STAMP_PACKAGE_SUBSTITUTIONS", "get_npm_package_substitutions_for_rjs")
load("//tools:substitutions.bzl", "substitutions")

def npm_package(
name,
Expand Down Expand Up @@ -71,8 +71,8 @@ def npm_package(
"//conditions:default": "substituted/package.json",
}),
out = "substituted_final/package.json",
substitutions = NO_STAMP_PACKAGE_SUBSTITUTIONS,
stamp_substitutions = get_npm_package_substitutions_for_rjs(),
substitutions = substitutions["rjs"]["nostamp"],
stamp_substitutions = substitutions["rjs"]["stamp"],
)

stamp_targets = []
Expand All @@ -81,8 +81,8 @@ def npm_package(
name = "stamp_file_%s" % f,
template = f,
out = "substituted/%s" % f,
substitutions = NO_STAMP_PACKAGE_SUBSTITUTIONS,
stamp_substitutions = get_npm_package_substitutions_for_rjs(),
substitutions = substitutions["rjs"]["nostamp"],
stamp_substitutions = substitutions["rjs"]["stamp"],
)

stamp_targets.append("stamp_file_%s" % f)
Expand Down
10 changes: 5 additions & 5 deletions tools/defaults.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ load("@npm//@angular/build-tooling/bazel:extract_js_module_output.bzl", "extract
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("//tools:link_package_json_to_tarballs.bzl", "link_package_json_to_tarballs")
load("//tools:snapshot_repo_filter.bzl", "SNAPSHOT_REPO_JQ_FILTER")
load("//tools:substitutions.bzl", "NO_STAMP_PACKAGE_SUBSTITUTIONS", "NPM_PACKAGE_SUBSTITUTIONS")
load("//tools:substitutions.bzl", "substitutions")

_DEFAULT_TSCONFIG_NG = "//:tsconfig-build-ng"
_DEFAULT_TSCONFIG_TEST = "//:tsconfig-test.json"
Expand Down Expand Up @@ -159,8 +159,8 @@ def pkg_npm(name, pkg_deps = [], use_prodmode_output = False, **kwargs):
package_name = None,
validate = False,
substitutions = select({
"//:stamp": NPM_PACKAGE_SUBSTITUTIONS,
"//conditions:default": NO_STAMP_PACKAGE_SUBSTITUTIONS,
"//:stamp": substitutions["legacy"]["stamp"],
"//conditions:default": substitutions["legacy"]["nostamp"],
}),
visibility = visibility,
nested_packages = nested_packages,
Expand Down Expand Up @@ -221,8 +221,8 @@ def ng_package(deps = [], **kwargs):
deps = deps,
license = "//:LICENSE",
substitutions = select({
"//:stamp": NPM_PACKAGE_SUBSTITUTIONS,
"//conditions:default": NO_STAMP_PACKAGE_SUBSTITUTIONS,
"//:stamp": substitutions["legacy"]["stamp"],
"//conditions:default": substitutions["legacy"]["nostamp"],
}),
**kwargs
)
5 changes: 5 additions & 0 deletions tools/package_json_release_filter.jq
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@

# Add engines; versions substituted via pkg_npm
+ {"engines": {"node": "0.0.0-ENGINES-NODE", "npm": "0.0.0-ENGINES-NPM", "yarn": "0.0.0-ENGINES-YARN"}}

# Remove all `workspace:` pnpm prefixes. Afterwards we can conveniently rely on
# substitutions from the stamp values. Note that we are doing it this way because
# substitutions can apply to multiple files, and `workspace:` can't be reliably replaced.
| walk(if type == "string" and startswith("workspace:") then sub("workspace:"; "") else . end)
20 changes: 16 additions & 4 deletions tools/substitutions.bzl
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
load("//:constants.bzl", "RELEASE_ENGINES_NODE", "RELEASE_ENGINES_NPM", "RELEASE_ENGINES_YARN")

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

NO_STAMP_PACKAGE_SUBSTITUTIONS = dict(NPM_PACKAGE_SUBSTITUTIONS, **{
_no_stamp_substitutions = dict(_stamp_substitutions, **{
"0.0.0-PLACEHOLDER": "0.0.0",
"0.0.0-EXPERIMENTAL-PLACEHOLDER": "0.0.0",
})

def get_npm_package_substitutions_for_rjs():
def _adjust_substitutions_for_rules_js(subs):
result = {}
for key, value in NPM_PACKAGE_SUBSTITUTIONS.items():
for key, value in subs.items():
# in `rules_js`, or `expand_template` from `bazel-lib`, stamp variables
# can only be retrieved via `{{X}}` syntax.
result[key] = value.replace("{", "{{").replace("}", "}}")
return result

substitutions = {
"legacy": {
"stamp": _stamp_substitutions,
"nostamp": _no_stamp_substitutions,
},
"rjs": {
"stamp": _adjust_substitutions_for_rules_js(_stamp_substitutions),
"nostamp": _adjust_substitutions_for_rules_js(_no_stamp_substitutions),
},
}

0 comments on commit 676ce57

Please sign in to comment.