-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
144 lines (125 loc) · 3.7 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
import { loadEnv } from 'vite'
import { resolve } from 'path'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import html from 'vite-plugin-html'
import styleImport, { AndDesignVueResolve } from 'vite-plugin-style-import'
import PurgeIcons from 'vite-plugin-purge-icons'
import { viteMockServe } from 'vite-plugin-mock'
import config, { generateModifyVars } from './src/settings'
const rootPath = process.cwd()
function pathResolve(dir) {
return resolve(rootPath, '.', dir)
}
/**
* 读取所有环境变量, 并转化为Oject形式
* @param envConf 环境对象
* @returns Object
*/
function wrapperEnv(envConf) {
const ret = {}
for (const envName of Object.keys(envConf)) {
let realName = envConf[envName].replace(/\\n/g, '\n')
realName = realName === 'true' ? true : realName === 'false' ? false : realName
if (envName === 'VITE_PORT') {
realName = Number(realName)
}
if (envName === 'VITE_PROXY' && realName) {
try {
realName = JSON.parse(realName.replace(/'/g, '"'))
} catch (error) {
realName = ''
}
}
ret[envName] = realName
if (typeof realName === 'string') {
process.env[envName] = realName
} else if (typeof realName === 'object') {
process.env[envName] = JSON.stringify(realName)
}
}
return ret
}
// 创建本地proxy
function createProxy(list) {
const httpsRE = /^https:\/\//
const ret = {}
for (const [prefix, target] of list) {
const isHttps = httpsRE.test(target)
ret[prefix] = {
target: target,
changeOrigin: true,
ws: true,
...(isHttps ? { secure: false } : {})
}
}
return ret
}
// https://vitejs.dev/config/
export default ({ command, mode }) => {
const isBuild = mode === 'production' ? true : false
const env = loadEnv(mode, rootPath)
const viteEnv = wrapperEnv(env)
const { VITE_PORT, VITE_PROXY = [], VITE_USE_MOCK } = viteEnv
return {
server: {
port: VITE_PORT,
proxy: isBuild ? null : createProxy(VITE_PROXY)
},
build: {
minify: 'terser',
terserOptions: {
compress: {
//生产环境时移除console
drop_console: true,
drop_debugger: true
}
}
},
// 别名
resolve: {
alias: [
{
find: /@\//,
replacement: pathResolve('src') + '/'
}
]
},
// 导入全局样式和修改antd的样式
css: {
preprocessorOptions: {
less: {
modifyVars: {
hack: `true; @import (reference) "${pathResolve('src/styles/index.less')}"`,
...generateModifyVars()
},
javascriptEnabled: true
}
}
},
define: {},
// 插件
plugins: [
vue(),
vueJsx(),
html({
minify: isBuild,
inject: {
data: {
title: config.title
}
}
}),
styleImport({
resolves: [AndDesignVueResolve()]
}),
PurgeIcons(),
VITE_USE_MOCK &&
viteMockServe({
ignore: /^\_/,
mockPath: 'mock',
localEnabled: !isBuild
})
]
}
}