Skip to content

Commit 4b5c52b

Browse files
kormidedgp1130
authored andcommitted
build: perform package.json substitutions in bazel build
1 parent f20c6d0 commit 4b5c52b

File tree

26 files changed

+432
-26
lines changed

26 files changed

+432
-26
lines changed

.bazelrc

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ build:release --stamp
7474
build:snapshot --workspace_status_command="yarn -s ng-dev release build-env-stamp --mode=snapshot"
7575
build:snapshot --stamp
7676

77+
build:local --//:enable_package_json_tar_deps
78+
7779
###############################
7880
# Output #
7981
###############################

.prettierignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
.yarn/
1313
dist/
1414
third_party/
15-
/tests/legacy-cli/e2e/assets/9.0-project/
15+
/tests/legacy-cli/e2e/assets/9.0-project/
16+
/tools/test/*.json

BUILD.bazel

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#
33
# Use of this source code is governed by an MIT-style license that can be
44
# found in the LICENSE file at https://angular.io/license
5+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
6+
57
package(default_visibility = ["//visibility:public"])
68

79
licenses(["notice"])
@@ -11,10 +13,24 @@ exports_files([
1113
"tsconfig.json", # @external
1214
"tsconfig-test.json", # @external
1315
"tsconfig-build.json", # @external
16+
"package.json",
1417
])
1518

1619
# Detect if the build is running under --stamp
1720
config_setting(
1821
name = "stamp",
1922
values = {"stamp": "true"},
2023
)
24+
25+
# If set will replace dependency versions with tarballs for packages in this repo
26+
bool_flag(
27+
name = "enable_package_json_tar_deps",
28+
build_setting_default = False,
29+
)
30+
31+
config_setting(
32+
name = "package_json_use_tar_deps",
33+
flag_values = {
34+
":enable_package_json_tar_deps": "true",
35+
},
36+
)

WORKSPACE

+26
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ workspace(
55

66
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
77

8+
http_archive(
9+
name = "bazel_skylib",
10+
sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d",
11+
urls = [
12+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
13+
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
14+
],
15+
)
16+
817
http_archive(
918
name = "build_bazel_rules_nodejs",
1019
sha256 = "cfc289523cf1594598215901154a6c2515e8bf3671fd708264a6f6aefe02bf39",
@@ -17,6 +26,10 @@ http_archive(
1726
urls = ["https://github.com/bazelbuild/rules_pkg/releases/download/0.5.1/rules_pkg-0.5.1.tar.gz"],
1827
)
1928

29+
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
30+
31+
bazel_skylib_workspace()
32+
2033
load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
2134

2235
rules_pkg_dependencies()
@@ -50,3 +63,16 @@ yarn_install(
5063
package_json = "//:package.json",
5164
yarn_lock = "//:yarn.lock",
5265
)
66+
67+
http_archive(
68+
name = "aspect_bazel_lib",
69+
sha256 = "534c9c61b72c257c95302d544984fd8ee63953c233292c5b6952ca5b33cd225e",
70+
strip_prefix = "bazel-lib-0.4.2",
71+
url = "https://github.com/aspect-build/bazel-lib/archive/v0.4.2.tar.gz",
72+
)
73+
74+
load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies", "register_jq_toolchains")
75+
76+
aspect_bazel_lib_dependencies()
77+
78+
register_jq_toolchains(version = "1.6")

packages/angular/cli/BUILD.bazel

+8-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,14 @@ genrule(
316316

317317
pkg_npm(
318318
name = "npm_package",
319-
srcs = [":package.json"],
319+
pkg_deps = [
320+
"//packages/angular_devkit/architect:package.json",
321+
"//packages/angular_devkit/build_angular:package.json",
322+
"//packages/angular_devkit/build_webpack:package.json",
323+
"//packages/angular_devkit/core:package.json",
324+
"//packages/angular_devkit/schematics:package.json",
325+
"//packages/schematics/angular:package.json",
326+
],
320327
deps = [
321328
":README.md",
322329
":angular-cli",

packages/angular/pwa/BUILD.bazel

+4-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ genrule(
7676

7777
pkg_npm(
7878
name = "npm_package",
79-
srcs = [":package.json"],
79+
pkg_deps = [
80+
"//packages/angular_devkit/schematics:package.json",
81+
"//packages/schematics/angular:package.json",
82+
],
8083
deps = [
8184
":README.md",
8285
":license",

packages/angular_devkit/architect/BUILD.bazel

+3-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ genrule(
9999

100100
pkg_npm(
101101
name = "npm_package",
102-
srcs = [":package.json"],
102+
pkg_deps = [
103+
"//packages/angular_devkit/core:package.json",
104+
],
103105
deps = [
104106
":README.md",
105107
":architect",

packages/angular_devkit/architect_cli/BUILD.bazel

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ genrule(
3737

3838
pkg_npm(
3939
name = "npm_package",
40-
srcs = [":package.json"],
40+
pkg_deps = [
41+
"//packages/angular_devkit/architect:package.json",
42+
"//packages/angular_devkit/core:package.json",
43+
],
4144
deps = [
4245
":README.md",
4346
":architect_cli",

packages/angular_devkit/benchmark/BUILD.bazel

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ genrule(
7878

7979
pkg_npm(
8080
name = "npm_package",
81-
srcs = [":package.json"],
81+
pkg_deps = [
82+
"//packages/angular_devkit/core:package.json",
83+
],
8284
deps = [
8385
"src/test/exit-code-one.js",
8486
"src/test/fibonacci.js",

packages/angular_devkit/build_angular/BUILD.bazel

+5-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,11 @@ genrule(
219219

220220
pkg_npm(
221221
name = "npm_package",
222-
srcs = [":package.json"],
222+
pkg_deps = [
223+
"//packages/angular_devkit/architect:package.json",
224+
"//packages/angular_devkit/build_webpack:package.json",
225+
"//packages/angular_devkit/core:package.json",
226+
],
223227
deps = [
224228
":README.md",
225229
":build_angular",

packages/angular_devkit/build_optimizer/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ genrule(
6868

6969
pkg_npm(
7070
name = "npm_package",
71-
srcs = [":package.json"],
7271
deps = [
7372
":README.md",
7473
":build_optimizer",

packages/angular_devkit/build_webpack/BUILD.bazel

+3-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ genrule(
109109

110110
pkg_npm(
111111
name = "npm_package",
112-
srcs = [":package.json"],
112+
pkg_deps = [
113+
"//packages/angular_devkit/architect:package.json",
114+
],
113115
deps = [
114116
":README.md",
115117
":build_webpack",

packages/angular_devkit/core/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ genrule(
8585

8686
pkg_npm(
8787
name = "npm_package",
88-
srcs = [":package.json"],
8988
deps = [
9089
":README.md",
9190
":core",

packages/angular_devkit/schematics/BUILD.bazel

+3-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ genrule(
7373

7474
pkg_npm(
7575
name = "npm_package",
76-
srcs = [":package.json"],
76+
pkg_deps = [
77+
"//packages/angular_devkit/core:package.json",
78+
],
7779
deps = [
7880
":README.md",
7981
":collection-schema.json",

packages/angular_devkit/schematics_cli/BUILD.bazel

+4-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ genrule(
9898

9999
pkg_npm(
100100
name = "npm_package",
101-
srcs = [":package.json"],
101+
pkg_deps = [
102+
"//packages/angular_devkit/schematics:package.json",
103+
"//packages/angular_devkit/core:package.json",
104+
],
102105
deps = [
103106
":README.md",
104107
":license",

packages/ngtools/webpack/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ genrule(
7676

7777
pkg_npm(
7878
name = "npm_package",
79-
srcs = [":package.json"],
8079
deps = [
8180
":README.md",
8281
":license",

packages/schematics/angular/BUILD.bazel

+4-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ genrule(
140140

141141
pkg_npm(
142142
name = "npm_package",
143-
srcs = [":package.json"],
143+
pkg_deps = [
144+
"//packages/angular_devkit/schematics:package.json",
145+
"//packages/angular_devkit/core:package.json",
146+
],
144147
deps = [
145148
"library/library-long.md",
146149
":README.md",

scripts/build-bazel.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,6 @@ async function _build(logger: logging.Logger, mode: BuildMode): Promise<string[]
113113
const queryTargetsCmd = `${bazelCmd} query --output=label "attr(name, npm_package_archive, //packages/...)"`;
114114
const targets = (await _exec(queryTargetsCmd, true, queryLogger)).split(/\r?\n/);
115115

116-
let configArg = '';
117-
if (mode === 'snapshot') {
118-
configArg = '--config=snapshot';
119-
} else if (mode === 'release') {
120-
configArg = '--config=release';
121-
}
122-
123116
const buildLogger = logger.createChild('build');
124117

125118
// If we are in release mode, run `bazel clean` to ensure the execroot and action cache
@@ -132,7 +125,7 @@ async function _build(logger: logging.Logger, mode: BuildMode): Promise<string[]
132125
await _exec(`${bazelCmd} clean`, false, buildLogger);
133126
}
134127

135-
await _exec(`${bazelCmd} build ${configArg} ${targets.join(' ')}`, false, buildLogger);
128+
await _exec(`${bazelCmd} build --config=${mode} ${targets.join(' ')}`, false, buildLogger);
136129

137130
return targets;
138131
}

tools/BUILD.bazel

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary")
77

88
package(default_visibility = ["//visibility:public"])
99

10+
exports_files([
11+
"package_json_release_filter.jq",
12+
])
13+
1014
nodejs_binary(
1115
name = "ng_cli_schema",
1216
data = [

tools/defaults.bzl

+71-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
"""Re-export of some bazel rules with repository-wide defaults."""
22

33
load("@npm//@bazel/typescript:index.bzl", _ts_library = "ts_library")
4-
load("@build_bazel_rules_nodejs//:index.bzl", _pkg_npm = "pkg_npm")
4+
load("@build_bazel_rules_nodejs//:index.bzl", "copy_to_bin", _pkg_npm = "pkg_npm")
55
load("@rules_pkg//:pkg.bzl", "pkg_tar")
66
load("@npm//@angular/dev-infra-private/bazel:extract_js_module_output.bzl", "extract_js_module_output")
7+
load("@aspect_bazel_lib//lib:utils.bzl", "to_label")
8+
load("@aspect_bazel_lib//lib:jq.bzl", "jq")
9+
load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
10+
load("//tools:link_package_json_to_tarballs.bzl", "link_package_json_to_tarballs")
711

812
_DEFAULT_TSCONFIG = "//:tsconfig-build.json"
913
_DEFAULT_TSCONFIG_TEST = "//:tsconfig-test.json"
@@ -44,8 +48,20 @@ def ts_library(
4448
**kwargs
4549
)
4650

47-
def pkg_npm(name, use_prodmode_output = False, **kwargs):
48-
"""Default values for pkg_npm"""
51+
def pkg_npm(name, pkg_deps = [], use_prodmode_output = False, **kwargs):
52+
"""Override of pkg_npm to produce package outputs and version substitutions conventional to the angular-cli project.
53+
54+
Produces a package and a tar of that package. Expects a package.json file
55+
in the same folder to exist.
56+
57+
Args:
58+
name: Name of the pkg_npm rule. '_archive.tar.gz' is appended to create the tarball.
59+
pkg_deps: package.json files of dependent packages. These are used for local path substitutions when --config=local is set.
60+
use_prodmode_output: False to ship ES5 devmode output, True to ship ESM output. Defaults to False.
61+
**kwargs: Additional arguments passed to the real pkg_npm.
62+
"""
63+
pkg_json = ":package.json"
64+
4965
visibility = kwargs.pop("visibility", None)
5066

5167
common_substitutions = dict(kwargs.pop("substitutions", {}))
@@ -79,6 +95,57 @@ def pkg_npm(name, use_prodmode_output = False, **kwargs):
7995
deps = deps,
8096
)
8197

98+
# Merge package.json with root package.json and perform various substitutions to
99+
# prepare it for release. For jq docs, see https://stedolan.github.io/jq/manual/.
100+
jq(
101+
name = "basic_substitutions",
102+
# Note: this jq filter relies on the order of the inputs
103+
# buildifier: do not sort
104+
srcs = ["//:package.json", pkg_json],
105+
filter_file = "//tools:package_json_release_filter.jq",
106+
args = ["--slurp"],
107+
out = "substituted/package.json",
108+
)
109+
110+
# Copy package.json files to bazel-out so we can use their bazel-out paths to determine
111+
# the corresponding package npm package tar.gz path for substitutions.
112+
copy_to_bin(
113+
name = "package_json_copy",
114+
srcs = [pkg_json],
115+
)
116+
pkg_deps_copies = []
117+
for pkg_dep in pkg_deps:
118+
pkg_label = to_label(pkg_dep)
119+
if pkg_label.name != "package.json":
120+
fail("ERROR: only package.json files allowed in pkg_deps of pkg_npm macro")
121+
pkg_deps_copies.append("@%s//%s:package_json_copy" % (pkg_label.workspace_name, pkg_label.package))
122+
123+
# Substitute dependencies on other packages in this repo with tarballs.
124+
link_package_json_to_tarballs(
125+
name = "tar_substitutions",
126+
src = "substituted/package.json",
127+
pkg_deps = [":package_json_copy"] + pkg_deps_copies,
128+
out = "substituted_with_tars/package.json",
129+
)
130+
131+
# Move the generated package.json along with other deps into a directory for pkg_npm
132+
# to package up because pkg_npm requires that all inputs be in the same directory.
133+
copy_to_directory(
134+
name = "package",
135+
srcs = select({
136+
# Do tar substitution if config_setting 'package_json_use_tar_deps' is true (local builds)
137+
"//:package_json_use_tar_deps": [":%s_js_module_output" % name, "substituted_with_tars/package.json"],
138+
"//conditions:default": [":%s_js_module_output" % name, "substituted/package.json"],
139+
}),
140+
replace_prefixes = {
141+
"substituted_with_tars/": "",
142+
"substituted/": "",
143+
},
144+
exclude_prefixes = [
145+
"packages", # Exclude compiled outputs of dependent packages
146+
],
147+
)
148+
82149
_pkg_npm(
83150
name = name,
84151
# We never set a `package_name` for NPM packages, neither do we enable validation.
@@ -101,7 +168,7 @@ def pkg_npm(name, use_prodmode_output = False, **kwargs):
101168
"//conditions:default": substitutions,
102169
}),
103170
visibility = visibility,
104-
deps = [":%s_js_module_output" % name],
171+
nested_packages = ["package"],
105172
tgz = None,
106173
**kwargs
107174
)

0 commit comments

Comments
 (0)