-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathvite.config.ts
241 lines (234 loc) · 7.33 KB
/
vite.config.ts
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
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { VitePWA } from 'vite-plugin-pwa'
import license from 'rollup-plugin-license'
import legacy from '@vitejs/plugin-legacy'
import htmlMinifier from 'vite-plugin-html-minifier'
import pkg from './package.json'
import fs from 'fs'
import crypto from 'crypto'
import { execSync } from 'child_process'
import { defaultAppUrl, origDesc, origTitle } from './src/constants/htmlVars'
import genManifestAndIcons from './tools/manifest'
import path from 'path'
const isDev = process.env.NODE_ENV === 'development'
const commitTimeDateObj = new Date(
parseInt(
execSync('git log -1 --date=unix --format="%ad"').toString().trim(),
) * 1000,
)
const commitTime = commitTimeDateObj.toUTCString()
const commitTime2 = commitTimeDateObj.toISOString().replace(/\.\d+Z$/, '+00:00')
const ogimageHash = (() => {
const fileBuffer = fs.readFileSync('./assets/logo/ogimage.jpg')
const hashSum = crypto.createHash('sha1')
hashSum.update(fileBuffer)
return hashSum.digest('hex').substring(0, 20)
})()
const ReactCompilerConfig = {
target: '19',
}
const homeUrl = process.env.APP_URL || defaultAppUrl
let isServe = false
export default defineConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
'@assets': path.resolve(__dirname, 'assets'),
'@root': path.resolve(__dirname),
},
},
define: {
'import.meta.env.APP_VERSION': JSON.stringify(pkg.version),
'import.meta.env.APP_TITLE': JSON.stringify(origTitle),
'import.meta.env.APP_URL': JSON.stringify(homeUrl),
'import.meta.env.APP_FAVICONSVG': JSON.stringify(
isDev ? './favicon.svg' : `${homeUrl}favicon.svg`,
),
'import.meta.env.APP_FAVICONICO': JSON.stringify(
isDev ? './favicon.ico' : `${homeUrl}favicon.ico`,
),
'import.meta.env.APP_OGIMAGE': JSON.stringify(
isDev ? './ogimage.jpg' : `${homeUrl}ogimage.jpg?${ogimageHash}`,
),
'import.meta.env.APP_DESCRIPTION': JSON.stringify(origDesc),
'import.meta.env.APP_COMMITTIME': JSON.stringify(commitTime),
'import.meta.env.APP_COMMITTIME2': JSON.stringify(commitTime2),
},
plugins: [
{
name: 'vite-plugin-run-script',
configResolved(config) {
const script = 'bun tools/sass-var-gen'
console.log(
`[vite-plugin-run-script] Running (configResolved) ${script}`,
)
execSync(script, { stdio: 'inherit' })
if (config.command === 'serve') {
isServe = true
} else {
const script = 'bun tools/mangle-action-type/mangle.ts'
console.log(
`[vite-plugin-run-script] Running (configResolved, !isServe) ${script}`,
)
execSync(script, {
stdio: 'inherit',
})
}
},
buildEnd() {
if (isServe) {
// console.log(`[vite-plugin-run-script] Running (buildEnd, isServe)`)
// execSync('', { stdio: 'inherit' })
} else {
const script = 'bun tools/mangle-action-type/restore.ts'
console.log(
`[vite-plugin-run-script] Running (buildEnd, !isServe) ${script}`,
)
execSync(script, {
stdio: 'inherit',
})
}
},
},
react({
babel: {
plugins: [['babel-plugin-react-compiler', ReactCompilerConfig]],
},
}),
VitePWA({
manifest: genManifestAndIcons(),
registerType: 'autoUpdate',
includeAssets: [
'**/*.{js,css,html,ico,png,svg,jpg,jpeg,webp,mp3,woff,woff2,ttf,otf}',
],
workbox: {
globPatterns: [
'**/*.{js,css,html,ico,png,svg,jpg,jpeg,webp,mp3,woff,woff2,ttf,otf}',
],
globIgnores: ['**/sw.js', '**/workbox-*.js'],
runtimeCaching: [
{
urlPattern: ({ request }) => request.destination === 'document',
handler: 'NetworkFirst', // Always check for updates, but cache for offline
options: {
cacheName: 'html-cache',
networkTimeoutSeconds: 5, // If the network is slow, use cached version
expiration: {
maxEntries: 20,
maxAgeSeconds: 60 * 60 * 24 * 7, // 7 days
},
},
},
{
urlPattern: ({ request }) =>
['script', 'style', 'worker'].includes(request.destination),
handler: 'StaleWhileRevalidate', // Load from cache, but update in background
options: {
cacheName: 'assets-cache',
expiration: {
maxEntries: 300,
maxAgeSeconds: 60 * 60 * 24 * 30, // 30 days
},
},
},
{
urlPattern: ({ request }) => request.destination === 'image',
handler: 'CacheFirst', // Always use cache unless expired
options: {
cacheName: 'image-cache',
expiration: {
maxEntries: 500,
maxAgeSeconds: 60 * 60 * 24 * 60, // 60 days
},
},
},
{
urlPattern: ({ request }) => request.destination === 'audio',
handler: 'CacheFirst', // Load from cache first for instant playback
options: {
cacheName: 'audio-cache',
expiration: {
maxEntries: 50,
maxAgeSeconds: 60 * 60 * 24 * 60, // 60 days
},
},
},
// {
// urlPattern: ({ url }) => url.pathname.startsWith('/api/'),
// handler: 'NetworkFirst', // Use fresh data when possible, but cache offline
// options: {
// cacheName: 'api-cache',
// networkTimeoutSeconds: 3,
// expiration: {
// maxEntries: 300,
// maxAgeSeconds: 60 * 60 * 24, // 1 day
// },
// },
// },
],
maximumFileSizeToCacheInBytes: 100000000, // 100 MB
cleanupOutdatedCaches: true,
clientsClaim: true,
skipWaiting: true,
},
}),
license({
thirdParty: {
output: 'dist/LICENSES.txt',
},
}),
legacy({
modernPolyfills: true,
renderLegacyChunks: false,
modernTargets: pkg.browserslist.production,
}),
htmlMinifier({
minify: {
collapseWhitespace: true,
removeComments: true,
removeRedundantAttributes: true,
removeEmptyAttributes: true,
minifyJS: true,
minifyCSS: true,
},
}),
],
server: {
port: 8080,
open: true,
},
build: {
target: 'es2017',
outDir: 'dist',
minify: 'terser',
assetsInlineLimit: 0,
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true,
},
},
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes('node_modules')) {
const vendor = id.split('node_modules/')[1].split('/')[0]
if (
vendor === 'peerjs' ||
vendor === 'peerjs-js-binarypack' ||
vendor === 'webrtc-adapter' ||
vendor === 'sdp'
) {
return
}
if (vendor === 'react-dom' || vendor === 'react') {
return vendor
}
return 'vendor'
}
},
},
},
},
})