claudify-agent 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/QUICK-REFERENCE.md +213 -213
- package/.claude/cost-dashboard.md +162 -162
- package/.claude/docs/COST-OPTIMIZATION.md +402 -403
- package/.claude/docs/MCP-INTEGRATION-GUIDE.md +348 -348
- package/.claude/docs/PLAN-MODE-OPTIMIZATION.md +835 -836
- package/.claude/hooks/auto-learning-review.ts +200 -195
- package/.claude/hooks/auto-lint-fix.ts +60 -104
- package/.claude/hooks/auto-security-review.ts +314 -354
- package/.claude/hooks/auto-validate.ts +249 -348
- package/.claude/hooks/lib/claude-hook.ts +168 -0
- package/.claude/hooks/plan-quality-tracker.ts +598 -719
- package/.claude/rules/anti-patterns.md +22 -22
- package/.claude/rules/context-tier-1.md +41 -41
- package/.claude/rules/context-tier-2.md +285 -285
- package/.claude/rules/general.md +25 -25
- package/.claude/rules/mcp-auto-use.md +46 -46
- package/.claude/rules/parallel-exploration-auto.md +30 -31
- package/.claude/rules/plan-mode-execution.md +289 -289
- package/.claude/rules/plan-mode-research.md +34 -34
- package/.claude/rules/plan-mode-structure.md +166 -166
- package/.claude/rules/plan-mode-validation.md +129 -130
- package/.claude/rules/security-critical.md +19 -19
- package/.claude/rules/tool-auto-selection.md +45 -45
- package/.claude/rules/tool-strategy.md +22 -22
- package/.claude/scripts/security-scan.js +1 -1
- package/.claude/security-dashboard.md +142 -142
- package/.claude/settings.json +50 -0
- package/.claude/skills/debug-optimizer/SKILL.md +420 -399
- package/.claude/skills/learning-loop/SKILL.md +313 -308
- package/.claude/skills/mcp-mastery/SKILL.md +218 -214
- package/.claude/skills/nextjs-guide/SKILL.md +687 -687
- package/.claude/skills/parallel-exploration/SKILL.md +197 -193
- package/.claude/skills/plan-mode-mastery/SKILL.md +268 -263
- package/.claude/skills/python-guide/SKILL.md +822 -822
- package/.claude/skills/react-guide/SKILL.md +762 -762
- package/.claude/skills/{security.md → security-guide/SKILL.md} +10 -0
- package/.claude/skills/solidity-guide/SKILL.md +5 -0
- package/.claude/skills/tool-selection/SKILL.md +433 -428
- package/.claude/skills/vite-guide/SKILL.md +445 -445
- package/CLAUDE.md +1 -0
- package/README.md +74 -35
- package/README.vi.md +106 -0
- package/bin/cli.js +13 -4
- package/package.json +9 -1
- package/.claude/skills/debug-optimizer.md +0 -12
- package/.claude/skills/nextjs.md +0 -13
- package/.claude/skills/react.md +0 -12
|
@@ -1,445 +1,445 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: vite-guide
|
|
3
|
-
description: Optimize Vite projects for maximum performance. Use when configuring Vite, improving dev server speed, optimizing builds, or when the user mentions Vite, vite.config, build performance, or HMR issues.
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# Vite Best Practices
|
|
7
|
-
|
|
8
|
-
## Quick Start
|
|
9
|
-
|
|
10
|
-
When working with Vite projects:
|
|
11
|
-
|
|
12
|
-
1. **Check configuration** - Review vite.config.js for performance issues
|
|
13
|
-
2. **Audit plugins** - Identify slow plugins or heavy operations
|
|
14
|
-
3. **Optimize imports** - Remove barrel files, use explicit extensions
|
|
15
|
-
4. **Configure warmup** - Pre-transform frequently used files
|
|
16
|
-
5. **Profile if slow** - Use `vite --profile` to identify bottlenecks
|
|
17
|
-
|
|
18
|
-
## Configuration Fundamentals
|
|
19
|
-
|
|
20
|
-
### Use defineConfig
|
|
21
|
-
|
|
22
|
-
Always use the `defineConfig` helper for TypeScript intelligence:
|
|
23
|
-
|
|
24
|
-
```javascript
|
|
25
|
-
import { defineConfig } from 'vite'
|
|
26
|
-
import react from '@vitejs/plugin-react-swc'
|
|
27
|
-
|
|
28
|
-
export default defineConfig({
|
|
29
|
-
plugins: [react()],
|
|
30
|
-
// ... config options
|
|
31
|
-
})
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
### Plugin Selection
|
|
35
|
-
|
|
36
|
-
**Preferred**: Use `@vitejs/plugin-react-swc` instead of `@vitejs/plugin-react` for 6-8x faster HMR.
|
|
37
|
-
|
|
38
|
-
```javascript
|
|
39
|
-
// ✅ Good - SWC-based (fast)
|
|
40
|
-
import react from '@vitejs/plugin-react-swc'
|
|
41
|
-
|
|
42
|
-
// ❌ Avoid - Babel-based (slower)
|
|
43
|
-
import react from '@vitejs/plugin-react'
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
## Performance Optimization
|
|
47
|
-
|
|
48
|
-
### Development Server Speed
|
|
49
|
-
|
|
50
|
-
#### 1. Replace Babel with SWC
|
|
51
|
-
|
|
52
|
-
Switch to SWC for dramatically faster transformations:
|
|
53
|
-
|
|
54
|
-
```javascript
|
|
55
|
-
// vite.config.js
|
|
56
|
-
import { defineConfig } from 'vite'
|
|
57
|
-
import react from '@vitejs/plugin-react-swc'
|
|
58
|
-
|
|
59
|
-
export default defineConfig({
|
|
60
|
-
plugins: [react()],
|
|
61
|
-
})
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
**Impact**: Reduces HMR transformation from 3-4s to <500ms.
|
|
65
|
-
|
|
66
|
-
#### 2. Avoid Barrel Files
|
|
67
|
-
|
|
68
|
-
Barrel files force Vite to process thousands of modules unnecessarily.
|
|
69
|
-
|
|
70
|
-
```javascript
|
|
71
|
-
// ❌ Bad - Barrel file import
|
|
72
|
-
import { slash, formatDate, debounce } from './utils'
|
|
73
|
-
|
|
74
|
-
// ✅ Good - Direct imports
|
|
75
|
-
import { slash } from './utils/slash.js'
|
|
76
|
-
import { formatDate } from './utils/formatDate.js'
|
|
77
|
-
import { debounce } from './utils/debounce.js'
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
**Why**: Barrel files (index.js re-exporting everything) require loading all files even when you need one function.
|
|
81
|
-
|
|
82
|
-
#### 3. Use Explicit Import Extensions
|
|
83
|
-
|
|
84
|
-
Enable TypeScript bundler module resolution and use explicit extensions:
|
|
85
|
-
|
|
86
|
-
```json
|
|
87
|
-
// tsconfig.json
|
|
88
|
-
{
|
|
89
|
-
"compilerOptions": {
|
|
90
|
-
"moduleResolution": "bundler",
|
|
91
|
-
"allowImportingTsExtensions": true
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
```typescript
|
|
97
|
-
// ✅ Good - Explicit extension
|
|
98
|
-
import { Component } from './Component.jsx'
|
|
99
|
-
|
|
100
|
-
// ❌ Avoid - Implicit extension (requires 6+ filesystem checks)
|
|
101
|
-
import { Component } from './Component'
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
#### 4. Configure File Warmup
|
|
105
|
-
|
|
106
|
-
Pre-transform frequently used files to avoid request waterfalls:
|
|
107
|
-
|
|
108
|
-
```javascript
|
|
109
|
-
// vite.config.js
|
|
110
|
-
export default defineConfig({
|
|
111
|
-
server: {
|
|
112
|
-
warmup: {
|
|
113
|
-
clientFiles: [
|
|
114
|
-
'./src/main.jsx',
|
|
115
|
-
'./src/App.jsx',
|
|
116
|
-
'./src/components/BigComponent.jsx',
|
|
117
|
-
'./src/utils/big-utils.js',
|
|
118
|
-
],
|
|
119
|
-
},
|
|
120
|
-
},
|
|
121
|
-
})
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
**How to find files to warmup**: Run `vite --debug transform` and look for files with >100ms transform time.
|
|
125
|
-
|
|
126
|
-
#### 5. Optimize Dependency Pre-bundling
|
|
127
|
-
|
|
128
|
-
Control which dependencies are pre-bundled:
|
|
129
|
-
|
|
130
|
-
```javascript
|
|
131
|
-
// vite.config.js
|
|
132
|
-
export default defineConfig({
|
|
133
|
-
optimizeDeps: {
|
|
134
|
-
include: ['lodash-es', 'axios'], // Force pre-bundling
|
|
135
|
-
exclude: ['@custom/large-lib'], // Skip pre-bundling
|
|
136
|
-
entries: ['./src/**/*.jsx'], // Custom entry points
|
|
137
|
-
},
|
|
138
|
-
})
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
### Build Optimization
|
|
142
|
-
|
|
143
|
-
#### 1. Manual Chunking
|
|
144
|
-
|
|
145
|
-
Avoid single large bundle by implementing manual chunks:
|
|
146
|
-
|
|
147
|
-
```javascript
|
|
148
|
-
// vite.config.js
|
|
149
|
-
export default defineConfig({
|
|
150
|
-
build: {
|
|
151
|
-
rollupOptions: {
|
|
152
|
-
output: {
|
|
153
|
-
manualChunks: {
|
|
154
|
-
vendor: ['react', 'react-dom'],
|
|
155
|
-
utils: ['lodash-es', 'dayjs'],
|
|
156
|
-
},
|
|
157
|
-
},
|
|
158
|
-
},
|
|
159
|
-
},
|
|
160
|
-
})
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
#### 2. CSS Optimization
|
|
164
|
-
|
|
165
|
-
Use native tooling when possible:
|
|
166
|
-
|
|
167
|
-
```javascript
|
|
168
|
-
// vite.config.js
|
|
169
|
-
export default defineConfig({
|
|
170
|
-
css: {
|
|
171
|
-
transformer: 'lightningcss', // Experimental but faster
|
|
172
|
-
lightningcssOptions: {
|
|
173
|
-
minify: true,
|
|
174
|
-
},
|
|
175
|
-
},
|
|
176
|
-
})
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
**Alternative**: Stick with PostCSS for better compatibility.
|
|
180
|
-
|
|
181
|
-
#### 3. Asset Handling
|
|
182
|
-
|
|
183
|
-
**Preferred**: Import SVGs as URLs, not components:
|
|
184
|
-
|
|
185
|
-
```javascript
|
|
186
|
-
// ✅ Good - URL import
|
|
187
|
-
import logo from './logo.svg'
|
|
188
|
-
<img src={logo} alt="Logo" />
|
|
189
|
-
|
|
190
|
-
// ❌ Avoid - SVG as component (slower)
|
|
191
|
-
import { ReactComponent as Logo } from './logo.svg'
|
|
192
|
-
<Logo />
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
## Plugin Audit Practices
|
|
196
|
-
|
|
197
|
-
### Slow Plugin Detection
|
|
198
|
-
|
|
199
|
-
Identify slow plugins using debug mode:
|
|
200
|
-
|
|
201
|
-
```bash
|
|
202
|
-
# See transform times
|
|
203
|
-
vite --debug plugin-transform
|
|
204
|
-
|
|
205
|
-
# Profile entire build
|
|
206
|
-
vite --profile
|
|
207
|
-
# Press 'p + enter' to generate .cpuprofile
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
### Plugin Best Practices
|
|
211
|
-
|
|
212
|
-
When evaluating community plugins:
|
|
213
|
-
|
|
214
|
-
1. **Check hooks**: Avoid plugins with heavy `buildStart`, `config`, or `configResolved` hooks
|
|
215
|
-
2. **Dynamic imports**: Large dependencies should be dynamically imported
|
|
216
|
-
3. **Conditional transforms**: Plugins should check file extensions before transforming
|
|
217
|
-
|
|
218
|
-
**Example optimization** (for plugin authors):
|
|
219
|
-
|
|
220
|
-
```javascript
|
|
221
|
-
// ✅ Good - Check before transform
|
|
222
|
-
transform(code, id) {
|
|
223
|
-
if (!id.endsWith('.jsx')) return null
|
|
224
|
-
if (!code.includes('useState')) return null
|
|
225
|
-
// ... transformation
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// ❌ Bad - Always transform
|
|
229
|
-
transform(code, id) {
|
|
230
|
-
// ... heavy transformation
|
|
231
|
-
}
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
## Common Performance Issues
|
|
235
|
-
|
|
236
|
-
### Issue 1: Slow Dev Server Start
|
|
237
|
-
|
|
238
|
-
**Symptoms**: Server takes >5s to start.
|
|
239
|
-
|
|
240
|
-
**Fixes**:
|
|
241
|
-
1. Check for plugins with heavy `config` hooks
|
|
242
|
-
2. Reduce `optimizeDeps.include` to essentials only
|
|
243
|
-
3. Use `@vitejs/plugin-react-swc` instead of Babel
|
|
244
|
-
4. Run `vite --profile` to identify bottlenecks
|
|
245
|
-
|
|
246
|
-
### Issue 2: Slow HMR Updates
|
|
247
|
-
|
|
248
|
-
**Symptoms**: Changes take >1s to reflect in browser.
|
|
249
|
-
|
|
250
|
-
**Fixes**:
|
|
251
|
-
1. Remove barrel file imports
|
|
252
|
-
2. Disable "Disable Cache" in Chrome DevTools Network tab
|
|
253
|
-
3. Check browser extensions (test in incognito mode)
|
|
254
|
-
4. Warm up frequently changed files
|
|
255
|
-
|
|
256
|
-
### Issue 3: Large Production Bundles
|
|
257
|
-
|
|
258
|
-
**Symptoms**: Main chunk >500KB gzipped.
|
|
259
|
-
|
|
260
|
-
**Fixes**:
|
|
261
|
-
1. Implement manual chunking
|
|
262
|
-
2. Check for accidentally bundled node_modules
|
|
263
|
-
3. Use dynamic imports for heavy components
|
|
264
|
-
4. Analyze bundle with `npm run build -- --sourcemap`
|
|
265
|
-
|
|
266
|
-
### Issue 4: Slow Full Page Reloads
|
|
267
|
-
|
|
268
|
-
**Symptoms**: Full reload takes >3s.
|
|
269
|
-
|
|
270
|
-
**Fixes**:
|
|
271
|
-
1. Ensure browser cache is enabled (not disabled in DevTools)
|
|
272
|
-
2. Reduce number of transformed files
|
|
273
|
-
3. Avoid transforming SVGs to components
|
|
274
|
-
4. Use CSS instead of Sass/Less when possible
|
|
275
|
-
|
|
276
|
-
## Profiling and Debugging
|
|
277
|
-
|
|
278
|
-
### Debug Commands
|
|
279
|
-
|
|
280
|
-
```bash
|
|
281
|
-
# Profile startup and transformations
|
|
282
|
-
vite --profile
|
|
283
|
-
|
|
284
|
-
# See detailed transform timings
|
|
285
|
-
vite --debug plugin-transform
|
|
286
|
-
|
|
287
|
-
# Debug resolve operations
|
|
288
|
-
vite --debug resolve
|
|
289
|
-
|
|
290
|
-
# Check transform cache
|
|
291
|
-
vite --debug transform
|
|
292
|
-
```
|
|
293
|
-
|
|
294
|
-
### Analyzing CPU Profiles
|
|
295
|
-
|
|
296
|
-
1. Run `vite --profile`
|
|
297
|
-
2. Use the app normally
|
|
298
|
-
3. Press `p + enter` in terminal
|
|
299
|
-
4. Open `.cpuprofile` in [speedscope.app](https://www.speedscope.app/)
|
|
300
|
-
5. Look for:
|
|
301
|
-
- Long-running hooks (`buildStart`, `config`)
|
|
302
|
-
- Slow transformations (>100ms)
|
|
303
|
-
- Excessive resolve operations
|
|
304
|
-
|
|
305
|
-
### Performance Checklist
|
|
306
|
-
|
|
307
|
-
Before considering a Vite project optimized:
|
|
308
|
-
|
|
309
|
-
- [ ] Using `@vitejs/plugin-react-swc` (for React projects)
|
|
310
|
-
- [ ] No barrel file imports in critical path
|
|
311
|
-
- [ ] Explicit import extensions enabled
|
|
312
|
-
- [ ] Frequently used files warmed up
|
|
313
|
-
- [ ] Browser cache enabled in DevTools
|
|
314
|
-
- [ ] Manual chunking configured for build
|
|
315
|
-
- [ ] No heavy operations in plugin hooks
|
|
316
|
-
- [ ] Profiled with `vite --profile` (no major bottlenecks)
|
|
317
|
-
|
|
318
|
-
## Commands
|
|
319
|
-
|
|
320
|
-
**Profile Vite**: Run profiling to identify performance issues
|
|
321
|
-
**Optimize config**: Review and optimize vite.config.js
|
|
322
|
-
**Fix HMR**: Diagnose and fix slow HMR updates
|
|
323
|
-
**Audit plugins**: Check for slow community plugins
|
|
324
|
-
|
|
325
|
-
## Examples
|
|
326
|
-
|
|
327
|
-
### Example 1: Optimized vite.config.js
|
|
328
|
-
|
|
329
|
-
```javascript
|
|
330
|
-
import { defineConfig } from 'vite'
|
|
331
|
-
import react from '@vitejs/plugin-react-swc'
|
|
332
|
-
import { visualizer } from 'rollup-plugin-visualizer'
|
|
333
|
-
|
|
334
|
-
export default defineConfig({
|
|
335
|
-
plugins: [
|
|
336
|
-
react(),
|
|
337
|
-
visualizer({ open: true }), // Analyze bundle
|
|
338
|
-
],
|
|
339
|
-
|
|
340
|
-
server: {
|
|
341
|
-
warmup: {
|
|
342
|
-
clientFiles: [
|
|
343
|
-
'./src/main.jsx',
|
|
344
|
-
'./src/App.jsx',
|
|
345
|
-
],
|
|
346
|
-
},
|
|
347
|
-
},
|
|
348
|
-
|
|
349
|
-
optimizeDeps: {
|
|
350
|
-
include: ['lodash-es', 'axios'],
|
|
351
|
-
},
|
|
352
|
-
|
|
353
|
-
build: {
|
|
354
|
-
rollupOptions: {
|
|
355
|
-
output: {
|
|
356
|
-
manualChunks: {
|
|
357
|
-
vendor: ['react', 'react-dom'],
|
|
358
|
-
utils: ['lodash-es', 'dayjs'],
|
|
359
|
-
},
|
|
360
|
-
},
|
|
361
|
-
},
|
|
362
|
-
},
|
|
363
|
-
})
|
|
364
|
-
```
|
|
365
|
-
|
|
366
|
-
### Example 2: Fixing Barrel File Imports
|
|
367
|
-
|
|
368
|
-
**Before** (slow):
|
|
369
|
-
|
|
370
|
-
```javascript
|
|
371
|
-
// src/App.jsx
|
|
372
|
-
import { Button, Input, Modal } from './components'
|
|
373
|
-
import { formatDate, validateEmail } from './utils'
|
|
374
|
-
```
|
|
375
|
-
|
|
376
|
-
**After** (fast):
|
|
377
|
-
|
|
378
|
-
```javascript
|
|
379
|
-
// src/App.jsx
|
|
380
|
-
import { Button } from './components/Button.jsx'
|
|
381
|
-
import { Input } from './components/Input.jsx'
|
|
382
|
-
import { Modal } from './components/Modal.jsx'
|
|
383
|
-
import { formatDate } from './utils/formatDate.js'
|
|
384
|
-
import { validateEmail } from './utils/validateEmail.js'
|
|
385
|
-
```
|
|
386
|
-
|
|
387
|
-
### Example 3: Diagnosing Slow HMR
|
|
388
|
-
|
|
389
|
-
**User**: "My HMR is taking 3-4 seconds to update"
|
|
390
|
-
|
|
391
|
-
**Agent Response**:
|
|
392
|
-
"I'll diagnose the slow HMR issue. Let me check:
|
|
393
|
-
|
|
394
|
-
1. **Browser setup**: Is 'Disable Cache' checked in DevTools?
|
|
395
|
-
- Fix: Uncheck it in Network tab
|
|
396
|
-
|
|
397
|
-
2. **Plugin audit**: Running `vite --debug plugin-transform`
|
|
398
|
-
- Looking for transforms >100ms
|
|
399
|
-
|
|
400
|
-
3. **Import patterns**: Checking for barrel files
|
|
401
|
-
- Found: `import { X } from './utils'` in 5 files
|
|
402
|
-
- Fix: Use direct imports
|
|
403
|
-
|
|
404
|
-
4. **Plugin choice**: Using `@vitejs/plugin-react`?
|
|
405
|
-
- Fix: Switch to `@vitejs/plugin-react-swc`
|
|
406
|
-
|
|
407
|
-
Let me start by checking your vite.config.js and running a profile..."
|
|
408
|
-
|
|
409
|
-
## Additional Resources
|
|
410
|
-
|
|
411
|
-
- [Vite Performance Guide](https://vitejs.dev/guide/performance/)
|
|
412
|
-
- [Vite Config Reference](https://vitejs.dev/config/)
|
|
413
|
-
- [speedscope.app](https://www.speedscope.app/) - CPU profile viewer
|
|
414
|
-
- [rollup-plugin-visualizer](https://github.com/btd/rollup-plugin-visualizer) - Bundle analyzer
|
|
415
|
-
|
|
416
|
-
## Anti-Patterns to Avoid
|
|
417
|
-
|
|
418
|
-
### 1. Windows-Style Paths
|
|
419
|
-
- ✅ Use: `./src/components/Button.jsx`
|
|
420
|
-
- ❌ Avoid: `.\src\components\Button.jsx`
|
|
421
|
-
|
|
422
|
-
### 2. Too Many Warmup Files
|
|
423
|
-
```javascript
|
|
424
|
-
// ❌ Bad - Overloads dev server
|
|
425
|
-
warmup: {
|
|
426
|
-
clientFiles: ['./src/**/*'] // All files!
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
// ✅ Good - Only critical files
|
|
430
|
-
warmup: {
|
|
431
|
-
clientFiles: [
|
|
432
|
-
'./src/main.jsx',
|
|
433
|
-
'./src/App.jsx',
|
|
434
|
-
]
|
|
435
|
-
}
|
|
436
|
-
```
|
|
437
|
-
|
|
438
|
-
### 3. Time-Sensitive Configuration
|
|
439
|
-
```javascript
|
|
440
|
-
// ❌ Bad - Will become outdated
|
|
441
|
-
// "Use Vite 5.0 features before 2026"
|
|
442
|
-
|
|
443
|
-
// ✅ Good - Use current stable features
|
|
444
|
-
// Use manualChunks for code splitting
|
|
445
|
-
```
|
|
1
|
+
---
|
|
2
|
+
name: vite-guide
|
|
3
|
+
description: Optimize Vite projects for maximum performance. Use when configuring Vite, improving dev server speed, optimizing builds, or when the user mentions Vite, vite.config, build performance, or HMR issues.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Vite Best Practices
|
|
7
|
+
|
|
8
|
+
## Quick Start
|
|
9
|
+
|
|
10
|
+
When working with Vite projects:
|
|
11
|
+
|
|
12
|
+
1. **Check configuration** - Review vite.config.js for performance issues
|
|
13
|
+
2. **Audit plugins** - Identify slow plugins or heavy operations
|
|
14
|
+
3. **Optimize imports** - Remove barrel files, use explicit extensions
|
|
15
|
+
4. **Configure warmup** - Pre-transform frequently used files
|
|
16
|
+
5. **Profile if slow** - Use `vite --profile` to identify bottlenecks
|
|
17
|
+
|
|
18
|
+
## Configuration Fundamentals
|
|
19
|
+
|
|
20
|
+
### Use defineConfig
|
|
21
|
+
|
|
22
|
+
Always use the `defineConfig` helper for TypeScript intelligence:
|
|
23
|
+
|
|
24
|
+
```javascript
|
|
25
|
+
import { defineConfig } from 'vite'
|
|
26
|
+
import react from '@vitejs/plugin-react-swc'
|
|
27
|
+
|
|
28
|
+
export default defineConfig({
|
|
29
|
+
plugins: [react()],
|
|
30
|
+
// ... config options
|
|
31
|
+
})
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Plugin Selection
|
|
35
|
+
|
|
36
|
+
**Preferred**: Use `@vitejs/plugin-react-swc` instead of `@vitejs/plugin-react` for 6-8x faster HMR.
|
|
37
|
+
|
|
38
|
+
```javascript
|
|
39
|
+
// ✅ Good - SWC-based (fast)
|
|
40
|
+
import react from '@vitejs/plugin-react-swc'
|
|
41
|
+
|
|
42
|
+
// ❌ Avoid - Babel-based (slower)
|
|
43
|
+
import react from '@vitejs/plugin-react'
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Performance Optimization
|
|
47
|
+
|
|
48
|
+
### Development Server Speed
|
|
49
|
+
|
|
50
|
+
#### 1. Replace Babel with SWC
|
|
51
|
+
|
|
52
|
+
Switch to SWC for dramatically faster transformations:
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
// vite.config.js
|
|
56
|
+
import { defineConfig } from 'vite'
|
|
57
|
+
import react from '@vitejs/plugin-react-swc'
|
|
58
|
+
|
|
59
|
+
export default defineConfig({
|
|
60
|
+
plugins: [react()],
|
|
61
|
+
})
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**Impact**: Reduces HMR transformation from 3-4s to <500ms.
|
|
65
|
+
|
|
66
|
+
#### 2. Avoid Barrel Files
|
|
67
|
+
|
|
68
|
+
Barrel files force Vite to process thousands of modules unnecessarily.
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
// ❌ Bad - Barrel file import
|
|
72
|
+
import { slash, formatDate, debounce } from './utils'
|
|
73
|
+
|
|
74
|
+
// ✅ Good - Direct imports
|
|
75
|
+
import { slash } from './utils/slash.js'
|
|
76
|
+
import { formatDate } from './utils/formatDate.js'
|
|
77
|
+
import { debounce } from './utils/debounce.js'
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Why**: Barrel files (index.js re-exporting everything) require loading all files even when you need one function.
|
|
81
|
+
|
|
82
|
+
#### 3. Use Explicit Import Extensions
|
|
83
|
+
|
|
84
|
+
Enable TypeScript bundler module resolution and use explicit extensions:
|
|
85
|
+
|
|
86
|
+
```json
|
|
87
|
+
// tsconfig.json
|
|
88
|
+
{
|
|
89
|
+
"compilerOptions": {
|
|
90
|
+
"moduleResolution": "bundler",
|
|
91
|
+
"allowImportingTsExtensions": true
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
// ✅ Good - Explicit extension
|
|
98
|
+
import { Component } from './Component.jsx'
|
|
99
|
+
|
|
100
|
+
// ❌ Avoid - Implicit extension (requires 6+ filesystem checks)
|
|
101
|
+
import { Component } from './Component'
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
#### 4. Configure File Warmup
|
|
105
|
+
|
|
106
|
+
Pre-transform frequently used files to avoid request waterfalls:
|
|
107
|
+
|
|
108
|
+
```javascript
|
|
109
|
+
// vite.config.js
|
|
110
|
+
export default defineConfig({
|
|
111
|
+
server: {
|
|
112
|
+
warmup: {
|
|
113
|
+
clientFiles: [
|
|
114
|
+
'./src/main.jsx',
|
|
115
|
+
'./src/App.jsx',
|
|
116
|
+
'./src/components/BigComponent.jsx',
|
|
117
|
+
'./src/utils/big-utils.js',
|
|
118
|
+
],
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
})
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
**How to find files to warmup**: Run `vite --debug transform` and look for files with >100ms transform time.
|
|
125
|
+
|
|
126
|
+
#### 5. Optimize Dependency Pre-bundling
|
|
127
|
+
|
|
128
|
+
Control which dependencies are pre-bundled:
|
|
129
|
+
|
|
130
|
+
```javascript
|
|
131
|
+
// vite.config.js
|
|
132
|
+
export default defineConfig({
|
|
133
|
+
optimizeDeps: {
|
|
134
|
+
include: ['lodash-es', 'axios'], // Force pre-bundling
|
|
135
|
+
exclude: ['@custom/large-lib'], // Skip pre-bundling
|
|
136
|
+
entries: ['./src/**/*.jsx'], // Custom entry points
|
|
137
|
+
},
|
|
138
|
+
})
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Build Optimization
|
|
142
|
+
|
|
143
|
+
#### 1. Manual Chunking
|
|
144
|
+
|
|
145
|
+
Avoid single large bundle by implementing manual chunks:
|
|
146
|
+
|
|
147
|
+
```javascript
|
|
148
|
+
// vite.config.js
|
|
149
|
+
export default defineConfig({
|
|
150
|
+
build: {
|
|
151
|
+
rollupOptions: {
|
|
152
|
+
output: {
|
|
153
|
+
manualChunks: {
|
|
154
|
+
vendor: ['react', 'react-dom'],
|
|
155
|
+
utils: ['lodash-es', 'dayjs'],
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
})
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
#### 2. CSS Optimization
|
|
164
|
+
|
|
165
|
+
Use native tooling when possible:
|
|
166
|
+
|
|
167
|
+
```javascript
|
|
168
|
+
// vite.config.js
|
|
169
|
+
export default defineConfig({
|
|
170
|
+
css: {
|
|
171
|
+
transformer: 'lightningcss', // Experimental but faster
|
|
172
|
+
lightningcssOptions: {
|
|
173
|
+
minify: true,
|
|
174
|
+
},
|
|
175
|
+
},
|
|
176
|
+
})
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
**Alternative**: Stick with PostCSS for better compatibility.
|
|
180
|
+
|
|
181
|
+
#### 3. Asset Handling
|
|
182
|
+
|
|
183
|
+
**Preferred**: Import SVGs as URLs, not components:
|
|
184
|
+
|
|
185
|
+
```javascript
|
|
186
|
+
// ✅ Good - URL import
|
|
187
|
+
import logo from './logo.svg'
|
|
188
|
+
<img src={logo} alt="Logo" />
|
|
189
|
+
|
|
190
|
+
// ❌ Avoid - SVG as component (slower)
|
|
191
|
+
import { ReactComponent as Logo } from './logo.svg'
|
|
192
|
+
<Logo />
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## Plugin Audit Practices
|
|
196
|
+
|
|
197
|
+
### Slow Plugin Detection
|
|
198
|
+
|
|
199
|
+
Identify slow plugins using debug mode:
|
|
200
|
+
|
|
201
|
+
```bash
|
|
202
|
+
# See transform times
|
|
203
|
+
vite --debug plugin-transform
|
|
204
|
+
|
|
205
|
+
# Profile entire build
|
|
206
|
+
vite --profile
|
|
207
|
+
# Press 'p + enter' to generate .cpuprofile
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Plugin Best Practices
|
|
211
|
+
|
|
212
|
+
When evaluating community plugins:
|
|
213
|
+
|
|
214
|
+
1. **Check hooks**: Avoid plugins with heavy `buildStart`, `config`, or `configResolved` hooks
|
|
215
|
+
2. **Dynamic imports**: Large dependencies should be dynamically imported
|
|
216
|
+
3. **Conditional transforms**: Plugins should check file extensions before transforming
|
|
217
|
+
|
|
218
|
+
**Example optimization** (for plugin authors):
|
|
219
|
+
|
|
220
|
+
```javascript
|
|
221
|
+
// ✅ Good - Check before transform
|
|
222
|
+
transform(code, id) {
|
|
223
|
+
if (!id.endsWith('.jsx')) return null
|
|
224
|
+
if (!code.includes('useState')) return null
|
|
225
|
+
// ... transformation
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// ❌ Bad - Always transform
|
|
229
|
+
transform(code, id) {
|
|
230
|
+
// ... heavy transformation
|
|
231
|
+
}
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
## Common Performance Issues
|
|
235
|
+
|
|
236
|
+
### Issue 1: Slow Dev Server Start
|
|
237
|
+
|
|
238
|
+
**Symptoms**: Server takes >5s to start.
|
|
239
|
+
|
|
240
|
+
**Fixes**:
|
|
241
|
+
1. Check for plugins with heavy `config` hooks
|
|
242
|
+
2. Reduce `optimizeDeps.include` to essentials only
|
|
243
|
+
3. Use `@vitejs/plugin-react-swc` instead of Babel
|
|
244
|
+
4. Run `vite --profile` to identify bottlenecks
|
|
245
|
+
|
|
246
|
+
### Issue 2: Slow HMR Updates
|
|
247
|
+
|
|
248
|
+
**Symptoms**: Changes take >1s to reflect in browser.
|
|
249
|
+
|
|
250
|
+
**Fixes**:
|
|
251
|
+
1. Remove barrel file imports
|
|
252
|
+
2. Disable "Disable Cache" in Chrome DevTools Network tab
|
|
253
|
+
3. Check browser extensions (test in incognito mode)
|
|
254
|
+
4. Warm up frequently changed files
|
|
255
|
+
|
|
256
|
+
### Issue 3: Large Production Bundles
|
|
257
|
+
|
|
258
|
+
**Symptoms**: Main chunk >500KB gzipped.
|
|
259
|
+
|
|
260
|
+
**Fixes**:
|
|
261
|
+
1. Implement manual chunking
|
|
262
|
+
2. Check for accidentally bundled node_modules
|
|
263
|
+
3. Use dynamic imports for heavy components
|
|
264
|
+
4. Analyze bundle with `npm run build -- --sourcemap`
|
|
265
|
+
|
|
266
|
+
### Issue 4: Slow Full Page Reloads
|
|
267
|
+
|
|
268
|
+
**Symptoms**: Full reload takes >3s.
|
|
269
|
+
|
|
270
|
+
**Fixes**:
|
|
271
|
+
1. Ensure browser cache is enabled (not disabled in DevTools)
|
|
272
|
+
2. Reduce number of transformed files
|
|
273
|
+
3. Avoid transforming SVGs to components
|
|
274
|
+
4. Use CSS instead of Sass/Less when possible
|
|
275
|
+
|
|
276
|
+
## Profiling and Debugging
|
|
277
|
+
|
|
278
|
+
### Debug Commands
|
|
279
|
+
|
|
280
|
+
```bash
|
|
281
|
+
# Profile startup and transformations
|
|
282
|
+
vite --profile
|
|
283
|
+
|
|
284
|
+
# See detailed transform timings
|
|
285
|
+
vite --debug plugin-transform
|
|
286
|
+
|
|
287
|
+
# Debug resolve operations
|
|
288
|
+
vite --debug resolve
|
|
289
|
+
|
|
290
|
+
# Check transform cache
|
|
291
|
+
vite --debug transform
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
### Analyzing CPU Profiles
|
|
295
|
+
|
|
296
|
+
1. Run `vite --profile`
|
|
297
|
+
2. Use the app normally
|
|
298
|
+
3. Press `p + enter` in terminal
|
|
299
|
+
4. Open `.cpuprofile` in [speedscope.app](https://www.speedscope.app/)
|
|
300
|
+
5. Look for:
|
|
301
|
+
- Long-running hooks (`buildStart`, `config`)
|
|
302
|
+
- Slow transformations (>100ms)
|
|
303
|
+
- Excessive resolve operations
|
|
304
|
+
|
|
305
|
+
### Performance Checklist
|
|
306
|
+
|
|
307
|
+
Before considering a Vite project optimized:
|
|
308
|
+
|
|
309
|
+
- [ ] Using `@vitejs/plugin-react-swc` (for React projects)
|
|
310
|
+
- [ ] No barrel file imports in critical path
|
|
311
|
+
- [ ] Explicit import extensions enabled
|
|
312
|
+
- [ ] Frequently used files warmed up
|
|
313
|
+
- [ ] Browser cache enabled in DevTools
|
|
314
|
+
- [ ] Manual chunking configured for build
|
|
315
|
+
- [ ] No heavy operations in plugin hooks
|
|
316
|
+
- [ ] Profiled with `vite --profile` (no major bottlenecks)
|
|
317
|
+
|
|
318
|
+
## Commands
|
|
319
|
+
|
|
320
|
+
**Profile Vite**: Run profiling to identify performance issues
|
|
321
|
+
**Optimize config**: Review and optimize vite.config.js
|
|
322
|
+
**Fix HMR**: Diagnose and fix slow HMR updates
|
|
323
|
+
**Audit plugins**: Check for slow community plugins
|
|
324
|
+
|
|
325
|
+
## Examples
|
|
326
|
+
|
|
327
|
+
### Example 1: Optimized vite.config.js
|
|
328
|
+
|
|
329
|
+
```javascript
|
|
330
|
+
import { defineConfig } from 'vite'
|
|
331
|
+
import react from '@vitejs/plugin-react-swc'
|
|
332
|
+
import { visualizer } from 'rollup-plugin-visualizer'
|
|
333
|
+
|
|
334
|
+
export default defineConfig({
|
|
335
|
+
plugins: [
|
|
336
|
+
react(),
|
|
337
|
+
visualizer({ open: true }), // Analyze bundle
|
|
338
|
+
],
|
|
339
|
+
|
|
340
|
+
server: {
|
|
341
|
+
warmup: {
|
|
342
|
+
clientFiles: [
|
|
343
|
+
'./src/main.jsx',
|
|
344
|
+
'./src/App.jsx',
|
|
345
|
+
],
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
|
|
349
|
+
optimizeDeps: {
|
|
350
|
+
include: ['lodash-es', 'axios'],
|
|
351
|
+
},
|
|
352
|
+
|
|
353
|
+
build: {
|
|
354
|
+
rollupOptions: {
|
|
355
|
+
output: {
|
|
356
|
+
manualChunks: {
|
|
357
|
+
vendor: ['react', 'react-dom'],
|
|
358
|
+
utils: ['lodash-es', 'dayjs'],
|
|
359
|
+
},
|
|
360
|
+
},
|
|
361
|
+
},
|
|
362
|
+
},
|
|
363
|
+
})
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
### Example 2: Fixing Barrel File Imports
|
|
367
|
+
|
|
368
|
+
**Before** (slow):
|
|
369
|
+
|
|
370
|
+
```javascript
|
|
371
|
+
// src/App.jsx
|
|
372
|
+
import { Button, Input, Modal } from './components'
|
|
373
|
+
import { formatDate, validateEmail } from './utils'
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
**After** (fast):
|
|
377
|
+
|
|
378
|
+
```javascript
|
|
379
|
+
// src/App.jsx
|
|
380
|
+
import { Button } from './components/Button.jsx'
|
|
381
|
+
import { Input } from './components/Input.jsx'
|
|
382
|
+
import { Modal } from './components/Modal.jsx'
|
|
383
|
+
import { formatDate } from './utils/formatDate.js'
|
|
384
|
+
import { validateEmail } from './utils/validateEmail.js'
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
### Example 3: Diagnosing Slow HMR
|
|
388
|
+
|
|
389
|
+
**User**: "My HMR is taking 3-4 seconds to update"
|
|
390
|
+
|
|
391
|
+
**Agent Response**:
|
|
392
|
+
"I'll diagnose the slow HMR issue. Let me check:
|
|
393
|
+
|
|
394
|
+
1. **Browser setup**: Is 'Disable Cache' checked in DevTools?
|
|
395
|
+
- Fix: Uncheck it in Network tab
|
|
396
|
+
|
|
397
|
+
2. **Plugin audit**: Running `vite --debug plugin-transform`
|
|
398
|
+
- Looking for transforms >100ms
|
|
399
|
+
|
|
400
|
+
3. **Import patterns**: Checking for barrel files
|
|
401
|
+
- Found: `import { X } from './utils'` in 5 files
|
|
402
|
+
- Fix: Use direct imports
|
|
403
|
+
|
|
404
|
+
4. **Plugin choice**: Using `@vitejs/plugin-react`?
|
|
405
|
+
- Fix: Switch to `@vitejs/plugin-react-swc`
|
|
406
|
+
|
|
407
|
+
Let me start by checking your vite.config.js and running a profile..."
|
|
408
|
+
|
|
409
|
+
## Additional Resources
|
|
410
|
+
|
|
411
|
+
- [Vite Performance Guide](https://vitejs.dev/guide/performance/)
|
|
412
|
+
- [Vite Config Reference](https://vitejs.dev/config/)
|
|
413
|
+
- [speedscope.app](https://www.speedscope.app/) - CPU profile viewer
|
|
414
|
+
- [rollup-plugin-visualizer](https://github.com/btd/rollup-plugin-visualizer) - Bundle analyzer
|
|
415
|
+
|
|
416
|
+
## Anti-Patterns to Avoid
|
|
417
|
+
|
|
418
|
+
### 1. Windows-Style Paths
|
|
419
|
+
- ✅ Use: `./src/components/Button.jsx`
|
|
420
|
+
- ❌ Avoid: `.\src\components\Button.jsx`
|
|
421
|
+
|
|
422
|
+
### 2. Too Many Warmup Files
|
|
423
|
+
```javascript
|
|
424
|
+
// ❌ Bad - Overloads dev server
|
|
425
|
+
warmup: {
|
|
426
|
+
clientFiles: ['./src/**/*'] // All files!
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// ✅ Good - Only critical files
|
|
430
|
+
warmup: {
|
|
431
|
+
clientFiles: [
|
|
432
|
+
'./src/main.jsx',
|
|
433
|
+
'./src/App.jsx',
|
|
434
|
+
]
|
|
435
|
+
}
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
### 3. Time-Sensitive Configuration
|
|
439
|
+
```javascript
|
|
440
|
+
// ❌ Bad - Will become outdated
|
|
441
|
+
// "Use Vite 5.0 features before 2026"
|
|
442
|
+
|
|
443
|
+
// ✅ Good - Use current stable features
|
|
444
|
+
// Use manualChunks for code splitting
|
|
445
|
+
```
|