-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathvite.config.ts
136 lines (129 loc) · 4 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
/*
* @Author: lucidity99 [email protected]
* @Date: 2023-06-05 22:42:28
* @LastEditors: lucidity99 [email protected]
* @LastEditTime: 2024-05-05 16:51:07
* @FilePath: /mocha-vue3-system/vite.config.ts
* @Description:
*
*
*/
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import IconsResolver from 'unplugin-icons/resolver'
import Icons from 'unplugin-icons/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import vueJsx from '@vitejs/plugin-vue-jsx'
import path from 'path'
import dayjs from 'dayjs'
import pkg from './package.json'
import { createSvg } from './src/components/svgIcon/svg'
const { dependencies, devDependencies, name, version } = pkg
const __APP_INFO__ = {
pkg: { dependencies, devDependencies, name, version },
lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
}
export default ({ mode }) => {
return defineConfig({
base: './',
server: {
port: 9527,
proxy: {
'/api': {
target: 'https://mock.apifox.cn/m1/2700315-0-default/', // 接口的域名
secure: false, // 如果是https接口,需要配置这个参数
changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
rewrite: (path) => path.replace(/^\/api/, '/')
}
}
},
build: {
rollupOptions: {
output: {
manualChunks: {
echarts: ['echarts']
}
}
}
},
define: {
__APP_INFO__: JSON.stringify(__APP_INFO__),
__APP_NAME__: JSON.stringify('mocha-vue3-admin')
},
plugins: [
vue({
template: {
compilerOptions: {
isCustomElement: (tag) => tag === 'iconify-icon'
}
}
}),
vueJsx(),
VueSetupExtend(),
AutoImport({
resolvers: [
ElementPlusResolver(),
// Auto import icon components
// 自动导入图标组件
IconsResolver()
]
}),
Components({
resolvers: [
// Auto register icon components
// 自动注册图标组件
IconsResolver({
enabledCollections: ['ep']
}),
ElementPlusResolver({
importStyle: mode === 'development' ? false : 'sass'
})
]
}),
Icons({
autoInstall: true
}),
// 使用unplugin-vue-components按需加载样式,开发环境会导致项目异常卡顿
// 导致原因:vite会预加载style,当首次启动 vite 服务时会对 style 进行依赖预构建,,因为element-plus的按需样式会导入大量style文件,导致页面会卡住直至style构建完成
// https://github.com/antfu/unplugin-vue-components/issues/361
// 这里自定义一个vite插件,更改src/main.js内容,开发环境全局引入样式
{
name: 'import-element-plus-style',
transform(code, id) {
if (/src\/main.js$/.test(id)) {
if (mode === 'development') {
return {
code: `${code};import 'element-plus/dist/index.css';`,
map: null
}
} else {
return {
code: `${code};import 'element-plus/theme-chalk/src/message-box.scss';import 'element-plus/theme-chalk/src/message.scss';`,
map: null
}
}
}
}
},
createSvg('./src/components/svgIcon/svgIcons/')
],
resolve: {
alias: {
'~/': `${path.resolve(__dirname, 'src')}/`,
'#/': `${path.resolve(__dirname, 'types')}/`,
assets: `${path.resolve(__dirname, 'src/assets')}/`,
'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
}
},
css: {
preprocessorOptions: {
scss: {
additionalData: `@use "~/assets/css/variables.scss" as *;`
}
}
}
})
}