-
Notifications
You must be signed in to change notification settings - Fork 33
/
vite.config.ts
200 lines (186 loc) · 5.7 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// vitejs
import type { UserConfig, ConfigEnv } from 'vite'
import { loadEnv, defineConfig } from 'vite'
import { wrapperEnv } from './build/utils'
// plugins
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { createHtmlPlugin } from 'vite-plugin-html'
import vueJsx from '@vitejs/plugin-vue-jsx'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import vueI18n from '@intlify/vite-plugin-vue-i18n'
import { visualizer } from 'rollup-plugin-visualizer'
import legacy from '@vitejs/plugin-legacy'
import { createProxy } from './build/proxy'
import { configCompressPlugin } from './build/compress'
import { configImageminPlugin } from './build/imagemin'
import { configPwaConfig } from './build/pwa'
import { viteMockServe } from 'vite-plugin-mock'
import { resolve } from 'path'
import pkg from './package.json'
import dayjs from 'dayjs'
function pathResolve(dir: string) {
return resolve(process.cwd(), '.', dir)
}
// 设置应用信息
const { dependencies, devDependencies, name, version } = pkg
const __APP_INFO__ = {
pkg: { dependencies, devDependencies, name, version },
lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss')
}
// https://vitejs.dev/config/
export default defineConfig(({ mode, command }: ConfigEnv): UserConfig => {
const root = process.cwd()
const env = loadEnv(mode, root)
// loadEnv读取的布尔类型是一个字符串。这个函数可以转换为布尔类型
const viteEnv = wrapperEnv(env)
const {
VITE_PORT,
VITE_PUBLIC_PATH,
VITE_PROXY,
VITE_DROP_CONSOLE,
VITE_HTTPS,
VITE_USE_MOCK,
VITE_GLOB_APP_TITLE,
VITE_APP_CONFIG_FILE_NAME,
VITE_BUILD_COMPRESS,
VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE,
VITE_USE_IMAGEMIN,
VITE_LEGACY
} = viteEnv
const isBuild = command === 'build'
const lifecycle = process.env.npm_lifecycle_event
const path = VITE_PUBLIC_PATH.endsWith('/') ? VITE_PUBLIC_PATH : `${VITE_PUBLIC_PATH}/`
const getAppConfigSrc = () => {
return `${path || '/'}${VITE_APP_CONFIG_FILE_NAME}?v=${pkg.version}-${new Date().getTime()}`
}
const buildPlugins = isBuild
? [
// gzip,brotli压缩输出,生产有效
configCompressPlugin(VITE_BUILD_COMPRESS, VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE),
// 图片压缩
VITE_USE_IMAGEMIN && configImageminPlugin(),
// pwd应用
configPwaConfig(viteEnv),
// 浏览器兼容
VITE_LEGACY && legacy()
]
: []
return {
base: VITE_PUBLIC_PATH,
root,
esbuild: {
pure: VITE_DROP_CONSOLE ? ['console.log', 'debugger'] : []
},
server: {
https: VITE_HTTPS,
// Listening on all local IPs
host: true,
port: VITE_PORT,
// Load proxy configuration from .env
proxy: createProxy(VITE_PROXY)
},
plugins: [
vue(),
AutoImport({
imports: ['vue', 'vue-router', '@vueuse/core', '@vueuse/head'],
resolvers: [ElementPlusResolver()],
include: [
/\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
/\.vue$/,
/\.vue\?vue/ // .vue
],
dts: true
}),
Components({
resolvers: [ElementPlusResolver(), IconsResolver()],
dts: true
}),
vueJsx(),
Icons({
autoInstall: true
}),
viteMockServe({
ignore: /^\_/,
mockPath: 'mock',
supportTs: true,
prodEnabled: VITE_USE_MOCK,
// 相当于在src/main.ts中inject下面的代码,所以注意文件的路径问题
injectCode: `
import { setupProdMockServer } from '../mock/_createProductionServer';
setupProdMockServer();
`
}),
createSvgIconsPlugin({
// 指定需要缓存的图标文件夹
iconDirs: [pathResolve('src/assets/svg')],
// 指定symbolId格式
symbolId: 'icon-[dir]-[name]'
}),
// 国际化
vueI18n({
// if you want to use Vue I18n Legacy API, you need to set `compositionOnly: false`
// compositionOnly: false,
// you need to set i18n resource including paths !
include: pathResolve('src/locales/**')
}),
// html定制化
createHtmlPlugin({
minify: isBuild,
inject: {
// Inject data into ejs template
data: {
title: VITE_GLOB_APP_TITLE
},
// Embed the generated _app.config.js file, 使用esno编译,npm run build:post
tags: isBuild
? [
{
tag: 'script',
attrs: {
src: getAppConfigSrc()
}
}
]
: []
}
}),
...buildPlugins,
// 打包分析
lifecycle === 'report' &&
visualizer({
filename: './node_modules/.cache/visualizer/stats.html',
open: true,
gzipSize: true,
brotliSize: true
})
],
json: {
stringify: true
},
define: {
__APP_INFO__: JSON.stringify(__APP_INFO__)
},
resolve: {
alias: {
'@': pathResolve('src'),
types: pathResolve('types'),
path: 'path-browserify'
// dayjs: 'dayjs/esm'
}
},
test: {
globals: true,
environment: 'happy-dom',
include: ['**/*.spec.(ts|tsx)', '**/*.test.(ts|tsx)'],
exclude: ['dist/**', 'build/**', 'mock/**', 'public', '**/node_modules/**'],
deps: {
inline: ['vue', 'vue-router', 'vuex', 'axios', 'element-plus']
}
}
}
})