forked from imzyb/MiSub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
163 lines (160 loc) · 4.57 KB
/
vite.config.js
File metadata and controls
163 lines (160 loc) · 4.57 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
158
159
160
161
162
163
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { VitePWA } from 'vite-plugin-pwa'
import tailwindcss from '@tailwindcss/vite'
import manifest from './public/manifest.json' with { type: 'json' }
export default defineConfig({
plugins: [
vue(),
tailwindcss(),
VitePWA({
registerType: 'autoUpdate',
workbox: {
globPatterns: ['**/*.{js,css,html,ico,png,svg,woff2}'],
// SPA 刷新统一交给服务端的 `functions/[[path]].js` 处理,避免 Service Worker 持有旧版 `index.html`
navigateFallbackDenylist: [/^\/.*$/],
runtimeCaching: [
{
urlPattern: /^\/cdn-cgi\/.*/,
handler: 'NetworkOnly',
},
{
urlPattern: /^https:\/\/api\..*/i,
handler: 'NetworkFirst',
options: {
cacheName: 'api-cache',
networkTimeoutSeconds: 10,
cacheableResponse: {
statuses: [0, 200]
},
expiration: {
maxEntries: 100,
maxAgeSeconds: 5 * 60 // 5分钟
}
}
},
{
urlPattern: /\/api\//,
handler: 'NetworkOnly'
},
{
urlPattern: /.*\.(js|css)$/,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'static-cache',
expiration: {
maxEntries: 200,
maxAgeSeconds: 24 * 60 * 60 // 24小时
}
}
},
{
urlPattern: /\.(png|jpg|jpeg|svg|webp)$/,
handler: 'CacheFirst',
options: {
cacheName: 'image-cache',
expiration: {
maxEntries: 100,
maxAgeSeconds: 7 * 24 * 60 * 60 // 7天
}
}
},
{
urlPattern: /\.(woff|woff2|eot|ttf|otf)$/,
handler: 'CacheFirst',
options: {
cacheName: 'font-cache',
expiration: {
maxEntries: 20,
maxAgeSeconds: 30 * 24 * 60 * 60 // 30天
}
}
}
]
},
includeAssets: ['favicon.ico', 'robots.txt', 'icons/*.png', 'offline.html'],
manifest,
devOptions: {
enabled: true,
type: 'module'
}
}),
{
name: 'html-transform-rocket-loader',
transformIndexHtml(html) {
// 自动为所有 module script 添加 data-cfasync="false" 以防止 Cloudflare Rocket Loader 破坏加载
return html.replace(/<script type="module"/g, '<script type="module" data-cfasync="false"');
}
}
],
// 性能优化构建配置
build: {
// 启用CSS代码分割
cssCodeSplit: true,
// 优化依赖预构建
commonjsOptions: {
include: [/node_modules/]
},
rollupOptions: {
output: {
// 手动代码分割(保守策略,避免循环依赖导致空白页)
manualChunks: {
vue: ['vue'],
router: ['vue-router'],
pinia: ['pinia']
},
// 优化文件名
chunkFileNames: 'assets/js/[name]-[hash].js',
entryFileNames: 'assets/js/[name]-[hash].js',
assetFileNames: (assetInfo) => {
const info = assetInfo.name.split('.')
const ext = info[info.length - 1]
if (/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i.test(assetInfo.name)) {
return `assets/media/[name]-[hash][extname]`
}
if (/\.(png|jpe?g|gif|svg)(\?.*)?$/i.test(assetInfo.name)) {
return `assets/img/[name]-[hash][extname]`
}
if (/\.(woff2?|eot|ttf|otf)(\?.*)?$/i.test(assetInfo.name)) {
return `assets/fonts/[name]-[hash][extname]`
}
return `assets/${ext}/[name]-[hash][extname]`
}
}
},
// 压缩配置
minify: 'terser',
// terserOptions removed for debugging
},
// 开发服务器配置
server: {
proxy: {
'/api': {
target: 'http://127.0.0.1:8787',
changeOrigin: true,
},
'/sub/': {
target: 'http://127.0.0.1:8787',
changeOrigin: true,
},
'^/(?!@|api/|sub/|assets/|@vite/|src/|icons/|images/)[^/]+/[^/]+$': {
target: 'http://127.0.0.1:8787',
changeOrigin: true,
},
// Catch-all proxy removed to fix SPA fallback
}
},
// 依赖优化
optimizeDeps: {
include: [
'vue',
'pinia'
]
},
// 路径解析
resolve: {
alias: {
'@': '/src'
}
}
})