-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
124 lines (121 loc) · 3.85 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
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import mockDevServerPlugin from "vite-plugin-mock-dev-server";
import { resolve } from "path";
import UnoCSS from "unocss/vite";
import {
name,
version,
engines,
dependencies,
devDependencies,
} from "./package.json";
const APP_INFO = {
pkg: { name, version, engines, dependencies, devDependencies },
buildTimestamp: new Date().toLocaleString(),
};
const pathSrc = resolve(__dirname, "src");
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd());
return {
base: './',
resolve: {
alias: {
"@": pathSrc,
},
},
css: {
// CSS 预处理器
preprocessorOptions: {
// 定义全局 SCSS 变量
scss: {
javascriptEnabled: true,
additionalData: `@use "@/styles/variables.scss" as *;`
},
},
},
plugins: [
vue(),
env.VITE_USE_MOCK === "true" ? mockDevServerPlugin() : null,
UnoCSS({
hmrTopLevelAwait: false,
}),
AutoImport({
// 自动导入 Vue 相关函数,如:ref, reactive, toRef 等
imports: ["vue", "vue-router", "vue-i18n", "pinia", "@vueuse/core"],
resolvers: [
// 自动导入 Element Plus 相关函数,如:ElMessage, ElMessageBox... (带样式)
ElementPlusResolver(),
],
// 是否在 vue 模板中自动导入
vueTemplate: true,
// 指定自动导入函数TS类型声明文件路径 (false:关闭自动生成)
dts: false,
}),
Components({
resolvers: [
// 自动导入 Element Plus 组件
ElementPlusResolver(),
],
// 指定自定义组件位置(默认:src/components)
dirs: ["src/components", "src/**/components"],
// 指定自动导入组件TS类型声明文件路径 (false:关闭自动生成)
dts: false,
}),
createSvgIconsPlugin({
// 指定需要缓存的图标文件夹
iconDirs: [resolve(pathSrc, "assets/icons")],
// 指定symbolId格式
symbolId: "icon-[dir]-[name]",
}),
],
server: {
host: '0.0.0.0',
port: Number(env.VITE_APP_PORT),
open: true,
proxy: {
/** 代理前缀为 /mock 的请求 */
[env.VITE_APP_BASE_URL]: {
changeOrigin: true,
target: env.VITE_APP_API_URL, // 接口地址
rewrite: (path) => path.replace(new RegExp("^" + env.VITE_APP_BASE_URL), ''),
}
}
},
build: {
//target: 'es2015',
sourcemap: false,
chunkSizeWarningLimit: 2000, // 消除打包大小超过500kb警告
rollupOptions: {
output: {
entryFileNames: "script/[name].[hash].js",
chunkFileNames: "script/[name].[hash].js",
assetFileNames: (assetInfo) => {
const info = assetInfo.name.split(".");
let extType = info[info.length - 1];
if (
/\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/i.test(assetInfo.name)
) {
extType = "media";
} else if (/\.(png|jpe?g|gif|svg)(\?.*)?$/.test(assetInfo.name)) {
extType = "image";
} else if (/\.(woff2?|eot|ttf|otf)(\?.*)?$/i.test(assetInfo.name)) {
extType = "fonts";
} else if (/\.(css?|sass|scss|less)(\?.*)?$/i.test(assetInfo.name)) {
extType = "style";
}
return `${extType}/[name].[hash].[ext]`;
},
},
},
},
define: {
APP_INFO: JSON.stringify(APP_INFO),
},
}
})