|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Bundle Size Monitoring Script |
| 5 | + * Checks JS/CSS bundle sizes and alerts on significant increases |
| 6 | + */ |
| 7 | + |
| 8 | +const fs = require('fs'); |
| 9 | +const path = require('path'); |
| 10 | + |
| 11 | +// Configuration |
| 12 | +const CONFIG = { |
| 13 | + distDir: 'dist', |
| 14 | + maxSizes: { |
| 15 | + js: 500 * 1024, // 500KB per JS chunk |
| 16 | + css: 100 * 1024, // 100KB per CSS file |
| 17 | + total: 2 * 1024 * 1024 // 2MB total |
| 18 | + }, |
| 19 | + // Optional: store previous sizes for comparison |
| 20 | + sizeHistoryFile: '.bundle-size-history.json' |
| 21 | +}; |
| 22 | + |
| 23 | +// Get file size in human readable format |
| 24 | +function formatSize(bytes) { |
| 25 | + if (bytes < 1024) return bytes + ' B'; |
| 26 | + if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(2) + ' KB'; |
| 27 | + return (bytes / (1024 * 1024)).toFixed(2) + ' MB'; |
| 28 | +} |
| 29 | + |
| 30 | +// Get all files of a specific type |
| 31 | +function getFiles(dir, ext, results = []) { |
| 32 | + try { |
| 33 | + const files = fs.readdirSync(dir); |
| 34 | + for (const file of files) { |
| 35 | + const fullPath = path.join(dir, file); |
| 36 | + const stat = fs.statSync(fullPath); |
| 37 | + if (stat.isDirectory()) { |
| 38 | + getFiles(fullPath, ext, results); |
| 39 | + } else if (file.endsWith(ext)) { |
| 40 | + results.push(fullPath); |
| 41 | + } |
| 42 | + } |
| 43 | + } catch (e) { |
| 44 | + console.error(`Error reading directory ${dir}:`, e.message); |
| 45 | + } |
| 46 | + return results; |
| 47 | +} |
| 48 | + |
| 49 | +// Main check function |
| 50 | +function checkBundleSizes() { |
| 51 | + console.log('🔍 Checking bundle sizes...\n'); |
| 52 | + |
| 53 | + if (!fs.existsSync(CONFIG.distDir)) { |
| 54 | + console.error('❌ Error: dist/ directory not found. Run "pnpm build" first.'); |
| 55 | + process.exit(1); |
| 56 | + } |
| 57 | + |
| 58 | + // Get all JS and CSS files |
| 59 | + const jsFiles = getFiles(CONFIG.distDir, '.js'); |
| 60 | + const cssFiles = getFiles(CONFIG.distDir, '.css'); |
| 61 | + |
| 62 | + let hasWarnings = false; |
| 63 | + let totalSize = 0; |
| 64 | + |
| 65 | + // Check JS files |
| 66 | + console.log('📜 JavaScript files:'); |
| 67 | + for (const file of jsFiles) { |
| 68 | + const stats = fs.statSync(file); |
| 69 | + const size = stats.size; |
| 70 | + totalSize += size; |
| 71 | + const relativePath = path.relative(CONFIG.distDir, file); |
| 72 | + const sizeStr = formatSize(size); |
| 73 | + |
| 74 | + if (size > CONFIG.maxSizes.js) { |
| 75 | + console.log(` ❌ ${relativePath}: ${sizeStr} (exceeds ${formatSize(CONFIG.maxSizes.js)})`); |
| 76 | + hasWarnings = true; |
| 77 | + } else { |
| 78 | + console.log(` ✅ ${relativePath}: ${sizeStr}`); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Check CSS files |
| 83 | + console.log('\n🎨 CSS files:'); |
| 84 | + for (const file of cssFiles) { |
| 85 | + const stats = fs.statSync(file); |
| 86 | + const size = stats.size; |
| 87 | + totalSize += size; |
| 88 | + const relativePath = path.relative(CONFIG.distDir, file); |
| 89 | + const sizeStr = formatSize(size); |
| 90 | + |
| 91 | + if (size > CONFIG.maxSizes.css) { |
| 92 | + console.log(` ❌ ${relativePath}: ${sizeStr} (exceeds ${formatSize(CONFIG.maxSizes.css)})`); |
| 93 | + hasWarnings = true; |
| 94 | + } else { |
| 95 | + console.log(` ✅ ${relativePath}: ${sizeStr}`); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Check total size |
| 100 | + console.log(`\n📊 Total bundle size: ${formatSize(totalSize)}`); |
| 101 | + if (totalSize > CONFIG.maxSizes.total) { |
| 102 | + console.log(`❌ Total size exceeds limit of ${formatSize(CONFIG.maxSizes.total)}`); |
| 103 | + hasWarnings = true; |
| 104 | + } |
| 105 | + |
| 106 | + // Summary |
| 107 | + console.log('\n' + '='.repeat(50)); |
| 108 | + if (hasWarnings) { |
| 109 | + console.log('⚠️ Bundle size warnings detected!'); |
| 110 | + console.log(' Consider code splitting or lazy loading.'); |
| 111 | + } else { |
| 112 | + console.log('✅ All bundle sizes are within limits!'); |
| 113 | + } |
| 114 | + |
| 115 | + return !hasWarnings; |
| 116 | +} |
| 117 | + |
| 118 | +// Run the check |
| 119 | +const success = checkBundleSizes(); |
| 120 | +process.exit(success ? 0 : 1); |
0 commit comments