|
| 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