blob: bb15c2c01cc060a8da52df7addac0f67a7d63375 [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 + [
Philipp Schrader3d7dedc2024-03-16 16:27:25 -0700255 "**/*.jinja2.*",
Philipp Schrader175a93c2023-02-19 13:13:40 -0800256 "public-api.ts",
257 ],
258 ) + extra_srcs
259
260 # An index file to allow direct imports of the directory similar to a package.json "main"
261 write_file(
262 name = "_index",
263 out = "index.ts",
264 content = ["export * from \"./public-api\";"],
265 visibility = ["//visibility:private"],
266 )
267
268 if generate_public_api:
269 write_file(
270 name = "_public_api",
271 out = "public-api.ts",
272 content = [
273 "export * from './%s.component';" % name,
274 "export * from './%s.module';" % name,
275 ],
276 visibility = ["//visibility:private"],
277 )
278 srcs.append(":_public_api")
279
280 ng_project(
Philipp Schradere5d13942024-03-17 15:44:35 -0700281 name = "_lib",
Philipp Schrader175a93c2023-02-19 13:13:40 -0800282 srcs = srcs + [":_index"],
283 deps = deps + PACKAGE_DEPS,
Philipp Schradere5d13942024-03-17 15:44:35 -0700284 visibility = ["//visibility:private"],
Philipp Schrader175a93c2023-02-19 13:13:40 -0800285 **kwargs
286 )
287
Philipp Schradere5d13942024-03-17 15:44:35 -0700288 npm_package(
289 name = name,
290 srcs = ["package.json", ":_lib"],
291 include_runfiles = False,
292 visibility = visibility,
293 )
294
Philipp Schraderba072d92024-02-21 17:00:37 -0800295def rollup_bundle(name, entry_point, node_modules = "//:node_modules", deps = [], visibility = None, **kwargs):
Philipp Schrader87277f42022-01-01 07:45:12 -0800296 """Calls the upstream rollup_bundle() and exposes a .min.js file.
297
298 Legacy version of rollup_bundle() used to provide the .min.js file. This
299 wrapper provides the same interface by explicitly exposing a .min.js file.
300 """
Philipp Schrader175a93c2023-02-19 13:13:40 -0800301 copy_file(
302 name = name + "__rollup_config",
303 src = "//:rollup.config.js",
304 out = name + "__rollup_config.js",
305 )
306
Philipp Schrader87277f42022-01-01 07:45:12 -0800307 upstream_rollup_bundle(
308 name = name,
309 visibility = visibility,
310 deps = deps + [
Philipp Schrader175a93c2023-02-19 13:13:40 -0800311 "//:node_modules/@rollup/plugin-node-resolve",
Philipp Schrader87277f42022-01-01 07:45:12 -0800312 ],
Philipp Schraderba072d92024-02-21 17:00:37 -0800313 node_modules = node_modules,
Philipp Schrader175a93c2023-02-19 13:13:40 -0800314 sourcemap = "false",
315 config_file = ":%s__rollup_config.js" % name,
316 entry_point = entry_point,
Philipp Schrader87277f42022-01-01 07:45:12 -0800317 **kwargs
318 )
319
320 terser_minified(
321 name = name + "__min",
Philipp Schrader175a93c2023-02-19 13:13:40 -0800322 srcs = [name + ".js"],
Philipp Schraderba072d92024-02-21 17:00:37 -0800323 node_modules = node_modules,
Austin Schuhf737d472023-07-29 17:35:59 -0700324 tags = [
325 "no-remote-cache",
326 ],
Philipp Schrader175a93c2023-02-19 13:13:40 -0800327 sourcemap = False,
Philipp Schrader87277f42022-01-01 07:45:12 -0800328 )
329
330 # Copy the __min.js file (a declared output inside the rule) so that it's a
331 # pre-declared output and publicly visible. I.e. via attr.output() below.
Philipp Schrader175a93c2023-02-19 13:13:40 -0800332 _expose_file_with_suffix(
Philipp Schrader87277f42022-01-01 07:45:12 -0800333 name = name + "__min_exposed",
334 src = ":%s__min" % name,
335 out = name + ".min.js",
Philipp Schrader175a93c2023-02-19 13:13:40 -0800336 suffix = "__min.js",
Philipp Schrader87277f42022-01-01 07:45:12 -0800337 visibility = visibility,
338 )
339
Philipp Schrader175a93c2023-02-19 13:13:40 -0800340def _expose_file_with_suffix_impl(ctx):
Philipp Schrader87277f42022-01-01 07:45:12 -0800341 """Copies the .min.js file in order to make it publicly accessible."""
Philipp Schrader175a93c2023-02-19 13:13:40 -0800342 sources = ctx.attr.src[JsInfo].sources.to_list()
Philipp Schrader87277f42022-01-01 07:45:12 -0800343 min_js = None
344 for src in sources:
Philipp Schrader175a93c2023-02-19 13:13:40 -0800345 if src.basename.endswith(ctx.attr.suffix):
Philipp Schrader87277f42022-01-01 07:45:12 -0800346 min_js = src
347 break
348
349 if min_js == None:
350 fail("Couldn't find .min.js in " + str(ctx.attr.src))
351
352 ctx.actions.run(
353 inputs = [min_js],
354 outputs = [ctx.outputs.out],
355 executable = "cp",
356 arguments = [min_js.path, ctx.outputs.out.path],
357 )
358
Philipp Schrader175a93c2023-02-19 13:13:40 -0800359_expose_file_with_suffix = rule(
360 implementation = _expose_file_with_suffix_impl,
Philipp Schrader87277f42022-01-01 07:45:12 -0800361 attrs = {
Philipp Schrader175a93c2023-02-19 13:13:40 -0800362 "src": attr.label(providers = [JsInfo]),
Philipp Schrader87277f42022-01-01 07:45:12 -0800363 "out": attr.output(mandatory = True),
Philipp Schrader175a93c2023-02-19 13:13:40 -0800364 "suffix": attr.string(mandatory = True),
Philipp Schrader87277f42022-01-01 07:45:12 -0800365 },
366)
Philipp Schrader155e76c2023-02-25 18:42:31 -0800367
Philipp Schraderba072d92024-02-21 17:00:37 -0800368def cypress_test(name, runner, data = None, **kwargs):
Philipp Schrader155e76c2023-02-25 18:42:31 -0800369 """Runs a cypress test with the specified runner.
370
371 Args:
372 runner: The runner that starts up any necessary servers and then
373 invokes Cypress itself. See the Module API documentation for more
374 information: https://docs.cypress.io/guides/guides/module-api
375 data: The spec files (*.cy.js) and the servers under test. Also any
376 other files needed at runtime.
377 kwargs: Arguments forwarded to the upstream cypress_module_test().
378 """
379
380 # Figure out how many directories deep this package is relative to the
381 # workspace root.
382 package_depth = len(native.package_name().split("/"))
383
384 # Chrome is located at the runfiles root. So we need to go up one more
385 # directory than the workspace root.
386 chrome_location = "../" * (package_depth + 1) + "chrome_linux/chrome"
Philipp Schraderba072d92024-02-21 17:00:37 -0800387
388 copy_file(
389 name = name + "_config",
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800390 out = name + "_cypress.config.js",
Philipp Schraderba072d92024-02-21 17:00:37 -0800391 src = "//tools/build_rules/js:cypress.config.js",
392 visibility = ["//visibility:private"],
393 )
Philipp Schrader155e76c2023-02-25 18:42:31 -0800394
395 data = data or []
Philipp Schraderba072d92024-02-21 17:00:37 -0800396 data.append(":%s_config" % name)
Philipp Schrader155e76c2023-02-25 18:42:31 -0800397 data.append("@xvfb_amd64//:wrapped_bin/Xvfb")
Philipp Schraderba072d92024-02-21 17:00:37 -0800398 data.append("//:node_modules")
Philipp Schrader155e76c2023-02-25 18:42:31 -0800399
400 cypress_module_test(
Philipp Schraderba072d92024-02-21 17:00:37 -0800401 name = name,
Philipp Schrader155e76c2023-02-25 18:42:31 -0800402 args = [
403 "run",
Philipp Schradere2e27ff2024-02-25 22:08:55 -0800404 "--config-file=%s_cypress.config.js" % name,
Philipp Schrader155e76c2023-02-25 18:42:31 -0800405 "--browser=" + chrome_location,
406 ],
407 browsers = ["@chrome_linux//:all"],
408 copy_data_to_bin = False,
409 cypress = "//:node_modules/cypress",
410 data = data,
411 runner = runner,
412 **kwargs
413 )