-
Notifications
You must be signed in to change notification settings - Fork 38
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
Sort bundle-size
summary lists
#1568
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -49,6 +49,66 @@ exports.getCheckFromDatabase = async (db, headSha) => { | |||||||||||||
* @param {number} delta the bundle size delta in KB. | ||||||||||||||
* @return {string} formatted bundle size delta. | ||||||||||||||
*/ | ||||||||||||||
exports.formatBundleSizeDelta = delta => { | ||||||||||||||
const formatBundleSizeDelta = delta => { | ||||||||||||||
return 'Δ ' + (delta >= 0 ? '+' : '') + delta.toFixed(2) + 'KB'; | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* @param {string} file | ||||||||||||||
* @param {string} description | ||||||||||||||
* @return {string} | ||||||||||||||
*/ | ||||||||||||||
const formatFileItem = (file, description) => `* \`${file}\`: ${description}`; | ||||||||||||||
|
||||||||||||||
exports.formatFileItem = formatFileItem; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* @param {{file: string, bundleSizeDelta: number}} item | ||||||||||||||
* @return {string} | ||||||||||||||
*/ | ||||||||||||||
exports.formatBundleSizeItem = ({file, bundleSizeDelta}) => { | ||||||||||||||
return formatFileItem(file, formatBundleSizeDelta(bundleSizeDelta)); | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* @param {string} file | ||||||||||||||
* @return {string} | ||||||||||||||
*/ | ||||||||||||||
const noExtension = file => { | ||||||||||||||
const parts = file.split('.'); | ||||||||||||||
parts.pop(); | ||||||||||||||
return parts.join('.'); | ||||||||||||||
}; | ||||||||||||||
Comment on lines
+77
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is basically "the opposite of
Suggested change
(Add Tested this locally to make sure it works, e.g.:
|
||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* @param {{file: string, bundleSizeDelta: number}[]} items | ||||||||||||||
* @param {'asc'|'desc'} sizeOrder | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I doubt we'll ever really need both options, we can simplify the code by just assuming desc |
||||||||||||||
* @return {{file: string, bundleSizeDelta: number}[]} | ||||||||||||||
*/ | ||||||||||||||
function sortBundleSizeItems(items, sizeOrder = 'desc') { | ||||||||||||||
const bySize = (a, b) => { | ||||||||||||||
const factor = sizeOrder === 'desc' ? -1 : 1; | ||||||||||||||
return factor * (a.bundleSizeDelta - b.bundleSizeDelta); | ||||||||||||||
}; | ||||||||||||||
// group by filename without extension, so that '.mjs' is always next to its | ||||||||||||||
// equivalent '.js' | ||||||||||||||
const groups = {}; | ||||||||||||||
for (const item of items) { | ||||||||||||||
const name = noExtension(item.file); | ||||||||||||||
const group = (groups[name] = groups[name] || { | ||||||||||||||
items: [], | ||||||||||||||
bundleSizeDelta: item.bundleSizeDelta, | ||||||||||||||
}); | ||||||||||||||
group.items.push(item); | ||||||||||||||
group.bundleSizeDelta = | ||||||||||||||
sizeOrder === 'desc' | ||||||||||||||
? Math.max(group.bundleSizeDelta, item.bundleSizeDelta) | ||||||||||||||
: Math.min(group.bundleSizeDelta, item.bundleSizeDelta); | ||||||||||||||
} | ||||||||||||||
return Object.values(groups) | ||||||||||||||
.sort(bySize) | ||||||||||||||
.map(({items}) => items.sort(bySize)) | ||||||||||||||
.flat(); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
exports.sortBundleSizeItems = sortBundleSizeItems; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Copyright 2022 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
const {sortBundleSizeItems} = require('../common'); | ||
|
||
describe('bundle-size common', () => { | ||
describe('sortBundleSizeItems', () => { | ||
it('sorts desc', async () => { | ||
expect( | ||
sortBundleSizeItems( | ||
[ | ||
{file: 'bar.js', bundleSizeDelta: -0.1}, | ||
{file: 'foo.js', bundleSizeDelta: 0.1}, | ||
], | ||
'desc' | ||
) | ||
).toEqual([ | ||
{file: 'foo.js', bundleSizeDelta: 0.1}, | ||
{file: 'bar.js', bundleSizeDelta: -0.1}, | ||
]); | ||
}); | ||
it('sorts asc', async () => { | ||
expect( | ||
sortBundleSizeItems( | ||
[ | ||
{file: 'foo.js', bundleSizeDelta: 0.1}, | ||
{file: 'bar.js', bundleSizeDelta: -0.1}, | ||
], | ||
'asc' | ||
) | ||
).toEqual([ | ||
{file: 'bar.js', bundleSizeDelta: -0.1}, | ||
{file: 'foo.js', bundleSizeDelta: 0.1}, | ||
]); | ||
}); | ||
it('groups by filename without extension desc', async () => { | ||
expect( | ||
sortBundleSizeItems( | ||
[ | ||
{file: 'bar.js', bundleSizeDelta: -0.1}, | ||
{file: 'foo.js', bundleSizeDelta: 0.15}, | ||
{file: 'baz.js', bundleSizeDelta: 0.05}, | ||
{file: 'baz.mjs', bundleSizeDelta: 0.08}, | ||
{file: 'bar.mjs', bundleSizeDelta: 0.2}, | ||
{file: 'foo.mjs', bundleSizeDelta: 0.1}, | ||
], | ||
'desc' | ||
) | ||
).toEqual([ | ||
{file: 'bar.mjs', bundleSizeDelta: 0.2}, | ||
{file: 'bar.js', bundleSizeDelta: -0.1}, | ||
{file: 'foo.js', bundleSizeDelta: 0.15}, | ||
{file: 'foo.mjs', bundleSizeDelta: 0.1}, | ||
{file: 'baz.mjs', bundleSizeDelta: 0.08}, | ||
{file: 'baz.js', bundleSizeDelta: 0.05}, | ||
]); | ||
}); | ||
it('groups by filename without extension asc', async () => { | ||
expect( | ||
sortBundleSizeItems( | ||
[ | ||
{file: 'bar.js', bundleSizeDelta: -0.1}, | ||
{file: 'foo.js', bundleSizeDelta: 0.15}, | ||
{file: 'baz.js', bundleSizeDelta: 0.05}, | ||
{file: 'baz.mjs', bundleSizeDelta: 0.08}, | ||
{file: 'bar.mjs', bundleSizeDelta: 0.2}, | ||
{file: 'foo.mjs', bundleSizeDelta: 0.1}, | ||
], | ||
'asc' | ||
) | ||
).toEqual([ | ||
{file: 'bar.js', bundleSizeDelta: -0.1}, | ||
{file: 'bar.mjs', bundleSizeDelta: 0.2}, | ||
{file: 'baz.js', bundleSizeDelta: 0.05}, | ||
{file: 'baz.mjs', bundleSizeDelta: 0.08}, | ||
{file: 'foo.mjs', bundleSizeDelta: 0.1}, | ||
{file: 'foo.js', bundleSizeDelta: 0.15}, | ||
]); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eh, I think it's fine to sort both cases as desc - it creates a consistent reading experience 🤷♂️