This repository has been archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
78 lines (66 loc) · 1.89 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
import type { UserConfig, ConfigEnv } from 'vite'
import { loadEnv } from 'vite'
import { resolve } from 'path'
import { wrapperEnv } from './build/utils'
import { createProxy } from './build/vite/proxy'
import { createVitePlugins } from './build/vite/plugin'
import { OUTPUT_DIR } from './build/constant'
// https://cn.vitejs.dev/config/#config-file
export default ({ command, mode }: ConfigEnv): UserConfig => {
const root = process.cwd()
// 加载对应模式的环境变量
const env = loadEnv(mode, root)
// 解析环境配置的类型,如 'true' 处理为 true
const viteEnv = wrapperEnv(env)
// 解构环境变量
const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE } = viteEnv
// 是否打包模式
const isBuild = command === 'build'
return {
// 项目根目录
root,
// 开发或生产环境服务的公共基础路径
base: VITE_PUBLIC_PATH,
// 资源解析选项
resolve: {
alias: [
// @/xxxx => src/xxxx
{
find: /@\//,
replacement: `${resolve(root, 'src')}/`,
},
{
// #/xxxx => types/xxxx
find: /#\//,
replacement: `${resolve(root, 'types')}/`,
},
],
},
// 开发服务器选项
server: {
host: true,
https: true,
port: VITE_PORT,
proxy: createProxy(VITE_PROXY),
},
// 构建选项
build: {
target: 'es2015',
outDir: OUTPUT_DIR,
// minify: 'terser',
// terserOptions: {
// compress: {
// keep_infinity: true,
// drop_console: VITE_DROP_CONSOLE,
// },
// },
chunkSizeWarningLimit: 2000,
},
// 继承 esbuild 转换选项
esbuild: {
pure: VITE_DROP_CONSOLE ? ['console.log', 'debugger'] : [],
},
// 插件会很多单独提取出来便于管理
plugins: createVitePlugins(viteEnv, isBuild),
}
}