|
| 1 | +const { resolve } = require('path'); |
| 2 | +const fs = require('fs'); |
| 3 | +const { execSync } = require('child_process'); |
| 4 | + |
| 5 | +const repoRoot = resolve(__dirname, '..'); |
| 6 | + |
| 7 | +const commitHash = process.env.GITHUB_SHA || execSync('git rev-parse HEAD').toString(); |
| 8 | +const runId = process.env.GITHUB_RUN_ID || 'local-run-id'; |
| 9 | + |
| 10 | +// CDN scripts |
| 11 | +function generateReportForCDNScripts() { |
| 12 | + const reports = []; |
| 13 | + const firebaseRoot = resolve(__dirname, '../packages/firebase'); |
| 14 | + const pkgJson = require(`${firebaseRoot}/package.json`); |
| 15 | + |
| 16 | + const special_files = [ |
| 17 | + 'firebase-performance-standalone.es2017.js', |
| 18 | + 'firebase-performance-standalone.js', |
| 19 | + 'firebase.js' |
| 20 | + ]; |
| 21 | + |
| 22 | + const files = [ |
| 23 | + ...special_files.map(file => `${firebaseRoot}/${file}`), |
| 24 | + ...pkgJson.components.map(component => `${firebaseRoot}/firebase-${component}.js`) |
| 25 | + ]; |
| 26 | + |
| 27 | + for (const file of files) { |
| 28 | + const { size } = fs.statSync(file); |
| 29 | + const fileName = file.split('/').slice(-1)[0] |
| 30 | + reports.push(makeReportObject('firebase', fileName, size)) |
| 31 | + } |
| 32 | + |
| 33 | + return reports; |
| 34 | +} |
| 35 | + |
| 36 | +// @firebase/* |
| 37 | +function generateReportForNPMPacakges() { |
| 38 | + const reports = []; |
| 39 | + const fields = [ |
| 40 | + 'main', |
| 41 | + 'module', |
| 42 | + 'esm2017', |
| 43 | + 'browser', |
| 44 | + 'react-native', |
| 45 | + 'lite', |
| 46 | + 'lite-esm2017' |
| 47 | + ]; |
| 48 | + |
| 49 | + const packageInfo = JSON.parse( |
| 50 | + execSync('npx lerna ls --json --scope @firebase/*', { cwd: repoRoot }).toString() |
| 51 | + ); |
| 52 | + |
| 53 | + for (const package of packageInfo) { |
| 54 | + const packageJson = require(`${package.location}/package.json`); |
| 55 | + |
| 56 | + for (const field of fields) { |
| 57 | + if (packageJson[field]) { |
| 58 | + const filePath = `${package.location}/${packageJson[field]}`; |
| 59 | + const { size } = fs.statSync(filePath); |
| 60 | + reports.push(makeReportObject(packageJson.name, field, size)); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return reports; |
| 66 | +} |
| 67 | + |
| 68 | +function makeReportObject(sdk, type, value) { |
| 69 | + return { |
| 70 | + sdk, |
| 71 | + type, |
| 72 | + value |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +function generateSizeReport() { |
| 77 | + const reports = [ |
| 78 | + ...generateReportForCDNScripts(), |
| 79 | + ...generateReportForNPMPacakges() |
| 80 | + ]; |
| 81 | + |
| 82 | + for (const r of reports) { |
| 83 | + console.log(r.sdk, r.type, r.value); |
| 84 | + } |
| 85 | + |
| 86 | + return { |
| 87 | + log: "https://www.goog.com", |
| 88 | + metric: "BinarySize", |
| 89 | + results: reports |
| 90 | + }; |
| 91 | +} |
| 92 | + |
| 93 | +generateSizeReport(); |
0 commit comments