|
| 1 | +import { readFileSync } from 'node:fs' |
| 2 | +import process from 'node:process' |
| 3 | + |
| 4 | +interface RollupStatsNode { |
| 5 | + renderedLength?: number |
| 6 | + gzipLength?: number |
| 7 | + brotliLength?: number |
| 8 | +} |
| 9 | + |
| 10 | +interface RollupStats { |
| 11 | + nodeParts?: Record<string, RollupStatsNode> |
| 12 | +} |
| 13 | + |
| 14 | +interface BundleSize { |
| 15 | + rendered: number |
| 16 | + gzip: number |
| 17 | + brotli: number |
| 18 | +} |
| 19 | + |
| 20 | +interface PackageComparison { |
| 21 | + name: string |
| 22 | + base: BundleSize |
| 23 | + head: BundleSize |
| 24 | + diff: { |
| 25 | + rendered: number |
| 26 | + gzip: number |
| 27 | + brotli: number |
| 28 | + } |
| 29 | + error?: string |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Calculate total size from Rollup stats |
| 34 | + */ |
| 35 | +function calculateTotalSize(stats: RollupStats): BundleSize { |
| 36 | + if (!stats.nodeParts) { |
| 37 | + return { rendered: 0, gzip: 0, brotli: 0 } |
| 38 | + } |
| 39 | + |
| 40 | + let totalRendered = 0 |
| 41 | + let totalGzip = 0 |
| 42 | + let totalBrotli = 0 |
| 43 | + |
| 44 | + for (const node of Object.values(stats.nodeParts)) { |
| 45 | + totalRendered += node.renderedLength || 0 |
| 46 | + totalGzip += node.gzipLength || 0 |
| 47 | + totalBrotli += node.brotliLength || 0 |
| 48 | + } |
| 49 | + |
| 50 | + return { rendered: totalRendered, gzip: totalGzip, brotli: totalBrotli } |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Format bytes to KB with 2 decimal places |
| 55 | + */ |
| 56 | +function formatBytes(bytes: number): string { |
| 57 | + return (bytes / 1024).toFixed(2) |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Format diff with sign and percentage |
| 62 | + */ |
| 63 | +function formatDiff(diff: number, base: number): { icon: string, sign: string, percent: string } { |
| 64 | + const percent = base ? ((diff / base) * 100).toFixed(2) : '0.00' |
| 65 | + const sign = diff > 0 ? '+' : '' |
| 66 | + const icon = diff > 0 ? '📈' : diff < 0 ? '📉' : '➡️' |
| 67 | + return { icon, sign, percent } |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Compare sizes for a single package |
| 72 | + */ |
| 73 | +function comparePackage(name: string, headPath: string, basePath: string): PackageComparison { |
| 74 | + try { |
| 75 | + const headStats: RollupStats = JSON.parse(readFileSync(headPath, 'utf8')) |
| 76 | + const baseStats: RollupStats = JSON.parse(readFileSync(basePath, 'utf8')) |
| 77 | + |
| 78 | + const head = calculateTotalSize(headStats) |
| 79 | + const base = calculateTotalSize(baseStats) |
| 80 | + |
| 81 | + return { |
| 82 | + name, |
| 83 | + base, |
| 84 | + head, |
| 85 | + diff: { |
| 86 | + rendered: head.rendered - base.rendered, |
| 87 | + gzip: head.gzip - base.gzip, |
| 88 | + brotli: head.brotli - base.brotli, |
| 89 | + }, |
| 90 | + } |
| 91 | + } |
| 92 | + catch (error) { |
| 93 | + return { |
| 94 | + name, |
| 95 | + base: { rendered: 0, gzip: 0, brotli: 0 }, |
| 96 | + head: { rendered: 0, gzip: 0, brotli: 0 }, |
| 97 | + diff: { rendered: 0, gzip: 0, brotli: 0 }, |
| 98 | + error: error instanceof Error ? error.message : String(error), |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Generate markdown comment for size comparison |
| 105 | + */ |
| 106 | +export function generateSizeComment(packages: string[], statsDir = '.'): string { |
| 107 | + let commentBody = '## 📦 Bundle Size Comparison\n\n' |
| 108 | + |
| 109 | + for (const pkg of packages) { |
| 110 | + const headPath = `${statsDir}/head-stats/${pkg}/stats.json` |
| 111 | + const basePath = `${statsDir}/base-stats/${pkg}/stats.json` |
| 112 | + |
| 113 | + const comparison = comparePackage(pkg, headPath, basePath) |
| 114 | + |
| 115 | + if (comparison.error) { |
| 116 | + console.error(`Error processing ${pkg}:`, comparison.error) |
| 117 | + commentBody += `### ⚠️ **${pkg}**\n\nCould not compare sizes: ${comparison.error}\n\n` |
| 118 | + continue |
| 119 | + } |
| 120 | + |
| 121 | + const { icon, sign, percent } = formatDiff(comparison.diff.rendered, comparison.base.rendered) |
| 122 | + |
| 123 | + commentBody += `### ${icon} **${pkg}**\n\n` |
| 124 | + commentBody += `| Metric | Base | Head | Diff |\n` |
| 125 | + commentBody += `|--------|------|------|------|\n` |
| 126 | + commentBody += `| Rendered | ${formatBytes(comparison.base.rendered)} KB | ${formatBytes(comparison.head.rendered)} KB | ${sign}${formatBytes(comparison.diff.rendered)} KB (${sign}${percent}%) |\n` |
| 127 | + |
| 128 | + if (comparison.base.gzip > 0 || comparison.head.gzip > 0) { |
| 129 | + const gzipFmt = formatDiff(comparison.diff.gzip, comparison.base.gzip) |
| 130 | + commentBody += `| Gzip | ${formatBytes(comparison.base.gzip)} KB | ${formatBytes(comparison.head.gzip)} KB | ${gzipFmt.sign}${formatBytes(comparison.diff.gzip)} KB (${gzipFmt.sign}${gzipFmt.percent}%) |\n` |
| 131 | + } |
| 132 | + |
| 133 | + commentBody += '\n' |
| 134 | + } |
| 135 | + |
| 136 | + return commentBody |
| 137 | +} |
| 138 | + |
| 139 | +// CLI usage |
| 140 | +const isMainModule = process.argv[1] && ( |
| 141 | + import.meta.url === `file://${process.argv[1]}` |
| 142 | + || import.meta.url.endsWith(process.argv[1]) |
| 143 | +) |
| 144 | + |
| 145 | +if (isMainModule) { |
| 146 | + const packages = process.argv.slice(2) |
| 147 | + if (packages.length === 0) { |
| 148 | + console.error('Usage: node scripts/parse-sizes.ts <package1> <package2> ...') |
| 149 | + console.error('') |
| 150 | + console.error('Example: node scripts/parse-sizes.ts nuxi nuxt-cli create-nuxt') |
| 151 | + process.exit(1) |
| 152 | + } |
| 153 | + |
| 154 | + const comment = generateSizeComment(packages) |
| 155 | + console.log(comment) |
| 156 | +} |
0 commit comments