-
Notifications
You must be signed in to change notification settings - Fork 22
/
vite.config.js
227 lines (220 loc) · 6.27 KB
/
vite.config.js
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
import { fileURLToPath, URL } from 'node:url';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import VueDevTools from 'vite-plugin-vue-devtools';
import { VitePWA } from 'vite-plugin-pwa';
import viteImagemin from 'vite-plugin-imagemin';
import fs from 'fs';
import path from 'path';
import sharp from 'sharp';
// Get the directory name in ESM
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Recursive function to get all image files in a directory and its subdirectories
const getAllImageFiles = (dir) => {
let results = [];
const list = fs.readdirSync(dir);
list.forEach((file) => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat && stat.isDirectory()) {
results = results.concat(getAllImageFiles(filePath));
} else {
results.push(filePath);
}
});
return results;
};
// Function to generate icons array from images directory
const generateIcons = async () => {
const imagesDir = path.resolve(__dirname, 'public/images');
const files = getAllImageFiles(imagesDir);
const icons = await Promise.all(files.map(async (file) => {
try {
const metadata = await sharp(file).metadata();
const { width, height, format } = metadata;
if (width === height && format === 'png') {
const icon = {
src: path.relative(path.resolve(__dirname, 'public'), file).replace(/\\/g, '/'),
sizes: `${width}x${height}`,
type: 'image/png'
};
if (file.includes('maskable')) {
icon.purpose = 'maskable';
}
return icon;
}
} catch (error) {
console.error(`Error processing file ${file}:`, error);
}
}));
return icons.filter(Boolean);
};
const formatTagScreenshots = [
{
"src": "images/wide-minimal-chat.png",
"sizes": "3832x2395",
"type": "image/png",
"form_factor": "wide",
"label": "Desktop Homescreen of MinimalChat"
},
{
"src": "images/narrow-minimal-chat.png",
"sizes": "860x1864",
"type": "image/png",
"form_factor": "narrow",
"label": "Mobile Homescreen of MinimalChat"
}
];
// https://vitejs.dev/config/
export default defineConfig(async () => ({
plugins: [
vue(),
viteImagemin({
gifsicle: {
optimizationLevel: 7,
interlaced: false,
},
optipng: {
optimizationLevel: 7,
},
mozjpeg: {
quality: 20,
},
pngquant: {
quality: [0.65, 0.9],
speed: 4,
},
svgo: {
plugins: [
{
name: 'removeViewBox',
},
{
name: 'removeEmptyAttrs',
active: false,
},
],
},
}),
VueDevTools(),
VitePWA({
registerType: "autoUpdate",
injectRegister: "auto",
workbox: {
maximumFileSizeToCacheInBytes: 8000000,
runtimeCaching: [
{
urlPattern: /\.(?:png|jpg|jpeg|svg|gif|webp)$/,
handler: 'CacheFirst',
options: {
cacheName: 'images',
expiration: {
maxEntries: 150,
maxAgeSeconds: 30 * 24 * 60 * 60, // 30 days
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
{
urlPattern: /\.(?:woff|woff2|eot|ttf|otf)$/,
handler: 'CacheFirst',
options: {
cacheName: 'fonts',
expiration: {
maxEntries: 50,
maxAgeSeconds: 365 * 24 * 60 * 60, // 1 year
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
{
urlPattern: /^https:\/\/fonts\.googleapis\.com/,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'google-fonts-stylesheets',
},
},
{
urlPattern: /^https:\/\/fonts\.gstatic\.com/,
handler: 'CacheFirst',
options: {
cacheName: 'google-fonts-webfonts',
expiration: {
maxEntries: 100,
maxAgeSeconds: 365 * 24 * 60 * 60, // 1 year
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
{
urlPattern: /^https:\/\/api\.yourdomain\.com\/.*\.(json|xml)$/,
handler: 'NetworkFirst',
options: {
cacheName: 'api-responses',
networkTimeoutSeconds: 10,
expiration: {
maxEntries: 50,
maxAgeSeconds: 24 * 60 * 60, // 1 day
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
],
},
manifest: {
name: 'MinimalChat',
short_name: 'MinimalChat',
description: 'A lightweight yet powerful LLM chat application',
theme_color: '#202124',
background_color: "#202124",
icons: await generateIcons(),
screenshots: formatTagScreenshots,
edge_side_panel: {
"preferred_width": 600
}
}
})
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
}
},
build: {
minify: 'terser', // Use Terser for more advanced minification
terserOptions: {
compress: {
drop_console: true, // Remove console logs for smaller bundle size
drop_debugger: true, // Remove debugger statements
ecma: 2020, // Use modern ECMAScript features
module: true,
toplevel: true,
passes: 10, // Multiple passes for better compression
},
format: {
comments: false, // Remove comments
},
},
target: 'esnext', // Target modern browsers for smaller bundle size
cssCodeSplit: true, // Enable CSS code splitting
sourcemap: false, // Disable source maps for production build
chunkSizeWarningLimit: 500, // Increase chunk size warning limit
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes('node_modules')) {
return id.toString().split('node_modules/')[1].split('/')[0].toString();
}
},
},
},
},
}));