blob: b735802f5588dc391212b4bcf0aeb93377475813 [file] [log] [blame]
Philipp Schrader175a93c2023-02-19 13:13:40 -08001load("@aspect_rules_js//js:providers.bzl", "JsInfo")
Philipp Schradere5d13942024-03-17 15:44:35 -07002load("@aspect_rules_js//npm:defs.bzl", "npm_package")
Philipp Schrader175a93c2023-02-19 13:13:40 -08003load("@bazel_skylib//rules:write_file.bzl", "write_file")
Philipp Schrader175a93c2023-02-19 13:13:40 -08004load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")
5load("@aspect_bazel_lib//lib:copy_file.bzl", "copy_file")
6load("@aspect_rules_esbuild//esbuild:defs.bzl", "esbuild")
Philipp Schrader3de4dfc2023-02-15 20:18:25 -08007
Philipp Schrader175a93c2023-02-19 13:13:40 -08008#load("@npm//:history-server/package_json.bzl", history_server_bin = "bin")
9load("@npm//:html-insert-assets/package_json.bzl", html_insert_assets_bin = "bin")
10load("//tools/build_rules/js:ng.bzl", "ng_esbuild", "ng_project")
11load("//tools/build_rules/js:ts.bzl", _ts_project = "ts_project")
Philipp Schraderba072d92024-02-21 17:00:37 -080012load("@aspect_rules_rollup//rollup:defs.bzl", upstream_rollup_bundle = "rollup")
13load("@aspect_rules_terser//terser:defs.bzl", terser_minified = "terser")
Philipp Schrader155e76c2023-02-25 18:42:31 -080014load("@aspect_rules_cypress//cypress:defs.bzl", "cypress_module_test")
Philipp Schrader3de4dfc2023-02-15 20:18:25 -080015
Philipp Schrader175a93c2023-02-19 13:13:40 -080016ts_project = _ts_project
17
18# Common dependencies of Angular applications
19APPLICATION_DEPS = [
20 "//:node_modules/@angular/common",
21 "//:node_modules/@angular/core",
22 #"//:node_modules/@angular/router",
23 "//:node_modules/@angular/platform-browser",
24 "//:node_modules/@types/node",
25 "//:node_modules/rxjs",
26 #"//:node_modules/tslib",
27]
28
29APPLICATION_HTML_ASSETS = ["styles.css", "favicon.ico"]
30
31# Common dependencies of Angular packages
32PACKAGE_DEPS = [
33 "//:node_modules/@angular/common",
34 "//:node_modules/@angular/core",
35 #"//:node_modules/@angular/router",
36 "//:node_modules/@types/node",
37 "//:node_modules/rxjs",
38 #"//:node_modules/tslib",
39]
40
41TEST_DEPS = APPLICATION_DEPS + [
42 "//:node_modules/@angular/compiler",
43 "//:node_modules/@types/jasmine",
44 "//:node_modules/jasmine-core",
45 "//:node_modules/@angular/platform-browser-dynamic",
46]
47
48NG_DEV_DEFINE = {
49 "process.env.NODE_ENV": "'development'",
50 "ngJitMode": "false",
51}
52NG_PROD_DEFINE = {
53 "process.env.NODE_ENV": "'production'",
54 "ngDevMode": "false",
55 "ngJitMode": "false",
56}
57
58def ng_application(
59 name,
60 deps = [],
Philipp Schrader175a93c2023-02-19 13:13:40 -080061 extra_srcs = [],
62 assets = None,
63 html_assets = APPLICATION_HTML_ASSETS,
Adam Snaider13d48d92023-08-03 12:20:15 -070064 visibility = ["//visibility:public"]):
Philipp Schrader3de4dfc2023-02-15 20:18:25 -080065 """
Philipp Schrader175a93c2023-02-19 13:13:40 -080066 Bazel macro for compiling an Angular application. Creates {name}, test, serve targets.
Philipp Schrader87277f42022-01-01 07:45:12 -080067
Philipp Schrader175a93c2023-02-19 13:13:40 -080068 Projects structure:
69 main.ts
70 index.html
71 polyfills.ts
72 styles.css, favicon.ico (defaults, can be overriden)
73 app/
74 **/*.{ts,css,html}
75
76 Tests:
77 app/
78 **/*.spec.ts
79
80 Args:
81 name: the rule name
82 deps: direct dependencies of the application
Philipp Schrader175a93c2023-02-19 13:13:40 -080083 html_assets: assets to insert into the index.html, [styles.css, favicon.ico] by default
84 assets: assets to include in the file bundle
85 visibility: visibility of the primary targets ({name}, 'test', 'serve')
Philipp Schrader175a93c2023-02-19 13:13:40 -080086 """
87 assets = assets if assets else native.glob(["assets/**/*"])
88 html_assets = html_assets if html_assets else []
89
90 test_spec_srcs = native.glob(["app/**/*.spec.ts"])
91
92 srcs = native.glob(
93 ["main.ts", "app/**/*", "package.json"],
94 exclude = test_spec_srcs,
95 ) + extra_srcs
96
97 # Primary app source
98 ng_project(
99 name = "_app",
100 srcs = srcs,
101 deps = deps + APPLICATION_DEPS,
Austin Schuhf737d472023-07-29 17:35:59 -0700102 tags = [
103 "no-remote-cache",
104 ],
Philipp Schrader175a93c2023-02-19 13:13:40 -0800105 visibility = ["//visibility:private"],
106 )
107
108 # App polyfills source + bundle.
109 ng_project(
110 name = "_polyfills",
111 srcs = ["polyfills.ts"],
112 deps = ["//:node_modules/zone.js"],
Austin Schuhf737d472023-07-29 17:35:59 -0700113 tags = [
114 "no-remote-cache",
115 ],
Philipp Schrader175a93c2023-02-19 13:13:40 -0800116 visibility = ["//visibility:private"],
117 )
118 esbuild(
119 name = "polyfills-bundle",
120 entry_point = "polyfills.js",
121 srcs = [":_polyfills"],
122 define = {"process.env.NODE_ENV": "'production'"},
123 config = {
124 "resolveExtensions": [".mjs", ".js"],
125 },
126 metafile = False,
127 format = "esm",
128 minify = True,
Austin Schuhf737d472023-07-29 17:35:59 -0700129 tags = [
130 "no-remote-cache",
131 ],
Philipp Schrader175a93c2023-02-19 13:13:40 -0800132 visibility = ["//visibility:private"],
133 )
134
135 _pkg_web(
136 name = "prod",
137 entry_point = "main.js",
138 entry_deps = [":_app"],
139 html_assets = html_assets,
140 assets = assets,
141 production = True,
142 visibility = ["//visibility:private"],
143 )
144
145 _pkg_web(
146 name = "dev",
147 entry_point = "main.js",
148 entry_deps = [":_app"],
149 html_assets = html_assets,
150 assets = assets,
151 production = False,
152 visibility = ["//visibility:private"],
153 )
154
155 # The default target: the prod package
156 native.alias(
157 name = name,
158 actual = "prod",
159 visibility = visibility,
160 )
161
162def _pkg_web(name, entry_point, entry_deps, html_assets, assets, production, visibility):
163 """ Bundle and create runnable web package.
164
165 For a given application entry_point, assets and defined constants... generate
166 a bundle using that entry and constants, an index.html referencing the bundle and
167 providated assets, package all content into a resulting directory of the given name.
168 """
169
170 bundle = "bundle-%s" % name
171
172 ng_esbuild(
173 name = bundle,
174 entry_points = [entry_point],
175 srcs = entry_deps,
176 define = NG_PROD_DEFINE if production else NG_DEV_DEFINE,
177 format = "esm",
178 output_dir = True,
179 splitting = True,
180 metafile = False,
181 minify = production,
182 visibility = ["//visibility:private"],
183 )
184
185 html_out = "_%s_html" % name
186
187 html_insert_assets_bin.html_insert_assets(
188 name = html_out,
189 outs = ["%s/index.html" % html_out],
190 args = [
191 # Template HTML file.
192 "--html",
193 "$(location :index.html)",
194 # Output HTML file.
195 "--out",
196 "%s/%s/index.html" % (native.package_name(), html_out),
197 # Root directory prefixes to strip from asset paths.
198 "--roots",
199 native.package_name(),
200 "%s/%s" % (native.package_name(), html_out),
201 ] +
202 # Generic Assets
203 ["--assets"] + ["$(execpath %s)" % s for s in html_assets] +
204 ["--scripts", "--module", "polyfills-bundle.js"] +
205 # Main bundle to bootstrap the app last
206 ["--scripts", "--module", "%s/main.js" % bundle],
207 # The input HTML template, all assets for potential access for stamping
208 srcs = [":index.html", ":%s" % bundle, ":polyfills-bundle"] + html_assets,
Austin Schuhf737d472023-07-29 17:35:59 -0700209 tags = [
210 "no-remote-cache",
211 ],
Philipp Schrader175a93c2023-02-19 13:13:40 -0800212 visibility = ["//visibility:private"],
213 )
214
215 copy_to_directory(
216 name = name,
217 srcs = [":%s" % bundle, ":polyfills-bundle", ":%s" % html_out] + html_assets + assets,
218 root_paths = [".", "%s/%s" % (native.package_name(), html_out)],
219 visibility = visibility,
220 )
221
222 # http server serving the bundle
223 # TODO(phil): Get this working.
224 #history_server_bin.history_server_binary(
225 # name = "serve" + ("-prod" if production else ""),
226 # args = ["$(location :%s)" % name],
227 # data = [":%s" % name],
228 # visibility = visibility,
229 #)
230
Adam Snaider13d48d92023-08-03 12:20:15 -0700231def ng_pkg(name, generate_public_api = True, extra_srcs = [], deps = [], visibility = ["//visibility:public"], **kwargs):
Philipp Schrader175a93c2023-02-19 13:13:40 -0800232 """
233 Bazel macro for compiling an npm-like Angular package project. Creates '{name}' and 'test' targets.
234
235 Projects structure:
236 src/
237 public-api.ts
238 **/*.{ts,css,html}
239
240 Tests:
241 src/
242 **/*.spec.ts
243
244 Args:
245 name: the rule name
246 deps: package dependencies
Philipp Schrader175a93c2023-02-19 13:13:40 -0800247 visibility: visibility of the primary targets ('{name}', 'test')
248 """
249
250 test_spec_srcs = native.glob(["**/*.spec.ts"])
251
252 srcs = native.glob(
253 ["**/*.ts", "**/*.css", "**/*.html"],
254 exclude = test_spec_srcs + [
255 "public-api.ts",
256 ],
257 ) + extra_srcs
258
259 # An index file to allow direct imports of the directory similar to a package.json "main"
260 write_file(
261 name = "_index",
262 out = "index.ts",
263 content = ["export * from \"./public-api\";"],
264 visibility = ["//visibility:private"],
265 )
266
267 if generate_public_api:
268 write_file(
269 name = "_public_api",
270 out = "public-api.ts",
271 content = [
272 "export * from './%s.component';" % name,
273 "export * from './%s.module';" % name,
274 ],
275 visibility = ["//visibility:private"],
276 )
277 srcs.append(":_public_api")
278
279 ng_project(
Philipp Schradere5d13942024-03-17 15:44:35 -0700280 name = "_lib",
Philipp Schrader175a93c2023-02-19 13:13:40 -0800281 srcs = srcs + [":_index"],
282 deps = deps + PACKAGE_DEPS,
Philipp Schradere5d13942024-03-17 15:44:35 -0700283 visibility = ["//visibility:private"],
Philipp Schrader175a93c2023-02-19 13:13:40 -0800284 **kwargs
285 )
286
Philipp Schradere5d13942024-03-17 15:44:35 -0700287 npm_package(
288 name = name,
289 srcs = ["package.json", ":_lib"],
290 include_runfiles = False,
291 visibility = visibility,
292 )
293
Philipp Schraderba072d92024-02-21 17:00:37 -0800294def rollup_bundle(name, entry_point, node_modules = "//:node_modules", deps = [], visibility = None, **kwargs):
Philipp Schrader87277f42022-01-01 07:45:12 -0800295 """Calls the upstream rollup_bundle() and exposes a .min.js file.
296
297 Legacy version of rollup_bundle() used to provide the .min.js file. This
298 wrapper provides the same interface by explicitly exposing a .min.js file.
299 """
Philipp Schrader175a93c2023-02-19 13:13:40 -0800300 copy_file(
301 name = name + "__rollup_config",
302 src = "//:rollup.config.js",
303 out = name + "__rollup_config.js",
304 )
305
Philipp Schrader87277f42022-01-01 07:45:12 -0800306 upstream_rollup_bundle(
307 name = name,
308 visibility = visibility,
309 deps = deps + [
Philipp Schrader175a93c2023-02-19 13:13:40 -0800310 "//:node_modules/@rollup/plugin-node-resolve",
Philipp Schrader87277f42022-01-01 07:45:12 -0800311 ],
Philipp Schraderba072d92024-02-21 17:00:37 -0800312 node_modules = node_modules,
Philipp Schrader175a93c2023-02-19 13:13:40 -0800313 sourcemap = "false",
314 config_file = ":%s__rollup_config.js" % name,
315 entry_point = entry_point,
Philipp Schrader87277f42022-01-01 07:45:12 -0800316 **kwargs
317 )
318
319 terser_minified(
320 name = name + "__min",
Philipp Schrader175a93c2023-02-19 13:13:40 -0800321 srcs = [name + ".js"],
Philipp Schraderba072d92024-02-21 17:00:37 -0800322 node_modules = node_modules,
Austin Schuhf737d472023-07-29 17:35:59 -0700323 tags = [
324 "no-remote-cache",
325 ],
Philipp Schrader175a93c2023-02-19 13:13:40 -0800326 sourcemap = False,
Philipp Schrader87277f42022-01-01 07:45:12 -0800327 )
328
329 # Copy the __min.js file (a declared output inside the rule) so that it's a
330 # pre-declared output and publicly visible. I.e. via attr.output() below.
Philipp Schrader175a93c2023-02-19 13:13:40 -0800331 _expose_file_with_suffix(
Philipp Schrader87277f42022-01-01 07:45:12 -0800332 name = name + "__min_exposed",
333 src = ":%s__min" % name,
334 out = name + ".min.js",
Philipp Schrader175a93c2023-02-19 13:13:40 -0800335 suffix = "__min.js",
Philipp Schrader87277f42022-01-01 07:45:12 -0800336 visibility = visibility,
337 )
338
Philipp Schrader175a93c2023-02-19 13:13:40 -0800339def _expose_file_with_suffix_impl(ctx):
Philipp Schrader87277f42022-01-01 07:45:12 -0800340 """Copies the .min.js file in order to make it publicly accessible."""
Philipp Schrader175a93c2023-02-19 13:13:40 -0800341 sources = ctx.attr.src[JsInfo].sources.to_list()
Philipp Schrader87277f42022-01-01 07:45:12 -0800342 min_js = None
343 for src in sources:
Philipp Schrader175a93c2023-02-19 13:13:40 -0800344 if src.basename.endswith(ctx.attr.suffix):
Philipp Schrader87277f42022-01-01 07:45:12 -0800345 min_js = src
346 break
347
348 if min_js == None:
349 fail("Couldn't find .min.js in " + str(ctx.attr.src))
350
351 ctx.actions.run(
352 inputs = [min_js],
353 outputs = [ctx.outputs.out],
354 executable = "cp",
355 arguments = [min_js.path, ctx.outputs.out.path],
356 )
357
Philipp Schrader175a93c2023-02-19 13:13:40 -0800358_expose_file_with_suffix = rule(
359 implementation = _expose_file_with_suffix_impl,
Philipp Schrader87277f42022-01-01 07:45:12 -0800360 attrs = {
Philipp Schrader175a93c2023-02-19 13:13:40 -0800361 "src": attr.label(providers = [JsInfo]),
Philipp Schrader87277f42022-01-01 07:45:12 -0800362 "out": attr.output(mandatory = True),
Philipp Schrader175a93c2023-02-19 13:13:40 -0800363 "suffix": attr.string(mandatory = True),
Philipp Schrader87277f42022-01-01 07:45:12 -0800364 },
365)
Philipp Schrader155e76c2023-02-25 18:42:31 -0800366
Philipp Schraderba072d92024-02-21 17:00:37 -0800367def cypress_test(name, runner, data = None, **kwargs):
Philipp Schrader155e76c2023-02-25 18:42:31 -0800368 """Runs a cypress test with the specified runner.
369
370 Args:
371 runner: The runner that starts up any necessary servers and then
372 invokes Cypress itself. See the Module API documentation for more
373 information: https://docs.cypress.io/guides/guides/module-api
374 data: The spec files (*.cy.js) and the servers under test. Also any
375 other files needed at runtime.
376 kwargs: Arguments forwarded to the upstream cypress_module_test().
377 """
378
379 # Figure out how many directories deep this package is relative to the
380 # workspace root.
381 package_depth = len(native.package_name().split("/"))
382
383 # Chrome is located at the runfiles root. So we need to go up one more
384 # directory than the workspace root.
385 chrome_location = "../" * (package_depth + 1) + "chrome_linux/chrome"
Philipp Schraderba072d92024-02-21 17:00:37 -0800386
387 copy_file(
388 name = name + "_config",
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800389 out = name + "_cypress.config.js",
Philipp Schraderba072d92024-02-21 17:00:37 -0800390 src = "//tools/build_rules/js:cypress.config.js",
391 visibility = ["//visibility:private"],
392 )
Philipp Schrader155e76c2023-02-25 18:42:31 -0800393
394 data = data or []
Philipp Schraderba072d92024-02-21 17:00:37 -0800395 data.append(":%s_config" % name)
Philipp Schrader155e76c2023-02-25 18:42:31 -0800396 data.append("@xvfb_amd64//:wrapped_bin/Xvfb")
Philipp Schraderba072d92024-02-21 17:00:37 -0800397 data.append("//:node_modules")
Philipp Schrader155e76c2023-02-25 18:42:31 -0800398
399 cypress_module_test(
Philipp Schraderba072d92024-02-21 17:00:37 -0800400 name = name,
Philipp Schrader155e76c2023-02-25 18:42:31 -0800401 args = [
402 "run",
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800403 "--config-file=%s_cypress.config.js" % name,
Philipp Schrader155e76c2023-02-25 18:42:31 -0800404 "--browser=" + chrome_location,
405 ],
406 browsers = ["@chrome_linux//:all"],
407 copy_data_to_bin = False,
408 cypress = "//:node_modules/cypress",
409 data = data,
410 runner = runner,
411 **kwargs
412 )