Spaces:
Running
Running
File size: 3,825 Bytes
0646b18 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# Frontend Bundle Optimization Summary
## π― Results
### Before Optimization
- **Single vendor bundle**: 16 MB (`vendors.98d59d7116d9a52db7bd.js`)
- **Source map**: 15 MB
- **Build mode**: Development (unminified)
- **Total**: ~31 MB
### After Optimization
- **Total JS size**: 6.67 MB (app bundles only, excluding static files)
- **Total dist size**: 7.07 MB
- **Build mode**: Production (minified)
- **Bundle splitting**: 91 optimized chunks
- **Reduction**: **~58% smaller** (from 16 MB to 6.67 MB)
## β
Optimizations Applied
### 1. **Production Build Mode**
- Changed from development to production mode
- Enabled minification with TerserPlugin
- Removed console.log statements in production
- Disabled source maps in production
### 2. **Advanced Code Splitting**
Split the monolithic vendor bundle into multiple optimized chunks:
- **Carbon Icons**: 21 chunks (~73 KB total)
- **Carbon AI**: 3 chunks (~1.6 MB total)
- **React Vendor**: Separate chunk (133 KB)
- **Other Vendors**: Multiple small chunks for better caching
### 3. **Tree Shaking**
- Enabled `usedExports: true` for dead code elimination
- Set `sideEffects: false` for aggressive tree shaking
- Configured Babel with `modules: false` to preserve ES6 modules
### 4. **Font Optimization**
- Added rule to prevent unnecessary font bundling
- Set `emit: false` for font assets to avoid duplication
### 5. **Terser Configuration**
- Remove comments
- Drop console statements in production
- Drop debugger statements
- Aggressive compression
## π Bundle Composition
### Largest Chunks
1. `carbon-ai-3959e959.js` - 1.1 MB (main Carbon AI Chat)
2. `carbon-ai-1e0ec562.js` - 408 KB (Carbon AI components)
3. `vendors-e567d44f.js` - 271 KB (vendor utilities)
4. `vendors-20baa8a5.js` - 270 KB (vendor utilities)
5. `vendors-e9c36b74.js` - 264 KB (vendor utilities)
## π Usage
### Build for Production
```bash
./build.sh
```
Or manually:
```bash
NODE_ENV=production pnpm run build
```
### Build for Development
```bash
pnpm run dev
```
## π‘ Further Optimization Ideas
### 1. **Lazy Loading Components**
Consider lazy loading heavy components like CardManager:
```typescript
const CardManager = React.lazy(() => import('./CardManager'));
```
### 2. **Dynamic Imports for Icons**
Instead of importing all icons, import only what's needed:
```typescript
// Bad: imports all icons
import { IconName } from '@carbon/icons-react';
// Good: import specific icons
import IconName from '@carbon/icons-react/es/icon-name';
```
### 3. **Reduce Carbon AI Chat Bundle**
- Check if you can use lighter alternatives
- Consider custom implementation for specific features
- Evaluate if all Carbon AI Chat features are needed
### 4. **CDN for Large Dependencies**
Consider loading React, React-DOM from CDN:
```html
<script src="https://cdn.jsdelivr.net/npm/react@18/umd/react.production.min.js"></script>
```
### 5. **Compression at Server Level**
Enable gzip/brotli compression on your web server:
- Gzip can reduce text-based assets by 70-80%
- Brotli can achieve even better compression
### 6. **Remove Unused Dependencies**
Review and remove unused packages:
```bash
npx depcheck
```
### 7. **Analyze Bundle Regularly**
Run bundle analyzer to track growth:
```bash
pnpm add -D webpack-bundle-analyzer
npx webpack-bundle-analyzer dist/bundle-stats.json
```
## π Notes
- The Carbon AI Chat library is the largest dependency (~1.5 MB minified)
- Carbon Icons are now split into 21 chunks for better caching
- The build now uses content hashing for optimal browser caching
- Static files (tailwind.js, background.js) remain unoptimized as they're copied as-is
## π Monitoring
To check bundle sizes after changes:
```bash
cd dist
find . -name "*.js" ! -name "tailwind.js" ! -name "background.js" -type f | xargs du -ck | tail -1
```
|