vite.config.ts
3.13 KB
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
import { resolve } from 'path'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { createHtmlPlugin } from 'vite-plugin-html'
import WindiCSS from 'vite-plugin-windicss'
import VueJSX from '@vitejs/plugin-vue-jsx'
import { globalVariablePlugin } from '@wry-smile/vite-plugin-global-variable'
import legacy from '@vitejs/plugin-legacy'
import { generateModifyVars } from './build/config/generate/generateModifyVars'
import { injectEnv } from './build/config/env'
import type { GlobEnvConfig } from './types/config'
import { createProxy } from './build/config/vite/proxy'
import { wrapperEnv } from './build/config/utils'
import { OssPlugin } from './build/config/vite/ossPlugin'
function pathResolve(dir: string) {
return resolve(process.cwd(), '.', dir)
}
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
const root = process.cwd()
const env = loadEnv(mode, root) as unknown as GlobEnvConfig
const { VITE_GLOB_PUBLIC_PATH, VITE_PROXY, VITE_GLOB_SHORT_NAME, VITE_GLOB_ENABLE_OSS, VITE_GLOB_OSS_ADDRESS } = wrapperEnv(env)
return {
mode,
base: VITE_GLOB_PUBLIC_PATH,
resolve: {
alias: [
{
find: /@\//,
replacement: `${pathResolve('src')}/`,
},
{
find: /#\//,
replacement: `${pathResolve('types')}/`,
},
{
find: 'vue',
replacement: `${pathResolve('./node_modules/vue/dist/vue.esm-browser.js')}`,
},
],
},
server: {
proxy: createProxy(VITE_PROXY),
},
css: {
preprocessorOptions: {
less: {
modifyVars: generateModifyVars(),
javascriptEnabled: true,
},
},
},
plugins: [
legacy(),
vue(),
VueJSX(),
WindiCSS(),
globalVariablePlugin(),
createHtmlPlugin({
inject: {
tags: [
{
tag: 'script',
injectTo: 'head-prepend',
children: injectEnv({
variable:
{
mode,
base: VITE_GLOB_PUBLIC_PATH,
shortName: VITE_GLOB_SHORT_NAME,
enableOss: VITE_GLOB_ENABLE_OSS,
ossAddress: VITE_GLOB_OSS_ADDRESS,
},
}),
},
],
},
}),
OssPlugin({ enableOss: VITE_GLOB_ENABLE_OSS }),
],
build: {
sourcemap: true,
emptyOutDir: true,
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes('/node_modules/vue/dist/vue.esm-browser.js'))
return 'vue'
if (id.includes('ant-design-vue'))
return 'ant-design-vue'
if (id.includes('lodash-es'))
return 'lodash-es'
if (id.includes('echarts'))
return 'echarts'
if (id.includes('node_modules'))
return 'lib'
},
chunkFileNames: 'static/js/[name]-[hash].js',
entryFileNames: 'static/js/[name]-[hash].js',
assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
},
},
},
}
})