豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 885008c

Browse files
committed
fix(bazel): benchmark angular compilation pipeline not working with APF v13
Fixes that the Angular compilation pipeline for benchmarks is not working with APF v13. This commit fixes it by bring the pipeline in sync with what is done in the CLI, and also providing a shared script that can be used by other size-tests in e.g. Angular Components or framework.
1 parent dccdbf0 commit 885008c

File tree

29 files changed

+5091
-603
lines changed

29 files changed

+5091
-603
lines changed

.bazelrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ test --nolegacy_external_runfiles
1717
build --deleted_packages=bazel/integration/tests/nested_bazel_workspaces/basic
1818
query --deleted_packages=bazel/integration/tests/nested_bazel_workspaces/basic
1919

20+
build --define=angular_ivy_enabled=True
21+
2022
###############################
2123
# Filesystem interactions #
2224
###############################

bazel/benchmark/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ package(default_visibility = ["//visibility:public"])
44
filegroup(
55
name = "files",
66
srcs = glob(["*"]) + [
7+
"//bazel/benchmark/app_bundling:files",
78
"//bazel/benchmark/brotli-cli:files",
89
"//bazel/benchmark/component_benchmark:files",
9-
"//bazel/benchmark/ng_rollup_bundle:files",
1010
"//bazel/benchmark/driver-utilities:files",
1111
],
1212
)

bazel/benchmark/app_bundling/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package(default_visibility = ["//visibility:public"])
22

33
exports_files([
4-
"rollup.config-tmpl.js",
54
"terser_config.json",
5+
"esbuild.config-tmpl.mjs",
66
])
77

88
# Make source files available for distribution via pkg_npm
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import * as path from 'path';
10+
11+
import {createEsbuildAngularOptimizePlugin} from '@angular/dev-infra-private/shared-scripts/angular-optimization/esbuild-plugin.mjs';
12+
import {createEs2015LinkerPlugin} from '@angular/compiler-cli/linker/babel';
13+
import {ConsoleLogger, NodeJSFileSystem, LogLevel} from '@angular/compiler-cli';
14+
import {GLOBAL_DEFS_FOR_TERSER_WITH_AOT} from '@angular/compiler-cli/private/tooling';
15+
16+
/** Root path pointing to the app bundle source entry-point file. */
17+
const entryPointSourceRootPath = path.normalize(`TMPL_ENTRY_POINT_ROOTPATH`);
18+
19+
/**
20+
* Root path to the bundle entry-point without extension.
21+
*
22+
* The extension of the bundle entry-point file is not known because the ESBuild rule
23+
* strips the source extension (like `.ts`) and resolves the file based on the resolved inputs.
24+
*/
25+
const entryPointBasepath = entryPointSourceRootPath.replace(/\.[^.]+$/, '');
26+
27+
/** Whether the given file is considered side-effect free. */
28+
function isFileSideEffectFree(filePath) {
29+
// All files except for the entry-point are considered side-effect free. We naively
30+
// check using `includes` as a root path is quite unique regardless.
31+
return !filePath.includes(entryPointBasepath);
32+
}
33+
34+
/** Babel plugin running the Angular linker. */
35+
const linkerBabelPlugin = createEs2015LinkerPlugin({
36+
fileSystem: new NodeJSFileSystem(),
37+
logger: new ConsoleLogger(LogLevel.warn),
38+
linkerJitMode: false,
39+
});
40+
41+
export default {
42+
// Note: We prefer `.mjs` here as this is the extension used by Angular APF packages.
43+
resolveExtensions: ['.mjs', '.js'],
44+
conditions: ['es2020', 'es2015'],
45+
mainFields: ['fesm2020', 'es2020', 'es2015', 'module', 'main'],
46+
// The majority of these options match with the ones the CLI sets:
47+
// https://github.com/angular/angular-cli/blob/0d76bf04bca6e083865972b5398a32bbe9396e14/packages/angular_devkit/build_angular/src/webpack/plugins/javascript-optimizer-worker.ts#L133.
48+
treeShaking: true,
49+
pure: ['forwardRef'],
50+
legalComments: 'none',
51+
// ESBuild requires the `define` option to take a string-based dictionary.
52+
define: convertObjectToStringDictionary(GLOBAL_DEFS_FOR_TERSER_WITH_AOT),
53+
plugins: [createEsbuildAngularOptimizePlugin(isFileSideEffectFree, [linkerBabelPlugin])],
54+
};
55+
56+
/** Converts an object to a string dictionary. */
57+
function convertObjectToStringDictionary(value) {
58+
return Object.entries(value).reduce((result, [propName, value]) => {
59+
result[propName] = String(value);
60+
return result;
61+
}, {});
62+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
)

bazel/benchmark/app_bundling/ng_rollup_bundle.bzl

Lines changed: 0 additions & 104 deletions
This file was deleted.

0 commit comments

Comments
 (0)