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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
---
description: Analyze and optimize bundle size
argument-hint:
allowed-tools: Bash, Read, Edit, MultiEdit
---
Analyze bundle size and optimize for production.
## Instructions
1. Run bundle analysis
2. Identify large dependencies
3. Find unused code
4. Implement optimization strategies
5. Generate optimization report
## Analysis Tools
### Next.js
```bash
# Install bundle analyzer
npm install -D @next/bundle-analyzer
# Configure next.config.js
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
})
module.exports = withBundleAnalyzer({
// your config
})
# Run analysis
ANALYZE=true npm run build
```
### Vite
```bash
# Install rollup plugin
npm install -D rollup-plugin-visualizer
# Add to vite.config.ts
import { visualizer } from 'rollup-plugin-visualizer'
plugins: [
visualizer({
open: true,
gzipSize: true,
brotliSize: true,
})
]
# Run build
npm run build
```
### General
```bash
# webpack-bundle-analyzer
npm install -D webpack-bundle-analyzer
# source-map-explorer
npm install -D source-map-explorer
npm run build
npx source-map-explorer 'build/static/js/*.js'
```
## Optimization Strategies
### 1. Code Splitting
```tsx
// Dynamic imports
const HeavyComponent = lazy(() => import('./HeavyComponent'))
// Route-based splitting (Next.js)
export default function Page() {
return <div>Auto code-split by route</div>
}
// Conditional loading
if (userNeedsFeature) {
const module = await import('./feature')
module.initialize()
}
```
### 2. Tree Shaking
```tsx
// ❌ Bad - imports entire library
import _ from 'lodash'
// ✅ Good - imports only what's needed
import debounce from 'lodash/debounce'
// For shadcn/ui - already optimized!
// Components are copied, not imported from package
```
### 3. Component Optimization
```tsx
// Memoize expensive components
const MemoizedComponent = memo(ExpensiveComponent)
// Lazy load heavy components
const Chart = lazy(() => import('./Chart'))
<Suspense fallback={<Skeleton />}>
<Chart />
</Suspense>
```
### 4. Asset Optimization
```tsx
// Next.js Image optimization
import Image from 'next/image'
<Image
src="/hero.jpg"
width={1200}
height={600}
priority
alt="Hero"
/>
// Font optimization
import { Inter } from 'next/font/google'
const inter = Inter({
subsets: ['latin'],
display: 'swap',
})
```
### 5. Dependency Optimization
```json
// Use lighter alternatives
{
"dependencies": {
// "moment": "^2.29.0", // 67kb
"date-fns": "^2.29.0", // 13kb (tree-shakeable)
// "lodash": "^4.17.0", // 71kb
"lodash-es": "^4.17.0", // Tree-shakeable
}
}
```
### 6. Tailwind CSS Optimization
```js
// tailwind.config.js
module.exports = {
content: [
// Be specific to avoid scanning unnecessary files
'./app/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}',
],
// Remove unused styles in production
purge: process.env.NODE_ENV === 'production' ? [
'./app/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}',
] : [],
}
```
## Optimization Checklist
- [ ] Enable production mode
- [ ] Remove console.logs and debug code
- [ ] Minify JavaScript and CSS
- [ ] Enable gzip/brotli compression
- [ ] Optimize images (WebP, AVIF)
- [ ] Lazy load non-critical resources
- [ ] Use CDN for static assets
- [ ] Implement caching strategies
- [ ] Remove unused dependencies
- [ ] Tree shake imports
## Report Format
```markdown
# Bundle Optimization Report
## Current Stats
- Total bundle size: XXXkb
- Gzipped size: XXXkb
- Largest chunks: [...]
## Issues Found
1. Large dependency: [package] (XXXkb)
2. Duplicate code in: [files]
3. Unused exports in: [modules]
## Optimizations Applied
1. ✅ Code split [component]
2. ✅ Lazy loaded [routes]
3. ✅ Replaced [heavy-lib] with [light-lib]
## Results
- Bundle size reduced by: XX%
- Initial load improved by: XXms
- Lighthouse score: XX → XX
## Recommendations
1. Consider replacing...
2. Lazy load...
3. Split chunk for...
```
## Example
If the user says: `/optimize-bundle`
1. Analyze current bundle size
2. Identify optimization opportunities:
- Large dependencies to replace
- Components to lazy load
- Unused code to remove
3. Implement optimizations
4. Re-analyze and compare results
5. Generate detailed report
|