|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import chalk from 'chalk'; |
| 10 | +import { existsSync, statSync, readdirSync } from 'fs'; |
| 11 | +import { resolve, join, extname } from 'path'; |
| 12 | + |
| 13 | +interface AnalyzeOptions { |
| 14 | + bundleSize?: boolean; |
| 15 | + renderPerformance?: boolean; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Analyze bundle size by scanning dist directory |
| 20 | + */ |
| 21 | +async function analyzeBundleSize() { |
| 22 | + console.log(chalk.bold('\n📦 Bundle Size Analysis\n')); |
| 23 | + |
| 24 | + const distDir = resolve(process.cwd(), 'dist'); |
| 25 | + |
| 26 | + if (!existsSync(distDir)) { |
| 27 | + console.log(chalk.yellow('⚠ No dist directory found. Run build first.')); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + const files: Array<{ path: string; size: number }> = []; |
| 32 | + |
| 33 | + function scanDirectory(dir: string) { |
| 34 | + const items = readdirSync(dir); |
| 35 | + |
| 36 | + for (const item of items) { |
| 37 | + const fullPath = join(dir, item); |
| 38 | + const stat = statSync(fullPath); |
| 39 | + |
| 40 | + if (stat.isDirectory()) { |
| 41 | + scanDirectory(fullPath); |
| 42 | + } else { |
| 43 | + files.push({ |
| 44 | + path: fullPath.replace(distDir + '/', ''), |
| 45 | + size: stat.size, |
| 46 | + }); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + scanDirectory(distDir); |
| 52 | + |
| 53 | + // Sort by size (largest first) |
| 54 | + files.sort((a, b) => b.size - a.size); |
| 55 | + |
| 56 | + // Calculate totals |
| 57 | + const totalSize = files.reduce((sum, file) => sum + file.size, 0); |
| 58 | + const jsFiles = files.filter(f => extname(f.path) === '.js'); |
| 59 | + const cssFiles = files.filter(f => extname(f.path) === '.css'); |
| 60 | + |
| 61 | + const jsSize = jsFiles.reduce((sum, file) => sum + file.size, 0); |
| 62 | + const cssSize = cssFiles.reduce((sum, file) => sum + file.size, 0); |
| 63 | + |
| 64 | + console.log(chalk.bold('Summary:')); |
| 65 | + console.log(chalk.gray(' Total Size:'), formatBytes(totalSize)); |
| 66 | + console.log(chalk.gray(' JavaScript:'), formatBytes(jsSize), chalk.dim(`(${jsFiles.length} files)`)); |
| 67 | + console.log(chalk.gray(' CSS: '), formatBytes(cssSize), chalk.dim(`(${cssFiles.length} files)`)); |
| 68 | + console.log(chalk.gray(' Other: '), formatBytes(totalSize - jsSize - cssSize)); |
| 69 | + |
| 70 | + console.log(chalk.bold('\nLargest Files:')); |
| 71 | + files.slice(0, 10).forEach((file) => { |
| 72 | + const sizeStr = formatBytes(file.size).padStart(10); |
| 73 | + console.log(chalk.gray(` ${sizeStr}`), file.path); |
| 74 | + }); |
| 75 | + |
| 76 | + // Bundle size recommendations |
| 77 | + console.log(chalk.bold('\n💡 Recommendations:')); |
| 78 | + |
| 79 | + if (totalSize > 1024 * 1024) { |
| 80 | + console.log(chalk.yellow(' ⚠ Total bundle size is large (> 1MB)')); |
| 81 | + console.log(chalk.gray(' Consider code splitting or lazy loading')); |
| 82 | + } |
| 83 | + |
| 84 | + if (jsSize > 500 * 1024) { |
| 85 | + console.log(chalk.yellow(' ⚠ JavaScript bundle is large (> 500KB)')); |
| 86 | + console.log(chalk.gray(' Consider:')); |
| 87 | + console.log(chalk.gray(' - Tree shaking unused code')); |
| 88 | + console.log(chalk.gray(' - Lazy loading components')); |
| 89 | + console.log(chalk.gray(' - Using dynamic imports')); |
| 90 | + } |
| 91 | + |
| 92 | + if (files.length > 100) { |
| 93 | + console.log(chalk.yellow(` ⚠ Large number of files (${files.length})`)); |
| 94 | + console.log(chalk.gray(' Consider bundling or combining files')); |
| 95 | + } |
| 96 | + |
| 97 | + console.log(''); |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Analyze render performance (placeholder for now) |
| 102 | + */ |
| 103 | +async function analyzeRenderPerformance() { |
| 104 | + console.log(chalk.bold('\n⚡ Render Performance Analysis\n')); |
| 105 | + |
| 106 | + console.log(chalk.gray('Performance analysis features:')); |
| 107 | + console.log(chalk.gray(' ✓ Expression caching enabled')); |
| 108 | + console.log(chalk.gray(' ✓ Component memoization available')); |
| 109 | + console.log(chalk.gray(' ✓ Virtual scrolling support for large lists')); |
| 110 | + |
| 111 | + console.log(chalk.bold('\n💡 Performance Tips:')); |
| 112 | + console.log(chalk.gray(' • Use virtual scrolling for lists > 100 items')); |
| 113 | + console.log(chalk.gray(' • Cache frequently evaluated expressions')); |
| 114 | + console.log(chalk.gray(' • Use React.memo for expensive components')); |
| 115 | + console.log(chalk.gray(' • Implement pagination for large datasets')); |
| 116 | + console.log(chalk.gray(' • Use code splitting for large apps')); |
| 117 | + |
| 118 | + console.log(''); |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Format bytes to human-readable string |
| 123 | + */ |
| 124 | +function formatBytes(bytes: number): string { |
| 125 | + if (bytes === 0) return '0 B'; |
| 126 | + |
| 127 | + const k = 1024; |
| 128 | + const sizes = ['B', 'KB', 'MB', 'GB']; |
| 129 | + const i = Math.floor(Math.log(bytes) / Math.log(k)); |
| 130 | + |
| 131 | + return `${(bytes / Math.pow(k, i)).toFixed(2)} ${sizes[i]}`; |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Analyze application performance |
| 136 | + * |
| 137 | + * @param options - Analysis options |
| 138 | + */ |
| 139 | +export async function analyze(options: AnalyzeOptions = {}) { |
| 140 | + console.log(chalk.blue('🔍 ObjectUI Performance Analyzer\n')); |
| 141 | + |
| 142 | + const runAll = !options.bundleSize && !options.renderPerformance; |
| 143 | + |
| 144 | + if (options.bundleSize || runAll) { |
| 145 | + await analyzeBundleSize(); |
| 146 | + } |
| 147 | + |
| 148 | + if (options.renderPerformance || runAll) { |
| 149 | + await analyzeRenderPerformance(); |
| 150 | + } |
| 151 | + |
| 152 | + console.log(chalk.green('✓ Analysis complete!\n')); |
| 153 | +} |
0 commit comments