vben
authored
|
1
|
import type { UserConfig, ConfigEnv } from 'vite';
|
vben
authored
|
2
|
import pkg from './package.json';
|
vben
authored
|
3
|
import dayjs from 'dayjs';
|
vben
authored
|
4
|
import { loadEnv } from 'vite';
|
vben
authored
|
5
|
import { resolve } from 'path';
|
Vben
authored
|
6
|
import { generateModifyVars } from './build/generate/generateModifyVars';
|
vben
authored
|
7
|
import { createProxy } from './build/vite/proxy';
|
vben
authored
|
8
|
import { wrapperEnv } from './build/utils';
|
vben
authored
|
9
|
import { createVitePlugins } from './build/vite/plugin';
|
vben
authored
|
10
|
import { OUTPUT_DIR } from './build/constant';
|
Vben
authored
|
11
|
|
Vben
authored
|
12
13
14
15
|
function pathResolve(dir: string) {
return resolve(process.cwd(), '.', dir);
}
|
Vben
authored
|
16
|
const { dependencies, devDependencies, name, version } = pkg;
|
Vben
authored
|
17
|
const __APP_INFO__ = {
|
Vben
authored
|
18
|
pkg: { dependencies, devDependencies, name, version },
|
vben
authored
|
19
|
lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
|
Vben
authored
|
20
|
};
|
nebv
authored
|
21
|
|
vben
authored
|
22
|
export default async ({ command, mode }: ConfigEnv): Promise<UserConfig> => {
|
vben
authored
|
23
24
|
const root = process.cwd();
|
vben
authored
|
25
|
const env = loadEnv(mode, root);
|
vben
authored
|
26
27
|
// The boolean type read by loadEnv is a string. This function can be converted to boolean type
|
vben
authored
|
28
|
const viteEnv = wrapperEnv(env);
|
vben
authored
|
29
|
|
vben
authored
|
30
|
const { VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE } = viteEnv;
|
vben
authored
|
31
|
|
vben
authored
|
32
|
const isBuild = command === 'build';
|
vben
authored
|
33
|
|
vben
authored
|
34
|
return {
|
vben
authored
|
35
|
base: VITE_PUBLIC_PATH,
|
vben
authored
|
36
|
root,
|
vben
authored
|
37
|
resolve: {
|
Vben
authored
|
38
|
alias: [
|
vben
authored
|
39
40
41
42
|
{
find: 'vue-i18n',
replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
},
|
Vben
authored
|
43
|
// /@/xxxx => src/xxxx
|
Vben
authored
|
44
45
46
47
|
{
find: /\/@\//,
replacement: pathResolve('src') + '/',
},
|
Vben
authored
|
48
|
// /#/xxxx => types/xxxx
|
Vben
authored
|
49
50
51
52
53
|
{
find: /\/#\//,
replacement: pathResolve('types') + '/',
},
],
|
vben
authored
|
54
|
},
|
vben
authored
|
55
|
server: {
|
|
56
|
host: true,
|
vben
authored
|
57
|
// Load proxy configuration from .env
|
vben
authored
|
58
59
|
proxy: createProxy(VITE_PROXY),
},
|
vben
authored
|
60
|
esbuild: {
|
|
61
|
drop: VITE_DROP_CONSOLE ? ['console', 'debugger'] : [],
|
vben
authored
|
62
|
},
|
vben
authored
|
63
|
build: {
|
vben
authored
|
64
65
|
target: 'es2015',
cssTarget: 'chrome80',
|
vben
authored
|
66
|
outDir: OUTPUT_DIR,
|
vben
authored
|
67
68
69
70
71
72
73
74
75
76
77
|
// minify: 'terser',
/**
* 当 minify=“minify:'terser'” 解开注释
* Uncomment when minify="minify:'terser'"
*/
// terserOptions: {
// compress: {
// keep_infinity: true,
// drop_console: VITE_DROP_CONSOLE,
// },
// },
|
vben
authored
|
78
|
// Turning off brotliSize display can slightly reduce packaging time
|
|
79
|
reportCompressedSize: false,
|
Vben
authored
|
80
|
chunkSizeWarningLimit: 2000,
|
vben
authored
|
81
82
|
},
define: {
|
Vben
authored
|
83
|
__APP_INFO__: JSON.stringify(__APP_INFO__),
|
vben
authored
|
84
|
},
|
vben
authored
|
85
|
|
vben
authored
|
86
87
88
|
css: {
preprocessorOptions: {
less: {
|
Vben
authored
|
89
|
modifyVars: generateModifyVars(),
|
vben
authored
|
90
|
javascriptEnabled: true,
|
vben
authored
|
91
|
},
|
vben
authored
|
92
|
},
|
vben
authored
|
93
|
},
|
vben
authored
|
94
|
|
vben
authored
|
95
|
// The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
|
vben
authored
|
96
|
plugins: await createVitePlugins(viteEnv, isBuild),
|
vben
authored
|
97
|
|
vben
authored
|
98
|
optimizeDeps: {
|
vben
authored
|
99
|
// @iconify/iconify: The dependency is dynamically and virtually loaded by @purge-icons/generated, so it needs to be specified explicitly
|
Vben
authored
|
100
101
102
103
104
|
include: [
'@iconify/iconify',
'ant-design-vue/es/locale/zh_CN',
'ant-design-vue/es/locale/en_US',
],
|
vben
authored
|
105
106
107
|
},
};
};
|