-
Notifications
You must be signed in to change notification settings - Fork 29
/
vite.config.ts
90 lines (76 loc) · 2.05 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
import { defineConfig, loadEnv } from 'vite'
import { resolve } from 'path'
import dayjs from 'dayjs'
import { createServerOptions, createPreviewOptions } from './build/vite/server'
import { createVitePlugins } from './build/vite/plugin'
import { dependencies, devDependencies, version } from './package.json'
function pathResolve(dir: string) {
return resolve(__dirname, '.', dir)
}
// inject info
const __APP_INFO__ = {
pkg: { dependencies, devDependencies, version },
lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
}
/**
* package env type
*/
function packageEnv(mode: string, root: string): ViteEnv {
const ret: any = {}
const envConf = loadEnv(mode, root)
Object.keys(envConf).forEach((envName) => {
let conf: unknown = envConf[envName]
// format port to number
if (envName === 'VITE_PORT') {
conf = Number(conf)
}
ret[envName] = conf
})
return ret
}
// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
const root = process.cwd()
const isBuild = command === 'build'
const env = packageEnv(mode, root)
return {
plugins: createVitePlugins(env, isBuild),
base: '/',
root,
resolve: {
alias: [
// /@/xxxx => src/xxxx
{
find: /\/@\//,
replacement: pathResolve('src') + '/',
},
// /#/xxxx => types/xxxx
{
find: /\/#\//,
replacement: pathResolve('types') + '/',
},
],
},
server: createServerOptions(env),
preview: createPreviewOptions(env),
build: {
target: 'es2015',
outDir: 'dist',
assetsDir: 'static',
minify: 'terser',
terserOptions: {
compress: {
keep_infinity: true,
// Used to delete console in production environment
drop_console: mode === 'production',
},
},
// Turning off brotliSize display can slightly reduce packaging time
brotliSize: false,
chunkSizeWarningLimit: 2000,
},
define: {
__APP_INFO__: JSON.stringify(__APP_INFO__),
},
}
})