Skip to content

Commit edaae77

Browse files
authored
fix: Fix bundle size tracking (#29486)
## **Description** The bundle size tracking was accidentally broken in #29408. This restores the bundle size tracking. [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/29486?quickstart=1) ## **Related issues** Fixes #29485 Resolves bug introduced by #29408 ## **Manual testing steps** 1. Check the "mv3: Bundle Size Stats" link in the `metamaskbot` comment 2. Once this is merged, check the bundle size tracker to ensure it's working again: https://github.com/MetaMask/extension_bundlesize_stats * Unfortunately I am not sure how to easily test this on the PR. The tracker is only updated when commits are made to `main`. ## **Screenshots/Recordings** N/A ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
1 parent 87c524e commit edaae77

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

.circleci/config.yml

+34
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ workflows:
277277
- user-actions-benchmark:
278278
requires:
279279
- prep-build-test
280+
- bundle-size:
281+
requires:
282+
- prep-build-test
280283
- job-publish-prerelease:
281284
requires:
282285
- prep-deps
@@ -294,6 +297,7 @@ workflows:
294297
- prep-build-ts-migration-dashboard
295298
- benchmark
296299
- user-actions-benchmark
300+
- bundle-size
297301
- all-tests-pass
298302
- job-publish-release:
299303
filters:
@@ -1270,6 +1274,36 @@ jobs:
12701274
paths:
12711275
- test-artifacts
12721276

1277+
bundle-size:
1278+
executor: node-browsers-small
1279+
steps:
1280+
- run: *shallow-git-clone-and-enable-vnc
1281+
- run: sudo corepack enable
1282+
- attach_workspace:
1283+
at: .
1284+
- run:
1285+
name: Move test build to dist
1286+
command: mv ./dist-test ./dist
1287+
- run:
1288+
name: Move test zips to builds
1289+
command: mv ./builds-test ./builds
1290+
- run:
1291+
name: Measure bundle size
1292+
command: yarn bundle-size --out test-artifacts/chrome
1293+
- run:
1294+
name: Install jq
1295+
command: sudo apt install jq -y
1296+
- run:
1297+
name: Record bundle size at commit
1298+
command: ./.circleci/scripts/bundle-stats-commit.sh
1299+
- store_artifacts:
1300+
path: test-artifacts
1301+
destination: test-artifacts
1302+
- persist_to_workspace:
1303+
root: .
1304+
paths:
1305+
- test-artifacts
1306+
12731307
job-publish-prerelease:
12741308
executor: node-browsers-medium
12751309
steps:

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"start:test:mv2:flask": "ENABLE_MV3=false yarn start:test:flask --apply-lavamoat=false --snow=false",
2929
"start:test:mv2": "ENABLE_MV3=false BLOCKAID_FILE_CDN=static.cx.metamask.io/api/v1/confirmations/ppom yarn start:test --apply-lavamoat=false --snow=false",
3030
"benchmark:chrome": "SELENIUM_BROWSER=chrome ts-node test/e2e/benchmark.js",
31+
"bundle-size": "SELENIUM_BROWSER=chrome ts-node test/e2e/mv3-perf-stats/bundle-size.js",
3132
"user-actions-benchmark:chrome": "SELENIUM_BROWSER=chrome ts-node test/e2e/user-actions-benchmark.js",
3233
"benchmark:firefox": "SELENIUM_BROWSER=firefox ts-node test/e2e/benchmark.js",
3334
"build:test": "yarn env:e2e build test",
+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#!/usr/bin/env node
2+
3+
/* eslint-disable node/shebang */
4+
const path = require('path');
5+
const { promises: fs } = require('fs');
6+
const yargs = require('yargs/yargs');
7+
const { hideBin } = require('yargs/helpers');
8+
const {
9+
isWritable,
10+
getFirstParentDirectoryThatExists,
11+
} = require('../../helpers/file');
12+
13+
const { exitWithError } = require('../../../development/lib/exit-with-error');
14+
15+
/**
16+
* The e2e test case is used to capture bundle time statistics for extension.
17+
*/
18+
19+
const backgroundFiles = [
20+
'scripts/runtime-lavamoat.js',
21+
'scripts/lockdown-more.js',
22+
'scripts/sentry-install.js',
23+
'scripts/policy-load.js',
24+
];
25+
26+
const uiFiles = [
27+
'scripts/sentry-install.js',
28+
'scripts/runtime-lavamoat.js',
29+
'scripts/lockdown-more.js',
30+
'scripts/policy-load.js',
31+
];
32+
33+
const BackgroundFileRegex = /background-[0-9]*.js/u;
34+
const CommonFileRegex = /common-[0-9]*.js/u;
35+
const UIFileRegex = /ui-[0-9]*.js/u;
36+
37+
async function main() {
38+
const { argv } = yargs(hideBin(process.argv)).usage(
39+
'$0 [options]',
40+
'Capture bundle size stats',
41+
(_yargs) =>
42+
_yargs.option('out', {
43+
description:
44+
'Output filename. Output printed to STDOUT of this is omitted.',
45+
type: 'string',
46+
normalize: true,
47+
}),
48+
);
49+
const { out } = argv;
50+
51+
const distFolder = 'dist/chrome';
52+
const backgroundFileList = [];
53+
const uiFileList = [];
54+
const commonFileList = [];
55+
56+
const files = await fs.readdir(distFolder);
57+
for (let i = 0; i < files.length; i++) {
58+
const file = files[i];
59+
if (CommonFileRegex.test(file)) {
60+
const stats = await fs.stat(`${distFolder}/${file}`);
61+
commonFileList.push({ name: file, size: stats.size });
62+
} else if (
63+
backgroundFiles.includes(file) ||
64+
BackgroundFileRegex.test(file)
65+
) {
66+
const stats = await fs.stat(`${distFolder}/${file}`);
67+
backgroundFileList.push({ name: file, size: stats.size });
68+
} else if (uiFiles.includes(file) || UIFileRegex.test(file)) {
69+
const stats = await fs.stat(`${distFolder}/${file}`);
70+
uiFileList.push({ name: file, size: stats.size });
71+
}
72+
}
73+
74+
const backgroundBundleSize = backgroundFileList.reduce(
75+
(result, file) => result + file.size,
76+
0,
77+
);
78+
79+
const uiBundleSize = uiFileList.reduce(
80+
(result, file) => result + file.size,
81+
0,
82+
);
83+
84+
const commonBundleSize = commonFileList.reduce(
85+
(result, file) => result + file.size,
86+
0,
87+
);
88+
89+
const result = {
90+
background: {
91+
name: 'background',
92+
size: backgroundBundleSize,
93+
fileList: backgroundFileList,
94+
},
95+
ui: {
96+
name: 'ui',
97+
size: uiBundleSize,
98+
fileList: uiFileList,
99+
},
100+
common: {
101+
name: 'common',
102+
size: commonBundleSize,
103+
fileList: commonFileList,
104+
},
105+
};
106+
107+
if (out) {
108+
const outPath = `${out}/bundle_size.json`;
109+
const outputDirectory = path.dirname(outPath);
110+
const existingParentDirectory = await getFirstParentDirectoryThatExists(
111+
outputDirectory,
112+
);
113+
if (!(await isWritable(existingParentDirectory))) {
114+
throw new Error('Specified output file directory is not writable');
115+
}
116+
if (outputDirectory !== existingParentDirectory) {
117+
await fs.mkdir(outputDirectory, { recursive: true });
118+
}
119+
await fs.writeFile(outPath, JSON.stringify(result, null, 2));
120+
await fs.writeFile(
121+
`${out}/bundle_size_stats.json`,
122+
JSON.stringify(
123+
{
124+
background: backgroundBundleSize,
125+
ui: uiBundleSize,
126+
common: commonBundleSize,
127+
timestamp: new Date().getTime(),
128+
},
129+
null,
130+
2,
131+
),
132+
);
133+
} else {
134+
console.log(JSON.stringify(result, null, 2));
135+
}
136+
}
137+
138+
main().catch((error) => {
139+
exitWithError(error);
140+
});

0 commit comments

Comments
 (0)