Skip to content
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

🏗 Report and store the bundle size of alternate runtime and extension files #25042

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 31 additions & 26 deletions build-system/tasks/bundle-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
const argv = require('minimist')(process.argv.slice(2));
const BBPromise = require('bluebird');
const brotliSize = require('brotli-size');
const colors = require('ansi-colors');
const deglob = require('globs-to-files');
const fs = require('fs');
const log = require('fancy-log');
const path = require('path');
Expand All @@ -38,31 +38,40 @@ const {
const {
VERSION: internalRuntimeVersion,
} = require('../compile/internal-version');
const {cyan, green, red, yellow} = require('ansi-colors');

const runtimeFile = './dist/v0.js';
const fileGlobs = ['dist/*.js', 'dist/v0/*-?.?.js'];
const normalizedRtvNumber = '1234567890123';

const expectedGitHubRepoSlug = 'ampproject/amphtml';
const bundleSizeAppBaseUrl = 'https://amp-bundle-size-bot.appspot.com/v0/';

const {red, cyan, yellow} = colors;

/**
* Get the brotli bundle size of the current build.
* Get the brotli bundle sizes of the current build.
*
* @return {string} the bundle size in KB rounded to 2 decimal points.
* @return {Map<string, number>} the bundle size in KB rounded to 2 decimal
* points.
*/
function getBrotliBundleSize() {
function getBrotliBundleSizes() {
// Brotli compressed size fluctuates because of changes in the RTV number, so
// normalize this across pull requests by replacing that RTV with a constant.
const normalizedFileContents = fs
.readFileSync(runtimeFile, 'utf8')
.replace(new RegExp(internalRuntimeVersion, 'g'), normalizedRtvNumber);
const bundleSize = parseFloat(
(brotliSize.sync(normalizedFileContents) / 1024).toFixed(2)
);
log('Bundle size', cyan('(brotli)'), 'is', cyan(`${bundleSize}KB`));
return bundleSize;
const bundleSizes = {};

log(cyan('brotli'), 'bundle sizes are:');
for (const filePath of deglob.sync(fileGlobs)) {
const normalizedFileContents = fs
.readFileSync(filePath, 'utf8')
.replace(new RegExp(internalRuntimeVersion, 'g'), normalizedRtvNumber);

const relativeFilePath = path.relative('.', filePath);
const bundleSize = parseFloat(
(brotliSize.sync(normalizedFileContents) / 1024).toFixed(2)
);
log(' ', cyan(relativeFilePath) + ':', green(`${bundleSize}KB`));
bundleSizes[relativeFilePath] = bundleSize;
}

return bundleSizes;
}

/**
Expand Down Expand Up @@ -100,10 +109,7 @@ async function storeBundleSize() {
json: true,
body: {
token: process.env.BUNDLE_SIZE_TOKEN,
// TODO(#20843, danielrozenberg): add extensions and other runtimes.
bundleSizes: {
danielrozenberg marked this conversation as resolved.
Show resolved Hide resolved
'dist/v0.js': getBrotliBundleSize(),
},
bundleSizes: getBrotliBundleSizes(),
},
});
if (response.statusCode < 200 || response.statusCode >= 300) {
Expand Down Expand Up @@ -159,19 +165,17 @@ async function skipBundleSize() {
async function reportBundleSize() {
if (isTravisPullRequestBuild()) {
const baseSha = gitTravisMasterBaseline();
const brotliBundleSize = getBrotliBundleSize();
const commitHash = gitCommitHash();
try {
const response = await requestPost({
uri: url.resolve(
bundleSizeAppBaseUrl,
path.join('commit', commitHash, 'report')
path.join('commit', commitHash, 'report.json')
),
json: true,
body: {
baseSha,
bundleSize: brotliBundleSize,
brotliBundleSize,
bundleSizes: getBrotliBundleSizes(),
},
});
if (response.statusCode < 200 || response.statusCode >= 300) {
Expand All @@ -196,9 +200,10 @@ async function reportBundleSize() {
}

function getLocalBundleSize() {
if (!fs.existsSync(runtimeFile)) {
log('Could not find', cyan(runtimeFile) + '.');
if (deglob.sync(fileGlobs).length === 0) {
log('Could not find runtime files.');
log('Run', cyan('gulp dist --noextensions'), 'and re-run this task.');
process.exitCode = 1;
return;
} else {
log(
Expand All @@ -208,7 +213,7 @@ function getLocalBundleSize() {
cyan(shortSha(gitCommitHash())) + '.'
);
}
getBrotliBundleSize();
getBrotliBundleSizes();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the point of this call? Should this function be returning something? JSDoc would help, but also not clear if there's any point to this function call

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getBrotliBundleSizes prints its own logs. @rsimha added the getLocalBundleSize() method originally, I'm just updating here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the point of this is just to log stuff?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's a debugging thing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

async function bundleSize() {
Expand Down