-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
157 lines (151 loc) · 4.88 KB
/
vite.config.ts
File metadata and controls
157 lines (151 loc) · 4.88 KB
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
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { visualizer } from 'rollup-plugin-visualizer'
// https://vite.dev/config/
export default defineConfig(({ mode }) => ({
plugins: [
react(),
// Bundle analyzer - only in analyze mode
...(mode === 'analyze' ? [
visualizer({
filename: 'dist/stats.html',
open: true,
gzipSize: true,
brotliSize: true,
template: 'treemap', // Better visualization
})
] : [])
],
css: {
modules: {
localsConvention: 'camelCase',
generateScopedName: '[name]__[local]___[hash:base64:5]'
},
// PostCSS configuration is automatically loaded from postcss.config.js
postcss: './postcss.config.js'
},
build: {
// Enable minification
minify: 'terser',
// Configure terser options for better compression
terserOptions: {
compress: {
drop_console: true, // Remove console.log in production
drop_debugger: true, // Remove debugger statements
pure_funcs: ['console.log', 'console.info', 'console.debug'], // Remove specific console methods
passes: 2, // Run compression twice for better results
},
mangle: {
safari10: true, // Fix Safari 10 issues
},
format: {
comments: false, // Remove comments
},
},
// Enable CSS code splitting
cssCodeSplit: true,
// Configure chunk splitting for better caching
rollupOptions: {
output: {
// Manual chunk splitting for better caching
manualChunks: (id) => {
// Vendor chunk for React and React DOM
if (id.includes('node_modules')) {
if (id.includes('react') || id.includes('react-dom')) {
return 'vendor';
}
// Other node_modules go to a separate chunk
return 'libs';
}
// Data chunks for question sections (enables code splitting)
if (id.includes('src/data/sections/')) {
const sectionName = id.split('/').pop()?.replace('.ts', '');
return `section-${sectionName}`;
}
// Main data loader
if (id.includes('src/data/')) {
return 'data';
}
// Component chunks for large components
if (id.includes('src/components/')) {
return 'components';
}
},
// Optimize chunk file names for caching
chunkFileNames: 'assets/[name]-[hash].js',
entryFileNames: 'assets/[name]-[hash].js',
assetFileNames: (assetInfo) => {
// Organize assets by type
const info = assetInfo.name?.split('.') || [];
const ext = info[info.length - 1];
if (/png|jpe?g|svg|gif|tiff|bmp|ico/i.test(ext)) {
return `assets/images/[name]-[hash].[ext]`;
}
if (/woff2?|eot|ttf|otf/i.test(ext)) {
return `assets/fonts/[name]-[hash].[ext]`;
}
return `assets/[name]-[hash].[ext]`;
},
},
// Enable tree shaking
treeshake: {
moduleSideEffects: false,
propertyReadSideEffects: false,
unknownGlobalSideEffects: false,
},
},
// Set chunk size warning limit (500kb)
chunkSizeWarningLimit: 500,
// Disable source maps for production (reduces bundle size)
sourcemap: false,
// Target modern browsers for better optimization
target: 'es2020',
// Enable CSS minification
cssMinify: true,
// Enable asset inlining for small files
assetsInlineLimit: 4096, // 4kb
},
// Optimize dependencies
optimizeDeps: {
include: ['react', 'react-dom'],
// Force pre-bundling of these dependencies
force: false,
// Exclude large dependencies that should be code-split
exclude: [],
},
// Configure for static site generation
base: './', // Use relative paths for static deployment
// Production server configuration
preview: {
port: 4173,
strictPort: true,
// Enable compression
headers: {
'Cache-Control': 'public, max-age=31536000', // 1 year cache for assets
},
},
// Development server optimizations
server: {
hmr: {
overlay: false, // Disable error overlay in development
},
// Enable compression in development
middlewareMode: false,
},
// Enable experimental features for better performance
esbuild: {
// Remove console.log in production
drop: mode === 'production' ? ['console', 'debugger'] : [],
// Enable legal comments removal
legalComments: 'none',
// Target modern browsers for better optimization
target: 'es2020',
},
// Define global constants for optimization
define: {
// Remove development-only code in production
__DEV__: mode === 'development',
// Enable performance monitoring only in development
__PERFORMANCE_MONITORING__: mode === 'development',
},
}))