-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathdefaults.bzl
327 lines (291 loc) · 14.6 KB
/
defaults.bzl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# Re-export of Bazel rules with repository-wide defaults
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("@build_bazel_rules_nodejs//:index.bzl", _pkg_npm = "pkg_npm")
load("@io_bazel_rules_sass//:defs.bzl", _npm_sass_library = "npm_sass_library", _sass_binary = "sass_binary", _sass_library = "sass_library")
load("@npm//@angular/bazel:index.bzl", _ng_package = "ng_package")
load("@npm//@angular/build-tooling/bazel/integration:index.bzl", _integration_test = "integration_test")
load("@npm//@angular/build-tooling/bazel/karma:index.bzl", _karma_web_test_suite = "karma_web_test_suite")
load("@npm//@angular/build-tooling/bazel/esbuild:index.bzl", _esbuild = "esbuild", _esbuild_config = "esbuild_config")
load("@npm//@angular/build-tooling/bazel/spec-bundling:index.bzl", _spec_bundle = "spec_bundle")
load("@npm//@angular/build-tooling/bazel/http-server:index.bzl", _http_server = "http_server")
load("@npm//@angular/build-tooling/bazel:extract_js_module_output.bzl", "extract_js_module_output")
load("@npm//@bazel/jasmine:index.bzl", _jasmine_node_test = "jasmine_node_test")
load("@npm//@bazel/protractor:index.bzl", _protractor_web_test_suite = "protractor_web_test_suite")
load("//:packages.bzl", "NO_STAMP_NPM_PACKAGE_SUBSTITUTIONS", "NPM_PACKAGE_SUBSTITUTIONS")
load("//:pkg-externals.bzl", "PKG_EXTERNALS")
load("//tools/markdown-to-html:index.bzl", _markdown_to_html = "markdown_to_html")
load("//tools/extract-tokens:index.bzl", _extract_tokens = "extract_tokens")
load("//tools/angular:index.bzl", "LINKER_PROCESSED_FW_PACKAGES")
load("//tools/bazel:legacy_target.bzl", "get_legacy_label")
npmPackageSubstitutions = select({
"//tools:stamp": NPM_PACKAGE_SUBSTITUTIONS,
"//conditions:default": NO_STAMP_NPM_PACKAGE_SUBSTITUTIONS,
})
# Re-exports to simplify build file load statements
markdown_to_html = _markdown_to_html
integration_test = _integration_test
extract_tokens = _extract_tokens
esbuild = _esbuild
esbuild_config = _esbuild_config
http_server = _http_server
def sass_binary(sourcemap = False, include_paths = [], **kwargs):
_sass_binary(
sourcemap = sourcemap,
include_paths = include_paths + ["external/npm/node_modules"],
compiler = "//tools/sass:compiler",
**kwargs
)
def sass_library(**kwargs):
_sass_library(**kwargs)
def npm_sass_library(**kwargs):
_npm_sass_library(**kwargs)
def ng_package(name, srcs = [], deps = [], externals = PKG_EXTERNALS, readme_md = None, visibility = None, **kwargs):
# If no readme file has been specified explicitly, use the default readme for
# release packages from "src/README.md".
if not readme_md:
readme_md = "//src:README.md"
# We need a genrule that copies the license into the current package. This
# allows us to include the license in the "ng_package".
native.genrule(
name = "license_copied",
srcs = ["//:LICENSE"],
outs = ["LICENSE"],
cmd = "cp $< $@",
)
_ng_package(
name = name,
externals = externals,
srcs = srcs + [":license_copied"],
deps = deps,
# We never set a `package_name` for NPM packages, neither do we enable validation.
# This is necessary because the source targets of the NPM packages all have
# package names set and setting a similar `package_name` on the NPM package would
# result in duplicate linker mappings that will conflict. e.g. consider the following
# scenario: We have a `ts_library` for `@angular/cdk`. We will configure a package
# name for the target so that it can be resolved in NodeJS executions from `node_modules`.
# If we'd also set a `package_name` for the associated `pkg_npm` target, there would be
# two mappings for `@angular/cdk` and the linker will complain. For a better development
# experience, we want the mapping to resolve to the direct outputs of the `ts_library`
# instead of requiring tests and other targets to assemble the NPM package first.
package_name = None,
validate = False,
readme_md = readme_md,
substitutions = npmPackageSubstitutions,
visibility = visibility,
**kwargs
)
pkg_tar(
name = name + "_archive",
srcs = [":%s" % name],
extension = "tar.gz",
strip_prefix = "./%s" % name,
package_dir = "package/",
# Target should not build on CI unless it is explicitly requested.
tags = ["manual"],
visibility = visibility,
)
def pkg_npm(name, visibility = None, **kwargs):
_pkg_npm(
name = name,
# We never set a `package_name` for NPM packages, neither do we enable validation.
# This is necessary because the source targets of the NPM packages all have
# package names set and setting a similar `package_name` on the NPM package would
# result in duplicate linker mappings that will conflict. e.g. consider the following
# scenario: We have a `ts_library` for `@angular/cdk`. We will configure a package
# name for the target so that it can be resolved in NodeJS executions from `node_modules`.
# If we'd also set a `package_name` for the associated `pkg_npm` target, there would be
# two mappings for `@angular/cdk` and the linker will complain. For a better development
# experience, we want the mapping to resolve to the direct outputs of the `ts_library`
# instead of requiring tests and other targets to assemble the NPM package first.
package_name = None,
validate = False,
substitutions = npmPackageSubstitutions,
visibility = visibility,
**kwargs
)
pkg_tar(
name = name + "_archive",
srcs = [":%s" % name],
package_dir = "package/",
extension = "tar.gz",
strip_prefix = "./%s" % name,
# Target should not build on CI unless it is explicitly requested.
tags = ["manual"],
visibility = visibility,
)
def jasmine_node_test(**kwargs):
kwargs["templated_args"] = ["--bazel_patch_module_resolver"] + kwargs.get("templated_args", [])
_jasmine_node_test(**kwargs)
def karma_web_test_suite(name, **kwargs):
test_deps = kwargs.get("deps", [])
kwargs["tags"] = ["partial-compilation-integration"] + kwargs.get("tags", [])
kwargs["deps"] = ["%s_bundle" % name]
test_deps_legacy = [get_legacy_label(d) for d in test_deps]
spec_bundle(
name = "%s_bundle" % name,
deps = test_deps_legacy,
platform = "browser",
)
# Set up default browsers if no explicit `browsers` have been specified.
if not hasattr(kwargs, "browsers"):
kwargs["tags"] = ["native"] + kwargs.get("tags", [])
kwargs["browsers"] = [
# Note: when changing the browser names here, also update the "yarn test"
# script to reflect the new browser names.
"@npm//@angular/build-tooling/bazel/browsers/chromium:chromium",
"@npm//@angular/build-tooling/bazel/browsers/firefox:firefox",
]
# Default test suite with all configured browsers, and the debug target being
# setup from `angular/dev-infra`.
_karma_web_test_suite(
name = name,
**kwargs
)
def protractor_web_test_suite(name, deps, **kwargs):
spec_bundle(
name = "%s_bundle" % name,
deps = deps,
platform = "cjs-legacy",
external = ["protractor", "selenium-webdriver"],
)
_protractor_web_test_suite(
name = name,
browsers = ["@npm//@angular/build-tooling/bazel/browsers/chromium:chromium"],
deps = ["%s_bundle" % name],
**kwargs
)
def node_integration_test(setup_chromium = False, node_repository = "nodejs", **kwargs):
"""Macro for defining an integration test with `node` and `yarn` being
declared as global tools.
By default the default Node version of the workspace is being used."""
data = kwargs.pop("data", [])
toolchains = kwargs.pop("toolchains", [])
environment = kwargs.pop("environment", {})
tool_mappings = kwargs.pop("tool_mappings", {})
# Setup Yarn and Node as tools in a way that allows for them to be overridden.
tool_mappings = dict({
"//:yarn_vendored": "yarn",
"@%s_toolchains//:resolved_toolchain" % node_repository: "node",
}, **tool_mappings)
# If Chromium should be configured, add it to the runfiles and expose its binaries
# through test environment variables. The variables are auto-detected by e.g. Karma.
if setup_chromium:
data.append("@npm//@angular/build-tooling/bazel/browsers/chromium")
toolchains.append("@npm//@angular/build-tooling/bazel/browsers/chromium:toolchain_alias")
environment.update({
"CHROMEDRIVER_BIN": "$(CHROMEDRIVER)",
"CHROME_BIN": "$(CHROMIUM)",
})
integration_test(
data = data,
environment = environment,
toolchains = toolchains,
tool_mappings = tool_mappings,
**kwargs
)
def ng_web_test_suite(deps = [], static_css = [], exclude_init_script = False, **kwargs):
bootstrap = [
# This matches the ZoneJS bundles used in default CLI projects. See:
# https://github.com/angular/angular-cli/blob/main/packages/schematics/angular/application/files/src/polyfills.ts.template#L58
# https://github.com/angular/angular-cli/blob/main/packages/schematics/angular/application/files/src/test.ts.template#L3
# Note `zone.js/dist/zone.js` is aliased in the CLI to point to the evergreen
# output that does not include legacy patches. See: https://github.com/angular/angular/issues/35157.
# TODO: Consider adding the legacy patches when testing Saucelabs/Browserstack with Bazel.
# CLI loads the legacy patches conditionally for ES5 legacy browsers. See:
# https://github.com/angular/angular-cli/blob/277bad3895cbce6de80aa10a05c349b10d9e09df/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts#L141
"@npm//:node_modules/zone.js/bundles/zone.umd.js",
"@npm//:node_modules/zone.js/bundles/zone-testing.umd.js",
"@npm//:node_modules/reflect-metadata/Reflect.js",
] + kwargs.pop("bootstrap", [])
# Always include a prebuilt theme in the test suite because otherwise tests, which depend on CSS
# that is needed for measuring, will unexpectedly fail. Also always adding a prebuilt theme
# reduces the amount of setup that is needed to create a test suite Bazel target. Note that the
# prebuilt theme will be also added to CDK test suites but shouldn't affect anything.
static_css = static_css + [
"//src/material/prebuilt-themes:azure-blue",
]
# Workaround for https://github.com/bazelbuild/rules_typescript/issues/301
# Since some of our tests depend on CSS files which are not part of the `ng_project` rule,
# we need to somehow load static CSS files within Karma (e.g. overlay prebuilt). Those styles
# are required for successful test runs. Since the `karma_web_test_suite` rule currently only
# allows JS files to be included and served within Karma, we need to create a JS file that
# loads the given CSS file.
for css_label in static_css:
css_id = "static-css-file-%s" % (css_label.replace("/", "_").replace(":", "-"))
bootstrap.append(":%s" % css_id)
native.genrule(
name = css_id,
srcs = [css_label],
outs = ["%s.css.js" % css_id],
output_to_bindir = True,
cmd = """
files=($(execpaths %s))
# Escape all double-quotes so that the content can be safely inlined into the
# JS template. Note that it needs to be escaped a second time because the string
# will be evaluated first in Bash and will then be stored in the JS output.
css_content=$$(cat $${files[0]} | sed 's/"/\\\\"/g')
js_template='var cssElement = document.createElement("style"); \
cssElement.type = "text/css"; \
cssElement.innerHTML = "'"$$css_content"'"; \
document.head.appendChild(cssElement);'
echo "$$js_template" > $@
""" % css_label,
)
karma_web_test_suite(
# Depend on our custom test initialization script. This needs to be the first dependency.
deps = deps if exclude_init_script else ["//test:angular_test_init"] + deps,
bootstrap = bootstrap,
**kwargs
)
def spec_bundle(name, deps, **kwargs):
# TODO: Rename once devmode and prodmode have been combined.
# For spec bundling we also only consume devmode output as it is ESM in this repository.
# This helps speeding up development experience as ESBuild (used internally by the rule)
# would request both devmode and prodmode output flavor (resulting in 2x TS compilations).
extract_js_module_output(
name = "%s_devmode_deps" % name,
deps = deps,
provider = "JSModuleInfo",
forward_linker_mappings = True,
include_external_npm_packages = True,
include_default_files = False,
include_declarations = False,
testonly = True,
)
_spec_bundle(
name = name,
# For specs, we always add the pre-processed linker FW packages so that these
# are resolved instead of the unprocessed FW entry-points through the `node_modules`.
deps = ["%s_devmode_deps" % name] + LINKER_PROCESSED_FW_PACKAGES,
workspace_name = "angular_material",
run_angular_linker = select({
# Depending on whether partial compilation is enabled, we may want to run the linker
# to test the Angular compiler linker AOT processing. Additionally, a config setting
# can forcibly disable the linker to ensure tests rely on JIT linking at runtime.
"//tools:force_partial_jit_compilation_enabled": False,
"//tools:partial_compilation_enabled": True,
"//conditions:default": False,
}),
**kwargs
)
# TODO: Rename once devmode and prodmode have been combined.
def devmode_esbuild(name, deps, testonly = False, **kwargs):
"""Extension of the default `@bazel/esbuild` rule so that only devmode ESM output
is requested. This is done to speed up local development because the ESBuild rule
by default requests all possible output flavors/modes."""
extract_js_module_output(
name = "%s_devmode_deps" % name,
deps = deps,
testonly = testonly,
forward_linker_mappings = True,
include_external_npm_packages = True,
include_default_files = False,
include_declarations = False,
provider = "JSModuleInfo",
)
_esbuild(
name = name,
deps = ["%s_devmode_deps" % name],
testonly = testonly,
**kwargs
)