|
| 1 | +# API Documentation |
| 2 | + |
| 3 | +## Core API |
| 4 | + |
| 5 | +### BundleSizeTrackerPlugin |
| 6 | + |
| 7 | +#### Constructor Options |
| 8 | + |
| 9 | +```typescript |
| 10 | +interface BundleSizeTrackerOptions { |
| 11 | + // Core Options |
| 12 | + maxSize?: number; // Maximum bundle size in KB |
| 13 | + outputFormat?: OutputFormat[]; // 'json' | 'html' | 'console' |
| 14 | + outputPath?: string; // Output directory for reports |
| 15 | + |
| 16 | + // Rules Configuration |
| 17 | + rules?: { |
| 18 | + pattern: string | RegExp; // Bundle name pattern |
| 19 | + maxSize: number; // Size limit in KB |
| 20 | + }[]; |
| 21 | + |
| 22 | + // Compression Options |
| 23 | + compression?: { |
| 24 | + gzip?: boolean; // Enable Gzip analysis |
| 25 | + brotli?: boolean; // Enable Brotli analysis |
| 26 | + raw?: boolean; // Show raw size |
| 27 | + }; |
| 28 | + |
| 29 | + // AI Configuration |
| 30 | + ai?: { |
| 31 | + enabled?: boolean; // Enable AI analysis |
| 32 | + model?: string; // AI model to use |
| 33 | + temperature?: number; // Model temperature |
| 34 | + apiKey?: string; // OpenAI API key |
| 35 | + }; |
| 36 | + |
| 37 | + // History Configuration |
| 38 | + history?: { |
| 39 | + enabled?: boolean; // Enable history tracking |
| 40 | + maxEntries?: number; // Maximum history entries |
| 41 | + exportPath?: string; // History export path |
| 42 | + thresholds?: { |
| 43 | + totalSizeIncreaseThreshold?: number; |
| 44 | + chunkSizeIncreaseThreshold?: number; |
| 45 | + maxTotalSize?: number; |
| 46 | + maxChunkSize?: number; |
| 47 | + }; |
| 48 | + }; |
| 49 | + |
| 50 | + // RUM Configuration |
| 51 | + rum?: { |
| 52 | + enabled?: boolean; // Enable RUM |
| 53 | + sampleRate?: number; // Sampling rate (0-1) |
| 54 | + endpoint?: string; // Data collection endpoint |
| 55 | + excludePatterns?: string[]; // URL patterns to exclude |
| 56 | + metrics?: { // Metrics to collect |
| 57 | + loadTime?: boolean; |
| 58 | + fcp?: boolean; |
| 59 | + lcp?: boolean; |
| 60 | + fid?: boolean; |
| 61 | + cls?: boolean; |
| 62 | + tti?: boolean; |
| 63 | + tbt?: boolean; |
| 64 | + }; |
| 65 | + }; |
| 66 | + |
| 67 | + // CI Configuration |
| 68 | + ci?: { |
| 69 | + enabled?: boolean; // Enable CI mode |
| 70 | + failOnError?: boolean; // Fail CI on size limit exceed |
| 71 | + commentOnPR?: boolean; // Comment on GitHub PR |
| 72 | + annotations?: boolean; // Add GitHub annotations |
| 73 | + }; |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### Methods |
| 78 | + |
| 79 | +#### analyze() |
| 80 | +Analyzes bundle sizes and generates reports. |
| 81 | + |
| 82 | +```typescript |
| 83 | +async analyze(): Promise<AnalysisResult> { |
| 84 | + return { |
| 85 | + totalSize: number; |
| 86 | + bundles: Bundle[]; |
| 87 | + status: 'pass' | 'fail'; |
| 88 | + recommendations: string[]; |
| 89 | + }; |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +#### getHistory() |
| 94 | +Retrieves bundle size history. |
| 95 | + |
| 96 | +```typescript |
| 97 | +async getHistory(): Promise<HistoryEntry[]> { |
| 98 | + return [ |
| 99 | + { |
| 100 | + timestamp: string; |
| 101 | + totalSize: number; |
| 102 | + bundles: Bundle[]; |
| 103 | + changes: Changes; |
| 104 | + } |
| 105 | + ]; |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +#### getRUMData() |
| 110 | +Retrieves Real User Monitoring data. |
| 111 | + |
| 112 | +```typescript |
| 113 | +async getRUMData(): Promise<RUMData[]> { |
| 114 | + return [ |
| 115 | + { |
| 116 | + timestamp: string; |
| 117 | + metrics: RUMMetrics; |
| 118 | + deviceInfo: DeviceInfo; |
| 119 | + connection: ConnectionInfo; |
| 120 | + } |
| 121 | + ]; |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +## Types |
| 126 | + |
| 127 | +### Bundle |
| 128 | +```typescript |
| 129 | +interface Bundle { |
| 130 | + name: string; |
| 131 | + size: number; |
| 132 | + compressed: { |
| 133 | + gzip?: number; |
| 134 | + brotli?: number; |
| 135 | + }; |
| 136 | + modules: Module[]; |
| 137 | +} |
| 138 | +``` |
| 139 | +
|
| 140 | +### RUMMetrics |
| 141 | +```typescript |
| 142 | +interface RUMMetrics { |
| 143 | + loadTime: number; |
| 144 | + firstContentfulPaint: number; |
| 145 | + largestContentfulPaint: number; |
| 146 | + firstInputDelay: number; |
| 147 | + cumulativeLayoutShift: number; |
| 148 | + timeToInteractive: number; |
| 149 | + totalBlockingTime: number; |
| 150 | +} |
| 151 | +``` |
| 152 | +
|
| 153 | +### DeviceInfo |
| 154 | +```typescript |
| 155 | +interface DeviceInfo { |
| 156 | + deviceType: 'mobile' | 'tablet' | 'desktop'; |
| 157 | + hardwareConcurrency: number; |
| 158 | + deviceMemory?: number; |
| 159 | +} |
| 160 | +``` |
| 161 | +
|
| 162 | +### ConnectionInfo |
| 163 | +```typescript |
| 164 | +interface ConnectionInfo { |
| 165 | + effectiveType: string; |
| 166 | + rtt: number; |
| 167 | + downlink: number; |
| 168 | +} |
| 169 | +``` |
| 170 | +
|
| 171 | +## Events |
| 172 | +
|
| 173 | +### onAnalysisComplete |
| 174 | +Fired when bundle analysis is complete. |
| 175 | +
|
| 176 | +```typescript |
| 177 | +onAnalysisComplete(callback: (result: AnalysisResult) => void): void; |
| 178 | +``` |
| 179 | +
|
| 180 | +### onSizeExceeded |
| 181 | +Fired when bundle size exceeds limit. |
| 182 | +
|
| 183 | +```typescript |
| 184 | +onSizeExceeded(callback: (bundle: Bundle) => void): void; |
| 185 | +``` |
| 186 | +
|
| 187 | +### onRUMDataCollected |
| 188 | +Fired when RUM data is collected. |
| 189 | +
|
| 190 | +```typescript |
| 191 | +onRUMDataCollected(callback: (data: RUMData) => void): void; |
| 192 | +``` |
| 193 | +
|
| 194 | +## CLI Commands |
| 195 | +
|
| 196 | +### Check Bundle Size |
| 197 | +```bash |
| 198 | +npx bundle-size-tracker check |
| 199 | +``` |
| 200 | +
|
| 201 | +### Generate Report |
| 202 | +```bash |
| 203 | +npx bundle-size-tracker report --format html |
| 204 | +``` |
| 205 | +
|
| 206 | +### View History |
| 207 | +```bash |
| 208 | +npx bundle-size-tracker history --last 10 |
| 209 | +``` |
| 210 | +
|
| 211 | +### Export RUM Data |
| 212 | +```bash |
| 213 | +npx bundle-size-tracker rum-export --format json |
| 214 | +``` |
| 215 | +
|
| 216 | +## Error Handling |
| 217 | +
|
| 218 | +### Error Types |
| 219 | +```typescript |
| 220 | +enum BundleSizeError { |
| 221 | + SIZE_LIMIT_EXCEEDED = 'SIZE_LIMIT_EXCEEDED', |
| 222 | + ANALYSIS_FAILED = 'ANALYSIS_FAILED', |
| 223 | + INVALID_CONFIG = 'INVALID_CONFIG', |
| 224 | + RUM_DATA_COLLECTION_FAILED = 'RUM_DATA_COLLECTION_FAILED' |
| 225 | +} |
| 226 | +``` |
| 227 | +
|
| 228 | +### Error Handling Example |
| 229 | +```typescript |
| 230 | +try { |
| 231 | + await analyzer.analyze(); |
| 232 | +} catch (error) { |
| 233 | + if (error.code === BundleSizeError.SIZE_LIMIT_EXCEEDED) { |
| 234 | + console.error(`Bundle size exceeded: ${error.bundle.name}`); |
| 235 | + } |
| 236 | +} |
| 237 | +``` |
| 238 | +
|
| 239 | +## Middleware |
| 240 | +
|
| 241 | +### Express Middleware |
| 242 | +```typescript |
| 243 | +import { rumMiddleware } from '@avixiii/bundle-size-tracker/middleware'; |
| 244 | + |
| 245 | +app.use('/api/rum', rumMiddleware({ |
| 246 | + storage: 'mongodb', |
| 247 | + url: process.env.MONGODB_URL |
| 248 | +})); |
| 249 | +``` |
| 250 | +
|
| 251 | +### Custom Storage Adapter |
| 252 | +```typescript |
| 253 | +interface StorageAdapter { |
| 254 | + save(data: RUMData): Promise<void>; |
| 255 | + get(query: Query): Promise<RUMData[]>; |
| 256 | + clear(): Promise<void>; |
| 257 | +} |
| 258 | + |
| 259 | +class CustomStorage implements StorageAdapter { |
| 260 | + // Implement storage methods |
| 261 | +} |
| 262 | +``` |
0 commit comments