blob: d334764d87d83457acb9e0847592060a42ba263e [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")
Philipp Schrader3de4dfc2023-02-15 20:18:25 -08006load("@npm//@bazel/typescript:index.bzl", upstream_ts_library = "ts_library", upstream_ts_project = "ts_project")
7
8def ts_project(**kwargs):
9 """A trivial wrapper to prepare for the rules_js migration.
10
11 The intent is to change his macro to wrap the new rules_js ts_project
12 implementation.
13 """
14 upstream_ts_library(**kwargs)
Philipp Schrader87277f42022-01-01 07:45:12 -080015
16def rollup_bundle(name, deps, visibility = None, **kwargs):
17 """Calls the upstream rollup_bundle() and exposes a .min.js file.
18
19 Legacy version of rollup_bundle() used to provide the .min.js file. This
20 wrapper provides the same interface by explicitly exposing a .min.js file.
21 """
22 upstream_rollup_bundle(
23 name = name,
24 visibility = visibility,
25 deps = deps + [
26 "@npm//@rollup/plugin-node-resolve",
27 ],
28 config_file = "//:rollup.config.js",
29 link_workspace_root = True,
30 **kwargs
31 )
32
33 terser_minified(
34 name = name + "__min",
35 src = name + ".js",
36 )
37
38 # Copy the __min.js file (a declared output inside the rule) so that it's a
39 # pre-declared output and publicly visible. I.e. via attr.output() below.
40 _expose_minified_js(
41 name = name + "__min_exposed",
42 src = ":%s__min" % name,
43 out = name + ".min.js",
44 visibility = visibility,
45 )
46
47def _expose_minified_js_impl(ctx):
48 """Copies the .min.js file in order to make it publicly accessible."""
49 sources = ctx.attr.src[JSModuleInfo].sources.to_list()
50 min_js = None
51 for src in sources:
52 if src.basename.endswith("__min.js"):
53 min_js = src
54 break
55
56 if min_js == None:
57 fail("Couldn't find .min.js in " + str(ctx.attr.src))
58
59 ctx.actions.run(
60 inputs = [min_js],
61 outputs = [ctx.outputs.out],
62 executable = "cp",
63 arguments = [min_js.path, ctx.outputs.out.path],
64 )
65
66_expose_minified_js = rule(
67 implementation = _expose_minified_js_impl,
68 attrs = {
69 "src": attr.label(providers = [JSModuleInfo]),
70 "out": attr.output(mandatory = True),
71 },
72)
Philipp Schrader684a8e82022-02-25 17:39:28 -080073
Philipp Schraderaa76a692022-05-29 23:55:16 -070074def protractor_ts_test(name, srcs, deps = None, data = None, **kwargs):
Philipp Schraderd999c9f2022-02-27 15:48:58 -080075 """Wraps upstream protractor_web_test_suite() to reduce boilerplate.
76
77 This is largely based on the upstream protractor example:
78 https://github.com/bazelbuild/rules_nodejs/blob/stable/examples/angular/e2e/BUILD.bazel
79
80 See the documentation for more information:
81 https://bazelbuild.github.io/rules_nodejs/Protractor.html#protractor_web_test_suite
82 """
Philipp Schrader3de4dfc2023-02-15 20:18:25 -080083 upstream_ts_project(
Philipp Schraderd999c9f2022-02-27 15:48:58 -080084 name = name + "__lib",
85 srcs = srcs,
86 testonly = 1,
87 deps = (deps or []) + [
88 # Implicit deps that are necessary to get tests of this kind to
89 # work.
90 "@npm//@types/jasmine",
91 "@npm//jasmine",
92 "@npm//protractor",
93 "@npm//@types/node",
94 ],
95 tsconfig = {},
96 declaration = True,
97 declaration_map = True,
98 )
99
Philipp Schraderaa76a692022-05-29 23:55:16 -0700100 data = (data or []) + [
101 "//tools/build_rules/js/waitpid_module",
102 ]
103
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800104 protractor_web_test_suite(
105 name = name,
106 srcs = [paths.replace_extension(src, ".js") for src in srcs],
107 deps = [":%s__lib" % name],
Philipp Schraderaa76a692022-05-29 23:55:16 -0700108 data = data,
Philipp Schraderd999c9f2022-02-27 15:48:58 -0800109 **kwargs
110 )