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
194 lines (191 loc) · 5.46 KB
/
Copy pathvite.config.js
File metadata and controls
194 lines (191 loc) · 5.46 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
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
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' assert { type: 'json' }
export default defineConfig({
plugins: [
vue(),
tailwindcss(),
VitePWA({
registerType: 'autoUpdate',
workbox: {
globPatterns: ['**/*.{js,css,html,ico,png,svg,woff2}'],
// 使用离线回退页面,并显式忽略订阅路径
navigateFallback: '/offline.html',
navigateFallbackDenylist: [
/^\/sub\/.*/, // /sub/...
/^\/[^/]+\/[^/]+(\?.*)?$/ // Two-segment paths like /test1/work, optionally with query params
],
runtimeCaching: [
{
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: 'NetworkFirst',
options: {
cacheName: 'local-api-cache',
networkTimeoutSeconds: 8,
cacheableResponse: {
statuses: [0, 200]
},
expiration: {
maxEntries: 50,
maxAgeSeconds: 3 * 60 // 3分钟
}
}
},
{
urlPattern: ({ request }) => request.destination === 'document',
handler: 'NetworkFirst',
options: {
cacheName: 'html-cache',
networkTimeoutSeconds: 3,
cacheableResponse: {
statuses: [0, 200]
},
expiration: {
maxEntries: 20,
maxAgeSeconds: 60 // 1分钟
}
}
},
{
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',
navigateFallbackDenylist: [
/^\/sub\/.*/,
/^\/[^/]+\/[^/]+(\?.*)?$/
],
}
}),
{
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: ['vue'],
// Pinia单独打包
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'
}
}
})