forked from Alessandro-Pang/short-link
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
147 lines (144 loc) · 3.87 KB
/
Copy pathvite.config.ts
File metadata and controls
147 lines (144 loc) · 3.87 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
/*
* @Author: zi.yang
* @Date: 2025-06-09 19:48:21
* @LastEditors: zi.yang
* @LastEditTime: 2025-12-29 00:00:00
* @Description:
* @FilePath: /short-link/vite.config.ts
*/
import { fileURLToPath, URL } from "node:url";
import tailwindcss from "@tailwindcss/vite";
import vue from "@vitejs/plugin-vue";
import { codeInspectorPlugin } from "code-inspector-plugin";
import { visualizer } from "rollup-plugin-visualizer";
import AutoImport from "unplugin-auto-import/vite";
import { ArcoResolver } from "unplugin-vue-components/resolvers";
import Components from "unplugin-vue-components/vite";
import { defineConfig, loadEnv, type UserConfig } from "vite";
import { compression } from "vite-plugin-compression2";
// https://vitejs.dev/config/
export default defineConfig(({ mode }): UserConfig => {
const env = loadEnv(mode, process.cwd(), "");
const isProduction = mode === "production";
const isAnalyze = process.env.ANALYZE === "true";
return {
plugins: [
vue(),
tailwindcss(),
AutoImport({
resolvers: [ArcoResolver()],
}),
Components({
resolvers: [
ArcoResolver({
sideEffect: false,
}),
],
}),
codeInspectorPlugin({
bundler: "vite",
}),
// Gzip 压缩
isProduction &&
compression({
algorithms: ["gzip", "brotliCompress"],
exclude: [/\.(br)$/, /\.(gz)$/],
threshold: 1024, // 只压缩大于 1KB 的文件
}),
// 打包分析 (运行 ANALYZE=true pnpm build 查看)
isAnalyze &&
visualizer({
open: true,
filename: "dist/stats.html",
gzipSize: true,
brotliSize: true,
}),
].filter(Boolean),
resolve: {
alias: {
"@": fileURLToPath(new URL("./src", import.meta.url)),
},
},
build: {
outDir: "dist",
emptyOutDir: true,
// 启用 CSS 代码分割
cssCodeSplit: true,
// 设置 chunk 大小警告阈值 (500KB)
chunkSizeWarningLimit: 500,
// 生产环境移除 console 和 debugger
minify: "esbuild",
target: "es2020",
rollupOptions: {
output: {
// 手动分割代码块
manualChunks: (id) => {
// Vue 核心库
if (
id.includes("node_modules/vue") ||
id.includes("node_modules/@vue") ||
id.includes("node_modules/vue-router") ||
id.includes("node_modules/pinia")
) {
return "vue-vendor";
}
// Arco Design UI 组件库
if (id.includes("node_modules/@arco-design")) {
return "arco-vendor";
}
// 工具库
if (
id.includes("node_modules/dayjs") ||
id.includes("node_modules/nanoid") ||
id.includes("node_modules/qrcode")
) {
return "utils";
}
},
// 优化 chunk 文件名
chunkFileNames: "assets/js/[name]-[hash].js",
// 入口文件名
entryFileNames: "assets/js/[name]-[hash].js",
// 静态资源文件名
assetFileNames: (assetInfo) => {
const name = assetInfo.name || "";
// CSS 文件
if (name.endsWith(".css")) {
return "assets/css/[name]-[hash][extname]";
}
// 图片文件
if (/\.(png|jpe?g|gif|svg|webp|ico)$/i.test(name)) {
return "assets/images/[name]-[hash][extname]";
}
// 字体文件
if (/\.(woff2?|eot|ttf|otf)$/i.test(name)) {
return "assets/fonts/[name]-[hash][extname]";
}
// 其他资源
return "assets/[name]-[hash][extname]";
},
},
},
},
// 依赖预构建优化
optimizeDeps: {
include: ["vue", "vue-router", "pinia", "@arco-design/web-vue", "dayjs", "qrcode"],
},
server: {
proxy: {
"/api": {
target: `http://localhost:${env.DEV_SERVER_PORT || 3000}`,
changeOrigin: true,
},
"/u": {
target: `http://localhost:${env.DEV_SERVER_PORT || 3000}`,
changeOrigin: true,
},
},
},
define: {
__SUPABASE_URL__: JSON.stringify(env.SUPABASE_URL),
__SUPABASE_ANON_KEY__: JSON.stringify(env.SUPABASE_ANON_KEY),
},
};
});