Philipp Schrader | 87277f4 | 2022-01-01 07:45:12 -0800 | [diff] [blame] | 1 | load("@build_bazel_rules_nodejs//:providers.bzl", "JSModuleInfo") |
| 2 | load("@npm//@bazel/rollup:index.bzl", upstream_rollup_bundle = "rollup_bundle") |
| 3 | load("@npm//@bazel/terser:index.bzl", "terser_minified") |
Philipp Schrader | d999c9f | 2022-02-27 15:48:58 -0800 | [diff] [blame^] | 4 | load("@bazel_skylib//lib:paths.bzl", "paths") |
| 5 | load("@npm//@bazel/protractor:index.bzl", "protractor_web_test_suite") |
| 6 | load("@npm//@bazel/typescript:index.bzl", "ts_project") |
Philipp Schrader | 87277f4 | 2022-01-01 07:45:12 -0800 | [diff] [blame] | 7 | |
| 8 | def 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 | |
| 39 | def _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 Schrader | 684a8e8 | 2022-02-25 17:39:28 -0800 | [diff] [blame] | 65 | |
| 66 | # Some rules (e.g. babel()) do not expose their files as runfiles. So we need |
| 67 | # to do this step manually. |
| 68 | def _turn_files_into_runfiles_impl(ctx): |
| 69 | files = ctx.attr.files.files |
| 70 | return [DefaultInfo( |
| 71 | files = files, |
| 72 | runfiles = ctx.runfiles(transitive_files = files), |
| 73 | )] |
| 74 | |
| 75 | turn_files_into_runfiles = rule( |
| 76 | implementation = _turn_files_into_runfiles_impl, |
| 77 | attrs = { |
| 78 | "files": attr.label( |
| 79 | mandatory = True, |
| 80 | doc = "The target whose files should be turned into runfiles.", |
| 81 | ), |
| 82 | }, |
| 83 | ) |
Philipp Schrader | d999c9f | 2022-02-27 15:48:58 -0800 | [diff] [blame^] | 84 | |
| 85 | def protractor_ts_test(name, srcs, deps = None, **kwargs): |
| 86 | """Wraps upstream protractor_web_test_suite() to reduce boilerplate. |
| 87 | |
| 88 | This is largely based on the upstream protractor example: |
| 89 | https://github.com/bazelbuild/rules_nodejs/blob/stable/examples/angular/e2e/BUILD.bazel |
| 90 | |
| 91 | See the documentation for more information: |
| 92 | https://bazelbuild.github.io/rules_nodejs/Protractor.html#protractor_web_test_suite |
| 93 | """ |
| 94 | ts_project( |
| 95 | name = name + "__lib", |
| 96 | srcs = srcs, |
| 97 | testonly = 1, |
| 98 | deps = (deps or []) + [ |
| 99 | # Implicit deps that are necessary to get tests of this kind to |
| 100 | # work. |
| 101 | "@npm//@types/jasmine", |
| 102 | "@npm//jasmine", |
| 103 | "@npm//protractor", |
| 104 | "@npm//@types/node", |
| 105 | ], |
| 106 | tsconfig = {}, |
| 107 | declaration = True, |
| 108 | declaration_map = True, |
| 109 | ) |
| 110 | |
| 111 | protractor_web_test_suite( |
| 112 | name = name, |
| 113 | srcs = [paths.replace_extension(src, ".js") for src in srcs], |
| 114 | deps = [":%s__lib" % name], |
| 115 | **kwargs |
| 116 | ) |