-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
3 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,140 @@ | ||
#!/usr/bin/env node | ||
|
||
/* eslint-disable node/shebang */ | ||
const path = require('path'); | ||
const { promises: fs } = require('fs'); | ||
const yargs = require('yargs/yargs'); | ||
const { hideBin } = require('yargs/helpers'); | ||
const { | ||
isWritable, | ||
getFirstParentDirectoryThatExists, | ||
} = require('../../helpers/file'); | ||
|
||
const { exitWithError } = require('../../../development/lib/exit-with-error'); | ||
|
||
/** | ||
* The e2e test case is used to capture bundle time statistics for extension. | ||
*/ | ||
|
||
const backgroundFiles = [ | ||
'scripts/runtime-lavamoat.js', | ||
'scripts/lockdown-more.js', | ||
'scripts/sentry-install.js', | ||
'scripts/policy-load.js', | ||
]; | ||
|
||
const uiFiles = [ | ||
'scripts/sentry-install.js', | ||
'scripts/runtime-lavamoat.js', | ||
'scripts/lockdown-more.js', | ||
'scripts/policy-load.js', | ||
]; | ||
|
||
const BackgroundFileRegex = /background-[0-9]*.js/u; | ||
const CommonFileRegex = /common-[0-9]*.js/u; | ||
const UIFileRegex = /ui-[0-9]*.js/u; | ||
|
||
async function main() { | ||
const { argv } = yargs(hideBin(process.argv)).usage( | ||
'$0 [options]', | ||
'Capture bundle size stats', | ||
(_yargs) => | ||
_yargs.option('out', { | ||
description: | ||
'Output filename. Output printed to STDOUT of this is omitted.', | ||
type: 'string', | ||
normalize: true, | ||
}), | ||
); | ||
const { out } = argv; | ||
|
||
const distFolder = 'dist/chrome'; | ||
const backgroundFileList = []; | ||
const uiFileList = []; | ||
const commonFileList = []; | ||
|
||
const files = await fs.readdir(distFolder); | ||
for (let i = 0; i < files.length; i++) { | ||
const file = files[i]; | ||
if (CommonFileRegex.test(file)) { | ||
const stats = await fs.stat(`${distFolder}/${file}`); | ||
commonFileList.push({ name: file, size: stats.size }); | ||
} else if ( | ||
backgroundFiles.includes(file) || | ||
BackgroundFileRegex.test(file) | ||
) { | ||
const stats = await fs.stat(`${distFolder}/${file}`); | ||
backgroundFileList.push({ name: file, size: stats.size }); | ||
} else if (uiFiles.includes(file) || UIFileRegex.test(file)) { | ||
const stats = await fs.stat(`${distFolder}/${file}`); | ||
uiFileList.push({ name: file, size: stats.size }); | ||
} | ||
} | ||
|
||
const backgroundBundleSize = backgroundFileList.reduce( | ||
(result, file) => result + file.size, | ||
0, | ||
); | ||
|
||
const uiBundleSize = uiFileList.reduce( | ||
(result, file) => result + file.size, | ||
0, | ||
); | ||
|
||
const commonBundleSize = commonFileList.reduce( | ||
(result, file) => result + file.size, | ||
0, | ||
); | ||
|
||
const result = { | ||
background: { | ||
name: 'background', | ||
size: backgroundBundleSize, | ||
fileList: backgroundFileList, | ||
}, | ||
ui: { | ||
name: 'ui', | ||
size: uiBundleSize, | ||
fileList: uiFileList, | ||
}, | ||
common: { | ||
name: 'common', | ||
size: commonBundleSize, | ||
fileList: commonFileList, | ||
}, | ||
}; | ||
|
||
if (out) { | ||
const outPath = `${out}/bundle_size.json`; | ||
const outputDirectory = path.dirname(outPath); | ||
const existingParentDirectory = await getFirstParentDirectoryThatExists( | ||
outputDirectory, | ||
); | ||
if (!(await isWritable(existingParentDirectory))) { | ||
throw new Error('Specified output file directory is not writable'); | ||
} | ||
if (outputDirectory !== existingParentDirectory) { | ||
await fs.mkdir(outputDirectory, { recursive: true }); | ||
} | ||
await fs.writeFile(outPath, JSON.stringify(result, null, 2)); | ||
await fs.writeFile( | ||
`${out}/bundle_size_stats.json`, | ||
JSON.stringify( | ||
{ | ||
background: backgroundBundleSize, | ||
ui: uiBundleSize, | ||
common: commonBundleSize, | ||
timestamp: new Date().getTime(), | ||
}, | ||
null, | ||
2, | ||
), | ||
); | ||
} else { | ||
console.log(JSON.stringify(result, null, 2)); | ||
} | ||
} | ||
|
||
main().catch((error) => { | ||
exitWithError(error); | ||
}); |