|
| 1 | +# Copyright Google LLC All Rights Reserved. |
| 2 | +# |
| 3 | +# Use of this source code is governed by an MIT-style license that can be |
| 4 | +# found in the LICENSE file at https://angular.io/license |
| 5 | + |
| 6 | +load("@build_bazel_rules_nodejs//:index.bzl", "npm_package_bin") |
| 7 | +load("@npm//@bazel/terser:index.bzl", "terser_minified") |
| 8 | +load("@npm//prettier:index.bzl", "prettier") |
| 9 | +load("//bazel/esbuild:index.bzl", "esbuild", "esbuild_config") |
| 10 | +load("//bazel:expand_template.bzl", "expand_template") |
| 11 | + |
| 12 | +def _create_esbuild_minify_options(debug = False): |
| 13 | + # The minify options match with the configuration used by the CLI. The whitespace |
| 14 | + # minification is left to Terser. More details can be found here: |
| 15 | + # https://github.com/angular/angular-cli/blob/0d76bf04bca6e083865972b5398a32bbe9396e14/packages/angular_devkit/build_angular/src/webpack/plugins/javascript-optimizer-worker.ts#L133. |
| 16 | + return { |
| 17 | + "minifyIdentifiers": not debug, |
| 18 | + "minifySyntax": True, |
| 19 | + "minifyWhitespace": False, |
| 20 | + # TODO: Remove when https://github.com/bazelbuild/rules_nodejs/pull/3106 landed. |
| 21 | + "ignoreAnnotations": False, |
| 22 | + } |
| 23 | + |
| 24 | +def app_bundle( |
| 25 | + name, |
| 26 | + entry_point, |
| 27 | + visibility = None, |
| 28 | + platform = "browser", |
| 29 | + target = "es2020", |
| 30 | + format = "iife", |
| 31 | + **kwargs): |
| 32 | + """ |
| 33 | + Bundles an Angular applications in an optimized way that closely matches |
| 34 | + the compilation pipeline as within the Angular CLI. |
| 35 | +
|
| 36 | + The rule produces a number of output bundles. |
| 37 | +
|
| 38 | + JS : "%{name}.js" |
| 39 | + JS minified : "%{name}.min.js" |
| 40 | + JS minified (compressed) : "%{name}.min.js.br", |
| 41 | + ---- |
| 42 | + JS debug : "%{name}.debug.js" |
| 43 | + JS debug minified : "%{name}.debug.min.js" |
| 44 | + JS debug minified (beautified) : "%{name}.debug.min.beautified.js" |
| 45 | + """ |
| 46 | + |
| 47 | + expand_template( |
| 48 | + name = "%s_config_file" % name, |
| 49 | + output_name = "%s_config.mjs" % name, |
| 50 | + template = "//bazel/benchmark/app_bundling:esbuild.config-tmpl.mjs", |
| 51 | + visibility = visibility, |
| 52 | + substitutions = { |
| 53 | + "TMPL_ENTRY_POINT_ROOTPATH": "$(rootpath %s)" % entry_point, |
| 54 | + }, |
| 55 | + data = [entry_point], |
| 56 | + ) |
| 57 | + |
| 58 | + esbuild_config( |
| 59 | + name = "%s_esbuild_config" % name, |
| 60 | + config_file = ":%s_config_file" % name, |
| 61 | + deps = [ |
| 62 | + "@npm//@angular/compiler-cli", |
| 63 | + "//shared-scripts/angular-optimization:js_lib", |
| 64 | + ], |
| 65 | + visibility = visibility, |
| 66 | + ) |
| 67 | + |
| 68 | + common_esbuild_options = dict(kwargs, **{ |
| 69 | + "config": "%s_esbuild_config" % name, |
| 70 | + "testonly": True, |
| 71 | + "entry_point": entry_point, |
| 72 | + "target": target, |
| 73 | + "platform": platform, |
| 74 | + "format": format, |
| 75 | + "sourcemap": "external", |
| 76 | + "visibility": visibility, |
| 77 | + }) |
| 78 | + |
| 79 | + common_terser_options = { |
| 80 | + "visibility": visibility, |
| 81 | + "config_file": "//bazel/benchmark/app_bundling:terser_config.json", |
| 82 | + # TODO: Enable source maps for better debugging when `@bazel/terser` pre-declares |
| 83 | + # JS and map outputs. Tracked with: DEV-120 |
| 84 | + "sourcemap": False, |
| 85 | + } |
| 86 | + |
| 87 | + esbuild( |
| 88 | + name = name, |
| 89 | + args = _create_esbuild_minify_options(False), |
| 90 | + **common_esbuild_options |
| 91 | + ) |
| 92 | + |
| 93 | + esbuild( |
| 94 | + name = "%s.debug" % name, |
| 95 | + args = _create_esbuild_minify_options(True), |
| 96 | + tags = ["manual"], |
| 97 | + **common_esbuild_options |
| 98 | + ) |
| 99 | + |
| 100 | + terser_minified(name = name + ".min", src = name + ".js", **common_terser_options) |
| 101 | + native.filegroup(name = name + ".min.js", srcs = [name + ".min"], visibility = visibility) |
| 102 | + |
| 103 | + terser_minified(name = name + ".debug.min", src = name + ".debug.js", debug = True, tags = ["manual"], **common_terser_options) |
| 104 | + native.filegroup(name = name + ".debug.min.js", srcs = [name + ".debug.min"], visibility = visibility, tags = ["manual"]) |
| 105 | + |
| 106 | + # For better debugging, we also run prettier on the minified debug bundle. This is |
| 107 | + # necessary as Terser no longer has beautify/formatting functionality. |
| 108 | + prettier( |
| 109 | + name = name + ".debug.min.beautified", |
| 110 | + args = ["$(execpath %s)" % (name + ".debug.min")], |
| 111 | + # The `outs` attribute needs to be set when `stdout` is captured as an output. |
| 112 | + outs = [], |
| 113 | + stdout = name + ".debug.min.beautified.js", |
| 114 | + data = [name + ".debug.min"], |
| 115 | + visibility = visibility, |
| 116 | + tags = ["manual"], |
| 117 | + ) |
| 118 | + |
| 119 | + npm_package_bin( |
| 120 | + name = "_%s_brotli" % name, |
| 121 | + tool = "//bazel/benchmark/brotli-cli", |
| 122 | + data = [name + ".min.js"], |
| 123 | + outs = [name + ".min.js.br"], |
| 124 | + args = [ |
| 125 | + "--output=$(execpath %s.min.js.br)" % name, |
| 126 | + "$(execpath %s.min.js)" % name, |
| 127 | + ], |
| 128 | + visibility = visibility, |
| 129 | + ) |
0 commit comments