1
1
"""Re-export of some bazel rules with repository-wide defaults."""
2
2
3
3
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" )
5
5
load ("@rules_pkg//:pkg.bzl" , "pkg_tar" )
6
6
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" )
7
11
8
12
_DEFAULT_TSCONFIG = "//:tsconfig-build.json"
9
13
_DEFAULT_TSCONFIG_TEST = "//:tsconfig-test.json"
@@ -44,8 +48,20 @@ def ts_library(
44
48
** kwargs
45
49
)
46
50
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
+
49
65
visibility = kwargs .pop ("visibility" , None )
50
66
51
67
common_substitutions = dict (kwargs .pop ("substitutions" , {}))
@@ -79,6 +95,57 @@ def pkg_npm(name, use_prodmode_output = False, **kwargs):
79
95
deps = deps ,
80
96
)
81
97
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
+
82
149
_pkg_npm (
83
150
name = name ,
84
151
# 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):
101
168
"//conditions:default" : substitutions ,
102
169
}),
103
170
visibility = visibility ,
104
- deps = [":%s_js_module_output" % name ],
171
+ nested_packages = ["package" ],
105
172
tgz = None ,
106
173
** kwargs
107
174
)
0 commit comments