-
Notifications
You must be signed in to change notification settings - Fork 85
/
vite.config.ts
114 lines (106 loc) · 2.54 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
import { resolve } from "path";
import { defineConfig, loadEnv, PluginOption } from "vite";
import vue from "@vitejs/plugin-vue";
import eslintPlugin from "vite-plugin-eslint";
import viteCompression from "vite-plugin-compression";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import { visualizer } from "rollup-plugin-visualizer";
import { viteMockServe } from "vite-plugin-mock";
// @see: https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
const root = process.cwd();
const env = loadEnv(mode, root);
const {
VITE_APP_PORT,
VITE_APP_MOCK,
VITE_APP_REPORT,
VITE_APP_BUILD_GZIP,
VITE_APP_OPEN,
VITE_APP_API_URL_PROXY,
VITE_APP_API_URL,
} = env;
const isBuild = command === "build";
// vite 插件
const vitePlugins: (PluginOption | PluginOption[])[] = [
vue(),
// 使用 svg 图标
createSvgIconsPlugin({
iconDirs: [resolve(__dirname, "./src/assets/iconsvg")],
symbolId: "icon-[name]",
}),
// EsLint 报错信息显示在浏览器界面上
eslintPlugin(),
];
// vite-plugin-compression gzip compress
if (VITE_APP_BUILD_GZIP === "true") {
vitePlugins.push(
viteCompression({
verbose: true,
disable: false,
threshold: 10240,
algorithm: "gzip",
ext: ".gz",
}),
);
}
// rollup-plugin-visualizer 是否生成包预览(分析依赖包大小,方便做优化处理)
if (VITE_APP_REPORT === "true") {
vitePlugins.push(visualizer());
}
// vite-plugin-mock
if (VITE_APP_MOCK === "true") {
vitePlugins.push(
viteMockServe({
mockPath: "mock",
supportTs: true,
watchFiles: true,
localEnabled: !isBuild,
prodEnabled: isBuild,
logger: true,
}),
);
}
// proxy
const proxy = {};
if (!isBuild) {
// 不是生产环境
if (VITE_APP_API_URL_PROXY && VITE_APP_API_URL_PROXY !== "") {
// VITE_APP_API_URL_PROXY存在,启用;如果VITE_APP_MOCK启用且mock中有相同url,则mock优先
proxy[VITE_APP_API_URL] = {
target: VITE_APP_API_URL_PROXY,
rewrite: (path) => path.replace(VITE_APP_API_URL, ""),
changeOrigin: true,
};
}
}
return {
root,
server: {
host: "0.0.0.0",
port: Number(VITE_APP_PORT || 3001),
open: VITE_APP_OPEN === "true",
cors: true,
proxy,
},
resolve: {
alias: [
{
find: /^~/,
replacement: `${resolve(__dirname, "./node_modules")}/`,
},
{
find: /@\//,
replacement: `${resolve(__dirname, "./src")}/`,
},
],
},
plugins: vitePlugins,
css: {
preprocessorOptions: {
less: {
javascriptEnabled: true,
},
},
},
};
});