-
Notifications
You must be signed in to change notification settings - Fork 6.8k
refactor(list): use light-weight tokens for injecting parent lists #19568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andrewseguin
merged 2 commits into
angular:master
from
devversion:refactor/size-improvement-light-weight-tokens
Jun 11, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
exports_files([ | ||
"size-test.json", | ||
]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"material/list": 215881 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
load("//tools:defaults.bzl", "ts_library") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
exports_files([ | ||
"rollup.config.js", | ||
"terser-config.json", | ||
]) | ||
|
||
ts_library( | ||
name = "check-size", | ||
srcs = ["check-size.ts"], | ||
deps = [ | ||
"@npm//@types/node", | ||
"@npm//chalk", | ||
], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Script that measures the size of a given test file and compares it | ||
* with an entry in a file size golden. If the size deviates by certain | ||
* amount, the script will fail with a non-zero exit code. | ||
*/ | ||
|
||
import chalk from 'chalk'; | ||
import {readFileSync, statSync, writeFileSync} from 'fs'; | ||
|
||
/** | ||
* Absolute byte deviation from the expected value that is allowed. If the | ||
* size deviates by 500 bytes of the expected value, the script will fail. | ||
*/ | ||
const ABSOLUTE_BYTE_THRESHOLD = 500; | ||
/** | ||
* Percentage deviation from the expected value that is allowed. If the | ||
* size deviates by 1% of the expected value, the script will fail. | ||
*/ | ||
const PERCENTAGE_DEVIATION_THRESHOLD = 1; | ||
|
||
/** | ||
* Extracted command line arguments specified by the Bazel NodeJS binaries: | ||
* - `testId`: Unique id for the given test file that is used as key in the golden. | ||
* - `testFileRootpath`: Bazel rootpath that resolves to the test file that should be measured. | ||
* - `isApprove`: Whether the script runs in approve mode, and the golden should be updated | ||
* with the actual measured size. | ||
*/ | ||
const [testId, testFileRootpath, isApprove] = process.argv.slice(2); | ||
devversion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const testFilePath = require.resolve(`angular_material/${testFileRootpath}`); | ||
const goldenFilePath = require.resolve('../../goldens/size-test.json'); | ||
|
||
const golden = JSON.parse(readFileSync(goldenFilePath, 'utf8')); | ||
const fileStat = statSync(testFilePath); | ||
const actualSize = fileStat.size; | ||
|
||
// If in approve mode, update the golden to reflect the new size. | ||
if (isApprove) { | ||
golden[testId] = actualSize; | ||
writeFileSync(goldenFilePath, JSON.stringify(golden, null, 2)); | ||
console.info(chalk.green(`Approved golden size for "${testId}"`)); | ||
process.exit(0); | ||
} | ||
|
||
// If no size has been collected for the test id, report an error. | ||
if (golden[testId] === undefined) { | ||
console.error(`No golden size for "${testId}". Please create a new entry.`); | ||
printApproveCommand(); | ||
process.exit(1); | ||
} | ||
|
||
const expectedSize = Number(golden[testId]); | ||
const absoluteSizeDiff = Math.abs(actualSize - expectedSize); | ||
const deviatedByPercentage = | ||
absoluteSizeDiff > (expectedSize * PERCENTAGE_DEVIATION_THRESHOLD / 100); | ||
const deviatedByAbsoluteDiff = absoluteSizeDiff > ABSOLUTE_BYTE_THRESHOLD; | ||
|
||
// Always print the expected and actual size so that it's easier to find culprit | ||
// commits when the size slowly moves toward the threshold boundaries. | ||
console.info(chalk.yellow(`Expected: ${expectedSize}, but got: ${actualSize}`)); | ||
|
||
if (deviatedByPercentage) { | ||
console.error(`Actual size deviates by more than 1% of the expected size. `); | ||
printApproveCommand(); | ||
process.exit(1); | ||
} else if (deviatedByAbsoluteDiff) { | ||
console.error(`Actual size deviates by more than 500 bytes from the expected.`); | ||
printApproveCommand(); | ||
process.exit(1); | ||
} | ||
|
||
/** Prints the command for approving the current test. */ | ||
function printApproveCommand() { | ||
console.info(chalk.yellow('You can approve the golden by running the following command:')); | ||
console.info(chalk.yellow(` bazel run ${process.env.BAZEL_TARGET}.approve`)); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary", "nodejs_test") | ||
load("@npm_bazel_rollup//:index.bzl", "rollup_bundle") | ||
load("@npm_bazel_terser//:index.bzl", "terser_minified") | ||
load("//tools:defaults.bzl", "ng_module") | ||
|
||
""" | ||
Performs size measurements for the specified file. The file will be built as part | ||
of a `ng_module` and then will be optimized with build-optimizer, rollup and Terser. | ||
|
||
The resulting size will be validated against a golden file to ensure that we don't | ||
regress in payload size, or that we can improvements to payload size. | ||
""" | ||
|
||
def size_test(name, file, deps): | ||
# Generates an unique id for the given size test. e.g. if the macro is called | ||
# within the `integration/size-test/material` package with `name = list`, then | ||
# the unique id will be set to `material/list`. | ||
test_id = "%s/%s" % (native.package_name()[len("integration/size-test/"):], name) | ||
|
||
ng_module( | ||
name = "%s_lib" % name, | ||
srcs = ["%s.ts" % name], | ||
testonly = True, | ||
deps = [ | ||
"@npm//@angular/core", | ||
"@npm//@angular/platform-browser-dynamic", | ||
] + deps, | ||
) | ||
|
||
rollup_bundle( | ||
name = "%s_bundle" % name, | ||
config_file = "//integration/size-test:rollup.config.js", | ||
testonly = True, | ||
entry_points = { | ||
(file): "%s_bundled" % name, | ||
}, | ||
deps = [ | ||
":%s_lib" % name, | ||
"@npm//rollup-plugin-node-resolve", | ||
"@npm//@angular-devkit/build-optimizer", | ||
], | ||
sourcemap = "false", | ||
) | ||
|
||
terser_minified( | ||
testonly = True, | ||
name = "%s_bundle_min" % name, | ||
src = ":%s_bundle" % name, | ||
config_file = "//integration/size-test:terser-config.json", | ||
sourcemap = False, | ||
) | ||
|
||
nodejs_test( | ||
name = name, | ||
data = [ | ||
"//goldens:size-test.json", | ||
"//integration/size-test:check-size", | ||
":%s_bundle_min" % name, | ||
], | ||
entry_point = "//integration/size-test:check-size.ts", | ||
args = [test_id, "$(rootpath :%s_bundle_min)" % name], | ||
) | ||
|
||
nodejs_binary( | ||
name = "%s.approve" % name, | ||
testonly = True, | ||
data = [ | ||
"//goldens:size-test.json", | ||
"//integration/size-test:check-size", | ||
":%s_bundle_min" % name, | ||
], | ||
entry_point = "//integration/size-test:check-size.ts", | ||
args = [test_id, "$(rootpath :%s_bundle_min)" % name, "true"], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
load("//integration/size-test:index.bzl", "size_test") | ||
|
||
size_test( | ||
name = "list", | ||
file = "list.ts", | ||
deps = ["//src/material/list"], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {Component, NgModule} from '@angular/core'; | ||
import {platformBrowser} from '@angular/platform-browser'; | ||
import {MatListModule} from '@angular/material/list'; | ||
|
||
/** | ||
* Basic component using `MatNavList` and `MatListItem`. Other parts of the list | ||
* module such as `MatList`, `MatSelectionList` or `MatListOption` are not used | ||
* and should be tree-shaken away. | ||
*/ | ||
@Component({ | ||
devversion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
template: ` | ||
<mat-nav-list> | ||
<mat-list-item> | ||
hello | ||
</mat-list-item> | ||
</mat-nav-list> | ||
`, | ||
}) | ||
export class TestComponent {} | ||
|
||
@NgModule({ | ||
imports: [MatListModule], | ||
declarations: [TestComponent], | ||
bootstrap: [TestComponent], | ||
}) | ||
export class AppModule {} | ||
|
||
platformBrowser().bootstrapModule(AppModule); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const {buildOptimizer} = require('@angular-devkit/build-optimizer'); | ||
const node = require('rollup-plugin-node-resolve'); | ||
|
||
const buildOptimizerPlugin = { | ||
name: 'build-optimizer', | ||
transform: (content, id) => { | ||
const {content: code, sourceMap: map} = buildOptimizer({ | ||
content, | ||
inputFilePath: id, | ||
emitSourceMap: true, | ||
isSideEffectFree: true, | ||
isAngularCoreFile: false, | ||
}); | ||
if (!code) { | ||
return null; | ||
} | ||
if (!map) { | ||
throw new Error('No sourcemap produced by build optimizer'); | ||
} | ||
return {code, map}; | ||
}, | ||
}; | ||
|
||
module.exports = { | ||
plugins: [ | ||
buildOptimizerPlugin, | ||
node({ | ||
mainFields: ['es2015_ivy_ngcc', 'module_ivy_ngcc','es2015', 'module'], | ||
}), | ||
], | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"output": { | ||
"ecma": "es2015", | ||
"comments": false | ||
}, | ||
"compress": { | ||
"global_defs": { | ||
"ngDevMode": false, | ||
"ngI18nClosureMode": false, | ||
"ngJitMode": false | ||
}, | ||
"passes": 3, | ||
"pure_getters": true | ||
}, | ||
"mangle": true | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"experimentalDecorators": true, | ||
"lib": ["es2015"], | ||
"types": ["node"] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.