Skip to content

Commit f7b5497

Browse files
authored
build: update size test tool to work with Angular v13 (#23811)
* test: remove unnecessary boostrap module calls in size tests * build: add comment explaining the processing of `.mjs` in esbuild rule Adds a little comment explaining why`.mjs` is processed in the Angular esbuild rule. * build: update size test tool to work with Angular v13 Updates the size test tool to properly run with the Angular v13 compilation pipeline, matching conceptually with what the Angular CLI performs. i.e. running the Angular linker, running the non-deprecated build-optimizer variant (i.e. the Babel plugins). * test: update size-golden to reflect recent compilation pipeline changes
1 parent 656320d commit f7b5497

File tree

10 files changed

+138
-84
lines changed

10 files changed

+138
-84
lines changed

goldens/size-test.yaml

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
cdk/drag-drop/all-directives: 160859
2-
cdk/drag-drop/basic: 158225
3-
material-experimental/mdc-chips/basic: 385551
4-
material-experimental/mdc-form-field/advanced: 417584
5-
material-experimental/mdc-form-field/basic: 416339
6-
material/autocomplete/without-optgroup: 392028
7-
material/button-toggle/standalone: 124412
8-
material/chips/basic: 320073
9-
material/datepicker/range-picker/without-form-field: 505044
10-
material/expansion/without-accordion: 330526
11-
material/form-field/advanced: 377468
12-
material/form-field/basic: 376144
13-
material/list/nav-list: 328072
14-
material/menu/without-lazy-content: 398590
15-
material/radio/without-group: 127571
16-
material/select/basic: 437305
17-
material/tabs/advanced: 369608
18-
material/tabs/basic: 368747
1+
cdk/drag-drop/all-directives: 155091
2+
cdk/drag-drop/basic: 152522
3+
material-experimental/mdc-chips/basic: 249660
4+
material-experimental/mdc-form-field/advanced: 296409
5+
material-experimental/mdc-form-field/basic: 294855
6+
material/autocomplete/without-optgroup: 281544
7+
material/button-toggle/standalone: 186619
8+
material/chips/basic: 228157
9+
material/datepicker/range-picker/without-form-field: 398783
10+
material/expansion/without-accordion: 200184
11+
material/form-field/advanced: 247973
12+
material/form-field/basic: 246355
13+
material/list/nav-list: 194468
14+
material/menu/without-lazy-content: 286831
15+
material/radio/without-group: 189803
16+
material/select/basic: 329893
17+
material/tabs/advanced: 248653
18+
material/tabs/basic: 247787

integration/size-test/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@npm//@bazel/esbuild:index.bzl", "esbuild_config")
12
load("//tools:defaults.bzl", "ts_library")
23

34
package(default_visibility = ["//visibility:public"])
@@ -8,6 +9,16 @@ exports_files([
89
"index-tmpl.ts",
910
])
1011

12+
esbuild_config(
13+
name = "esbuild_config",
14+
config_file = "esbuild.config.mjs",
15+
deps = [
16+
"@npm//@angular-devkit/build-angular",
17+
"@npm//@angular/compiler-cli",
18+
"@npm//@babel/core",
19+
],
20+
)
21+
1122
ts_library(
1223
name = "check-size",
1324
srcs = ["check-size.ts"],
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 babel from '@babel/core';
10+
import {createEs2015LinkerPlugin} from '@angular/compiler-cli/linker/babel';
11+
import {ConsoleLogger, NodeJSFileSystem, LogLevel} from '@angular/compiler-cli';
12+
import {GLOBAL_DEFS_FOR_TERSER_WITH_AOT} from '@angular/compiler-cli/private/tooling';
13+
import adjustStaticClassMembersPlugin from '@angular-devkit/build-angular/src/babel/plugins/adjust-static-class-members.js';
14+
import elideAngularMetadataPlugin from '@angular-devkit/build-angular/src/babel/plugins/elide-angular-metadata.js';
15+
import adjustTypeScriptEnumsPlugin from '@angular-devkit/build-angular/src/babel/plugins/adjust-typescript-enums.js';
16+
import pureToplevelFunctionsPlugin from '@angular-devkit/build-angular/src/babel/plugins/pure-toplevel-functions.js';
17+
import fs from 'fs';
18+
19+
/** Babel plugin running the Angular linker. */
20+
const linkerBabelPlugin = createEs2015LinkerPlugin({
21+
fileSystem: new NodeJSFileSystem(),
22+
logger: new ConsoleLogger(LogLevel.warn),
23+
linkerJitMode: false,
24+
});
25+
26+
/**
27+
* ESBuild plugin configuring various optimization Babel plugins. The Babel plugins
28+
* configured as part of this plugin run in the Angular CLI compilation pipeline as well.
29+
*/
30+
const esbuildBabelOptimizePlugin = {
31+
name: 'ng-babel-optimize-esbuild',
32+
setup: build => {
33+
build.onLoad({filter: /.*/}, async args => {
34+
const filePath = args.path;
35+
const content = await fs.promises.readFile(filePath, 'utf8');
36+
const plugins = [
37+
linkerBabelPlugin,
38+
adjustStaticClassMembersPlugin,
39+
elideAngularMetadataPlugin,
40+
adjustTypeScriptEnumsPlugin,
41+
];
42+
43+
// All files except for the auto-generated module entry-point are considered side-effect
44+
// free. For these we can add the pure-top level Babel plugin. This matches conceptually
45+
// with what is done in the Angular CLI compilation pipeline, with respect to everything
46+
// in this repo being an official side-effect free APF package.
47+
if (!args.path.includes('autogenerated_module_index.mjs')) {
48+
plugins.push(pureToplevelFunctionsPlugin);
49+
}
50+
51+
const {code} = await babel.transformAsync(content, {
52+
filename: filePath,
53+
filenameRelative: filePath,
54+
plugins: plugins,
55+
// Sourcemaps are generated inline so that ESBuild can process them.
56+
sourceMaps: 'inline',
57+
compact: false,
58+
});
59+
60+
return {contents: code};
61+
});
62+
},
63+
};
64+
65+
export default {
66+
// Note: We prefer `.mjs` here as this is the extension used by Angular APF packages.
67+
resolveExtensions: ['.mjs', '.js'],
68+
conditions: ['es2020', 'es2015'],
69+
mainFields: ['fesm2020', 'es2020', 'es2015', 'module'],
70+
format: 'iife',
71+
// The majority of these options match with the ones the CLI sets:
72+
// https://github.com/angular/angular-cli/blob/0d76bf04bca6e083865972b5398a32bbe9396e14/packages/angular_devkit/build_angular/src/webpack/plugins/javascript-optimizer-worker.ts#L133.
73+
treeShaking: true,
74+
minifyIdentifiers: true,
75+
minifySyntax: true,
76+
minifyWhitespace: false,
77+
pure: ['forwardRef'],
78+
legalComments: 'none',
79+
// ESBuild requires the `define` option to take a string-based dictionary.
80+
define: convertObjectToStringDictionary(GLOBAL_DEFS_FOR_TERSER_WITH_AOT),
81+
plugins: [esbuildBabelOptimizePlugin],
82+
};
83+
84+
/** Converts an object to a string dictionary. */
85+
function convertObjectToStringDictionary(value) {
86+
return Object.entries(value).reduce((result, [propName, value]) => {
87+
result[propName] = String(value);
88+
return result;
89+
}, {});
90+
}

integration/size-test/index.bzl

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
load("@npm//@angular/dev-infra-private/bazel:expand_template.bzl", "expand_template")
22
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary", "nodejs_test")
33
load("@bazel_skylib//lib:paths.bzl", "paths")
4-
load("@npm//@bazel/rollup:index.bzl", "rollup_bundle")
4+
load("@npm//@bazel/esbuild:index.bzl", "esbuild")
55
load("@npm//@bazel/terser:index.bzl", "terser_minified")
66
load("//tools:defaults.bzl", "ng_module")
77

88
"""
99
Performs size measurements for the specified file. The file will be built as part
10-
of a `ng_module` and then will be optimized with build-optimizer, rollup and Terser.
10+
of a `ng_module` and then will be optimized with esbuild, babel and terser.
1111
1212
The resulting size will be validated against a golden file to ensure that we don't
1313
regress in payload size, or that we can improvements to payload size.
@@ -35,31 +35,30 @@ def size_test(name, file, deps):
3535
testonly = True,
3636
deps = [
3737
"@npm//@angular/core",
38-
"@npm//@angular/platform-browser-dynamic",
38+
"@npm//@angular/platform-browser",
3939
] + deps,
4040
)
4141

42-
rollup_bundle(
42+
esbuild(
4343
name = "%s_bundle" % name,
44-
config_file = "//integration/size-test:rollup.config.js",
44+
config = "//integration/size-test:esbuild_config",
4545
testonly = True,
46-
entry_points = {
47-
(index_file): "%s_bundled" % name,
48-
},
46+
minify = True,
47+
entry_point = index_file,
4948
deps = [
5049
":%s_lib" % name,
51-
"@npm//@rollup/plugin-node-resolve",
52-
"@npm//@angular-devkit/build-optimizer",
5350
],
51+
target = "es2020",
52+
platform = "browser",
5453
# Link the workspace root so that files can be loaded from the workspace.
5554
link_workspace_root = True,
56-
sourcemap = "false",
55+
sourcemap = "external",
5756
)
5857

5958
terser_minified(
6059
testonly = True,
6160
name = "%s_bundle_min" % name,
62-
src = ":%s_bundle" % name,
61+
src = "%s_bundle" % name,
6362
config_file = "//integration/size-test:terser-config.json",
6463
sourcemap = False,
6564
)

integration/size-test/material-experimental/mdc-chips/basic.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {Component, NgModule} from '@angular/core';
2-
import {platformBrowser} from '@angular/platform-browser';
32
import {MatChipsModule} from '@angular/material-experimental/mdc-chips';
43

54
/**
@@ -21,5 +20,3 @@ export class TestComponent {}
2120
bootstrap: [TestComponent],
2221
})
2322
export class AppModule {}
24-
25-
platformBrowser().bootstrapModule(AppModule);

integration/size-test/material/chips/basic.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {Component, NgModule} from '@angular/core';
2-
import {platformBrowser} from '@angular/platform-browser';
32
import {MatChipsModule} from '@angular/material/chips';
43

54
/**
@@ -21,5 +20,3 @@ export class TestComponent {}
2120
bootstrap: [TestComponent],
2221
})
2322
export class AppModule {}
24-
25-
platformBrowser().bootstrapModule(AppModule);

integration/size-test/rollup.config.js

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
{
2-
"output": {
3-
"ecma": "es2015",
4-
"comments": false
5-
},
2+
"ecma": "es2020",
63
"compress": {
7-
"global_defs": {
8-
"ngDevMode": false,
9-
"ngI18nClosureMode": false,
10-
"ngJitMode": false
11-
},
12-
"passes": 3,
4+
"passes": 2,
135
"pure_getters": true
146
},
15-
"toplevel": true,
16-
"mangle": true
7+
"format": {
8+
"ascii_only": true,
9+
"wrap_func_args": false
10+
},
11+
"mangle": false
1712
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
},
6868
"devDependencies": {
6969
"@angular-devkit/build-angular": "13.0.0-next.7",
70-
"@angular-devkit/build-optimizer": "0.1300.0-next.7",
7170
"@angular-devkit/core": "13.0.0-next.7",
7271
"@angular-devkit/schematics": "13.0.0-next.7",
7372
"@angular/bazel": "13.0.0-next.15",

tools/angular/esbuild.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {createLinkerEsbuildPlugin} from './create_linker_esbuild_plugin.mjs';
1010

1111
export default {
12+
// Note: We support `.mjs` here as this is the extension used by Angular APF packages.
1213
resolveExtensions: ['.mjs', '.js'],
1314
format: 'esm',
1415
plugins: [

0 commit comments

Comments
 (0)