blob: 55102669ce1b50da4cf2a530e8fe2ef74ae4368c [file] [log] [blame]
Philipp Schrader87277f42022-01-01 07:45:12 -08001load("@build_bazel_rules_nodejs//:providers.bzl", "JSModuleInfo")
2load("@npm//@bazel/rollup:index.bzl", upstream_rollup_bundle = "rollup_bundle")
3load("@npm//@bazel/terser:index.bzl", "terser_minified")
Philipp Schraderd999c9f2022-02-27 15:48:58 -08004load("@bazel_skylib//lib:paths.bzl", "paths")
5load("@npm//@bazel/protractor:index.bzl", "protractor_web_test_suite")
6load("@npm//@bazel/typescript:index.bzl", "ts_project")
Philipp Schrader87277f42022-01-01 07:45:12 -08007
8def rollup_bundle(name, deps, visibility = None, **kwargs):
9 """Calls the upstream rollup_bundle() and exposes a .min.js file.
10
11 Legacy version of rollup_bundle() used to provide the .min.js file. This
12 wrapper provides the same interface by explicitly exposing a .min.js file.
13 """
14 upstream_rollup_bundle(
15 name = name,
16 visibility = visibility,
17 deps = deps + [
18 "@npm//@rollup/plugin-node-resolve",
19 ],
20 config_file = "//:rollup.config.js",
21 link_workspace_root = True,
22 **kwargs
23 )
24
25 terser_minified(
26 name = name + "__min",
27 src = name + ".js",
28 )
29
30 # Copy the __min.js file (a declared output inside the rule) so that it's a
31 # pre-declared output and publicly visible. I.e. via attr.output() below.
32 _expose_minified_js(
33 name = name + "__min_exposed",
34 src = ":%s__min" % name,
35 out = name + ".min.js",
36 visibility = visibility,
37 )
38
39def _expose_minified_js_impl(ctx):
40 """Copies the .min.js file in order to make it publicly accessible."""
41 sources = ctx.attr.src[JSModuleInfo].sources.to_list()
42 min_js = None
43 for src in sources:
44 if src.basename.endswith("__min.js"):
45 min_js = src
46 break
47
48 if min_js == None:
49 fail("Couldn't find .min.js in " + str(ctx.attr.src))
50
51 ctx.actions.run(
52 inputs = [min_js],
53 outputs = [ctx.outputs.out],
54 executable = "cp",
55 arguments = [min_js.path, ctx.outputs.out.path],
56 )
57
58_expose_minified_js = rule(
59 implementation = _expose_minified_js_impl,
60 attrs = {
61 "src": attr.label(providers = [JSModuleInfo]),
62 "out": attr.output(mandatory = True),
63 },
64)
Philipp Schrader684a8e82022-02-25 17:39:28 -080065
Philipp Schraderaa76a692022-05-29 23:55:16 -070066def protractor_ts_test(name, srcs, deps = None, data = None, **kwargs):
Philipp Schraderd999c9f2022-02-27 15:48:58 -080067 """Wraps upstream protractor_web_test_suite() to reduce boilerplate.
68
69 This is largely based on the upstream protractor example:
70 https://github.com/bazelbuild/rules_nodejs/blob/stable/examples/angular/e2e/BUILD.bazel
71
72 See the documentation for more information:
73 https://bazelbuild.github.io/rules_nodejs/Protractor.html#protractor_web_test_suite
74 """
75 ts_project(
76 name = name + "__lib",
77 srcs = srcs,
78 testonly = 1,
79 deps = (deps or []) + [
80 # Implicit deps that are necessary to get tests of this kind to
81 # work.
82 "@npm//@types/jasmine",
83 "@npm//jasmine",
84 "@npm//protractor",
85 "@npm//@types/node",
86 ],
87 tsconfig = {},
88 declaration = True,
89 declaration_map = True,
90 )
91
Philipp Schraderaa76a692022-05-29 23:55:16 -070092 data = (data or []) + [
93 "//tools/build_rules/js/waitpid_module",
94 ]
95
Philipp Schraderd999c9f2022-02-27 15:48:58 -080096 protractor_web_test_suite(
97 name = name,
98 srcs = [paths.replace_extension(src, ".js") for src in srcs],
99 deps = [":%s__lib" % name],
Philipp Schraderaa76a692022-05-29 23:55:16 -0700100 data = data,
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800101 **kwargs
102 )