-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
97 lines (95 loc) · 2.75 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
/*
* @Date: 2023-11-29 15:03:02
* @FilePath: /zhousan-mine-h5/vite.config.ts
*/
import proxy from './config/proxy';
import { resolve } from 'path';
import type { ConfigEnv, UserConfig } from 'vite';
import { loadEnv } from 'vite';
import { VITE_DROP_CONSOLE, VITE_BASE_PATH } from './config/constant';
import { createVitePlugins } from './config/plugins';
import { themeVariables } from './config/theme';
export default ({ command, mode }: ConfigEnv): UserConfig => {
const isBuild = command === 'build';
const root = process.cwd();
const env = loadEnv(mode, root);
return {
root: process.cwd(),
publicDir: 'public',
base: VITE_BASE_PATH,
plugins: createVitePlugins(mode, isBuild),
css: {
modules: {
generateScopedName: '[name]__[local]___[hash:base64:5]',
hashPrefix: 'prefix',
},
preprocessorOptions: {
less: {
javascriptEnabled: true,
modifyVars: themeVariables,
},
},
},
resolve: {
alias: {
'@': `${resolve(__dirname, 'src')}`,
},
// 解析package.json中的字段
mainFields: ['module', 'jsnext:main', 'jsnext'],
},
json: {
// 是否支持从 .json 文件中进行按名导入
namedExports: true,
// 若设置为 true,导入的 JSON 会被转换为 export default JSON.parse("...") 会比转译成对象字面量性能更好, 尤其是当 JSON 文件较大的时候。开启此项,则会禁用按名导入
stringify: false,
},
// 设为 false 可以避免 Vite 清屏而错过在终端中打印某些关键信息
clearScreen: false,
// 调整控制台输出的级别 'info' | 'warn' | 'error' | 'silent'
logLevel: 'info',
server: {
open: true,
host: '0.0.0.0',
},
// optimizeDeps: {
// include: ['moment/dist/locale/zh-cn'],
// exclude: ['moment/dist/locale/*'],
// },
build: {
target: 'modules',
outDir: 'build',
assetsDir: 'assets',
cssCodeSplit: true,
assetsInlineLimit: 4096,
sourcemap: false,
minify: 'terser',
chunkSizeWarningLimit: 500,
emptyOutDir: true,
manifest: false,
rollupOptions: {
output: {
manualChunks: {
react: ['react'],
'antd-mobile': ['antd-mobile'],
'react-vant': ['react-vant']
},
},
},
// 传递给 Terser 的更多 minify 选项。
terserOptions: {
compress: {
keep_infinity: true,
drop_console: VITE_DROP_CONSOLE,
},
},
},
// 全局变量替换 Record<string, string>
define: {
_GLOBAL_VARS_: JSON.stringify({
...env,
MODE: mode,
BUILD_TIME: new Date().toLocaleString(),
}),
},
};
};