-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite.config.js
More file actions
116 lines (114 loc) · 3.26 KB
/
vite.config.js
File metadata and controls
116 lines (114 loc) · 3.26 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
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
// 自定义缓存策略插件
function cacheHeaderPlugin() {
return {
name: 'cache-headers',
configResolved(config) {
this.config = config
},
apply: 'serve',
middleware: (req, res, next) => {
// 图片缓存 14 天 (1209600 秒)
if (/\.(png|jpg|jpeg|gif|svg|webp|avif)$/i.test(req.url)) {
res.setHeader('Cache-Control', 'public, max-age=1209600, immutable')
}
// 字体缓存 30 天
else if (/\.(woff|woff2|ttf|otf|eot)$/i.test(req.url)) {
res.setHeader('Cache-Control', 'public, max-age=2592000, immutable')
}
// CSS 和 JS(带 hash)缓存 1 年
else if (/\.(js|css)$/.test(req.url) && req.url.includes('[hash')) {
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable')
}
// HTML 缓存 1 小时
else if (/\.html$/i.test(req.url)) {
res.setHeader('Cache-Control', 'public, max-age=3600, must-revalidate')
}
next()
}
}
}
// https://vite.dev/config/
export default defineConfig({
plugins: [
vue(),
vueDevTools(),
cacheHeaderPlugin(),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
},
},
server: {
host: '0.0.0.0',
},
build: {
// Optimize build output
minify: 'terser',
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
pure_funcs: ['console.log', 'console.info', 'console.debug'],
passes: 3,
inline: 3,
reduce_vars: true,
reduce_funcs: true,
},
mangle: {
safari10: false,
},
format: {
comments: false,
},
},
// Aggressive code splitting for better caching and parallel loading
rollupOptions: {
output: {
manualChunks: (id) => {
if (id.includes('node_modules')) {
if (id.includes('vue-i18n')) {
return 'vue-i18n'
}
if (id.includes('vue-toastification')) {
return 'vue-toast'
}
if (id.includes('tailwindcss')) {
return 'vendor'
}
return 'vendor'
}
},
// Optimize chunk names for better caching
chunkFileNames: 'assets/[name].[hash:8].js',
entryFileNames: 'assets/[name].[hash:8].js',
assetFileNames: (assetInfo) => {
const info = assetInfo.name.split('.')
const ext = info[info.length - 1]
if (/png|jpe?g|gif|svg|webp|avif/.test(ext)) {
return 'assets/[name].[hash:8][extname]'
} else if (/woff|woff2|ttf|otf|eot/.test(ext)) {
return 'assets/[name].[hash:8][extname]'
} else if (ext === 'css') {
return 'assets/[name].[hash:8][extname]'
}
return 'assets/[name].[hash:8][extname]'
},
generatedCode: {
preset: 'es2015',
arrowFunctions: true,
}
},
},
// Performance optimizations
reportCompressedSize: false,
target: ['es2020'],
cssCodeSplit: true,
sourcemap: false,
chunkSizeWarningLimit: 500,
},
})