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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
---
name: optimize-config
description: Optimize TailwindCSS configuration for better performance, smaller bundle size, and efficient development workflow
tools: Read, Edit, Bash, Grep, Glob
---
# Optimize TailwindCSS Configuration
This command analyzes and optimizes your TailwindCSS setup for maximum performance and minimal bundle size.
## What This Command Does
1. **Content Path Optimization**
- Analyzes project structure to optimize content scanning
- Configures precise file patterns for better purging
- Excludes unnecessary directories and files
2. **Bundle Size Analysis**
- Identifies unused utilities in your CSS bundle
- Optimizes safelist configuration
- Configures effective CSS purging strategies
3. **Build Performance**
- Optimizes PostCSS pipeline configuration
- Configures caching strategies
- Sets up development vs production optimizations
4. **Plugin and Theme Cleanup**
- Removes unused plugins and theme extensions
- Optimizes custom utility configurations
- Cleans up redundant theme settings
## Usage Examples
### Analyze Current Bundle Size
```bash
# Build CSS and analyze size
npx tailwindcss -i ./src/styles.css -o ./dist/output.css
wc -c ./dist/output.css
# With minification
npx tailwindcss -i ./src/styles.css -o ./dist/output.css --minify
wc -c ./dist/output.css
# Compress with Brotli
brotli -q 11 ./dist/output.css
ls -lh ./dist/output.css.br
```
### Content Path Optimization
```javascript
// Before: Generic paths
module.exports = {
content: ["./src/**/*.{js,jsx,ts,tsx}"],
}
// After: Specific optimized paths
module.exports = {
content: [
// Be specific about directories
'./pages/**/*.{js,ts,jsx,tsx,mdx}',
'./components/**/*.{js,ts,jsx,tsx}',
'./app/**/*.{js,ts,jsx,tsx}',
'./lib/**/*.{js,ts}',
// Include component libraries if used
'./node_modules/@your-ui-lib/**/*.{js,ts,jsx,tsx}',
// Exclude unnecessary files
'!./node_modules',
'!./.git',
'!./.next',
'!./dist',
'!./coverage',
],
}
```
### Advanced Content Configuration
```javascript
module.exports = {
content: [
{
files: ['./src/**/*.{js,ts,jsx,tsx}'],
// Custom extraction for complex patterns
transform: {
js: (content) => {
// Extract classes from template literals
return content.match(/(?:class|className)(?:Name)?[`:=]\s*[`"']([^`"']*)[`"']/g) || []
}
}
},
{
files: ['./components/**/*.{js,ts,jsx,tsx}'],
// Extract dynamic class compositions
transform: {
jsx: (content) => {
const matches = content.match(/(?:clsx|cn|twMerge)\([^)]*\)/g) || []
return matches.join(' ')
}
}
}
]
}
```
## Performance Optimizations
### Production Build Configuration
```javascript
// postcss.config.js - Environment-specific optimization
module.exports = {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
// Production-only optimizations
...(process.env.NODE_ENV === 'production' ? [
require('@fullhuman/postcss-purgecss')({
content: [
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}',
],
defaultExtractor: content => content.match(/[\w-/:]+(?<!:)/g) || [],
safelist: {
standard: [/^hljs/, /^prose/],
deep: [/^animate-/, /^transition-/],
greedy: [/^bg-/, /^text-/, /^border-/]
}
}),
require('cssnano')({
preset: ['advanced', {
discardComments: { removeAll: true },
reduceIdents: false,
zindex: false,
}]
})
] : [])
]
}
```
### Webpack/Next.js Optimization
```javascript
// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
optimizeCss: true,
swcMinify: true,
},
webpack: (config, { dev, isServer }) => {
// CSS optimization for production
if (!dev && !isServer) {
config.optimization.splitChunks.cacheGroups.styles = {
name: 'styles',
test: /\.(css|scss)$/,
chunks: 'all',
enforce: true,
}
}
return config
},
}
module.exports = nextConfig
```
### Vite Optimization
```javascript
// vite.config.js
import { defineConfig } from 'vite'
export default defineConfig({
css: {
postcss: './postcss.config.js',
devSourcemap: true,
},
build: {
cssCodeSplit: true,
cssMinify: 'esbuild',
rollupOptions: {
output: {
manualChunks: {
'tailwind-base': ['tailwindcss/base'],
'tailwind-components': ['tailwindcss/components'],
'tailwind-utilities': ['tailwindcss/utilities']
}
}
},
reportCompressedSize: true,
chunkSizeWarningLimit: 1000,
},
})
```
## Safelist Optimization
### Smart Safelist Configuration
```javascript
module.exports = {
safelist: [
// Dynamic color variations
{
pattern: /^(bg|text|border)-(red|green|blue|yellow|purple)-(50|100|500|600|700|900)$/,
variants: ['hover', 'focus', 'active', 'disabled'],
},
// Animation and state classes
{
pattern: /^(opacity|scale|rotate|translate[xy]?)-(0|25|50|75|100)$/,
variants: ['group-hover', 'peer-focus', 'motion-reduce'],
},
// Responsive grid columns (often dynamically generated)
/^grid-cols-(1|2|3|4|6|12)$/,
// Common state classes
/^(animate|transition)-.+/,
// Dynamic spacing that might be calculated
{
pattern: /^(p|m|w|h)-(0|1|2|4|8|16|32|64)$/,
variants: ['sm', 'md', 'lg', 'xl', '2xl'],
},
],
// Block classes that should never be included
blocklist: [
'container', // If using custom container
'debug-*', // Debug utilities
],
}
```
## Bundle Analysis Tools
### CSS Analysis Script
```javascript
// scripts/analyze-css.js
const fs = require('fs')
const path = require('path')
function analyzeCSSBundle(filePath) {
const css = fs.readFileSync(filePath, 'utf8')
// Extract all utility classes
const utilities = css.match(/\.[a-zA-Z][a-zA-Z0-9_-]*\s*{/g) || []
const uniqueUtilities = [...new Set(utilities.map(u => u.replace(/\s*{$/, '')))]
// File size analysis
const stats = fs.statSync(filePath)
const sizeKB = (stats.size / 1024).toFixed(2)
console.log(`CSS Bundle Analysis:`)
console.log(`- File size: ${sizeKB}KB`)
console.log(`- Utility classes: ${uniqueUtilities.length}`)
console.log(`- Average bytes per utility: ${(stats.size / uniqueUtilities.length).toFixed(2)}`)
// Most common utility patterns
const patterns = {}
uniqueUtilities.forEach(utility => {
const pattern = utility.replace(/\d+/g, '#').replace(/-(xs|sm|md|lg|xl|2xl)$/, '-*')
patterns[pattern] = (patterns[pattern] || 0) + 1
})
const topPatterns = Object.entries(patterns)
.sort(([,a], [,b]) => b - a)
.slice(0, 10)
console.log('\nTop utility patterns:')
topPatterns.forEach(([pattern, count]) => {
console.log(`- ${pattern}: ${count} variants`)
})
}
// Usage: node scripts/analyze-css.js dist/output.css
analyzeCSSBundle(process.argv[2])
```
### Unused CSS Detection
```bash
# Using PurgeCSS to find unused CSS
npm install -g purgecss
# Analyze unused CSS
purgecss --css dist/styles.css \
--content 'src/**/*.{js,jsx,ts,tsx}' \
--output temp/ \
--rejected
# Compare sizes
echo "Original size:" && wc -c dist/styles.css
echo "Purged size:" && wc -c temp/styles.css
```
## Monitoring and Automation
### GitHub Actions for Bundle Size Monitoring
```yaml
# .github/workflows/css-size-check.yml
name: CSS Bundle Size Check
on: [pull_request]
jobs:
css-size:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build CSS
run: npm run build:css
- name: Check bundle size
run: |
SIZE=$(wc -c < dist/styles.css)
echo "CSS bundle size: $SIZE bytes"
if [ $SIZE -gt 100000 ]; then
echo "❌ CSS bundle is too large (>100KB)"
exit 1
else
echo "✅ CSS bundle size is acceptable"
fi
- name: Comment PR
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const size = fs.statSync('dist/styles.css').size;
const sizeKB = (size / 1024).toFixed(2);
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `📊 CSS Bundle Size: ${sizeKB}KB`
});
```
### Pre-commit Hook for CSS Optimization
```bash
#!/bin/sh
# .husky/pre-commit
# Build CSS and check size
npm run build:css
# Check if CSS file is too large
SIZE=$(wc -c < dist/styles.css)
if [ $SIZE -gt 100000 ]; then
echo "❌ CSS bundle is too large (${SIZE} bytes > 100KB)"
echo "Consider optimizing your Tailwind configuration"
exit 1
fi
echo "✅ CSS bundle size is acceptable (${SIZE} bytes)"
```
## Optimization Checklist
### Performance Checklist
- [ ] Content paths are specific and exclude unnecessary files
- [ ] Safelist includes only genuinely dynamic classes
- [ ] Unused plugins are removed from configuration
- [ ] CSS is minified in production builds
- [ ] CSS code splitting is enabled where possible
- [ ] Bundle size is monitored in CI/CD pipeline
### Development Experience Checklist
- [ ] Hot reload works efficiently with content changes
- [ ] Build times are optimized for development
- [ ] Source maps are available for debugging
- [ ] Error reporting is clear for configuration issues
### Production Checklist
- [ ] CSS is compressed (Gzip/Brotli)
- [ ] Critical CSS is inlined where beneficial
- [ ] Unused CSS is properly purged
- [ ] Bundle analysis is automated
- [ ] Performance monitoring is in place
Remember: **Optimize for your specific use case, measure before and after, and maintain monitoring over time!**
|