-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
140 lines (138 loc) · 4.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
/* eslint-disable @typescript-eslint/no-explicit-any */
import { resolve } from 'path' // 此处如果报错则安装 node/path依赖
import { defineConfig, loadEnv } from 'vite'
import type { ConfigEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import { viteMockServe } from 'vite-plugin-mock'
import legacy from '@vitejs/plugin-legacy'
import viteCompression from 'vite-plugin-compression'
import UnoCSS from 'unocss/vite'
const CWD = process.cwd()
// https://vitejs.dev/config/
export default defineConfig(({ mode }: ConfigEnv) => {
const { VITE_IS_DEBUG, VITE_BASE } = loadEnv(mode, CWD)
return {
base: VITE_BASE,
server: {
port: 8088,
host: '0.0.0.0',
open: false,
https: false,
cors: true,
proxy: {
'/api': {
target: 'http://10.1.50.85:8006', // 要访问的跨域的域名
changeOrigin: true,
rewrite: (path) => path.replace(/\/api/, ''),
},
},
},
plugins: [
vue(),
Components({
resolvers: [
AntDesignVueResolver({
importStyle: false, // css in js
}),
],
}),
UnoCSS(),
// 兼容性配置
legacy({
targets: ['defaults', '> 1%', 'not IE 11', 'chrome 65', 'not dead'],
additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
renderLegacyChunks: true,
modernPolyfills: [
'es.symbol',
'es.array.filter',
'es.promise',
'es.promise.finally',
'es.promise.all-settled',
'es/map',
'es/set',
'es.array.for-each',
'es.object.define-properties',
'es.object.define-property',
'es.object.get-own-property-descriptor',
'es.object.get-own-property-descriptors',
'es.object.keys',
'es.object.to-string',
'web.dom-collections.for-each',
'es.global-this',
'esnext.global-this',
'esnext.string.match-all',
],
}),
viteMockServe({
mockPath: 'mock',
enable: true,
}),
// gzip压缩 生产环境生成 .gz 文件
viteCompression({
verbose: true,
disable: false,
threshold: 10240,
algorithm: 'gzip',
ext: '.gz',
}),
],
resolve: {
alias: {
'@': resolve(__dirname, 'src'),
'~': resolve(__dirname, 'src'),
store: resolve(__dirname, 'src/store'),
types: resolve(__dirname, 'src/types'),
utils: resolve(__dirname, 'src/utils'),
router: resolve(__dirname, 'src/router'),
enum: resolve(__dirname, 'src/enum'),
api: resolve(__dirname, 'src/api'),
},
},
css: {
preprocessorOptions: {
less: {
javascriptEnabled: true,
additionalData: `
@import "@/styles/variables.less";
`,
},
},
},
esbuild: {
pure: VITE_IS_DEBUG === 'true' ? [] : ['console.log', 'debugger'],
supported: {
// https://github.com/vitejs/vite/pull/8665
'top-level-await': true,
},
},
build: {
assetsDir: 'assets', // 指定静态资源存放路径
cssCodeSplit: true, // css代码拆分,禁用则所有样式保存在一个css里面
sourcemap: VITE_IS_DEBUG === 'true',
minify: 'esbuild',
cssTarget: 'chrome65',
chunkSizeWarningLimit: 2300,
rollupOptions: {
output: {
// 最小化拆分包
manualChunks: (id: any) => {
if (id.includes('node_modules')) {
let chunkName = 'index'
if (id.includes('/.pnpm/')) {
chunkName = id.toString().split('/.pnpm/')[1].split('/')[0].toString()
} else {
chunkName = id.toString().split('node_modules/')[1].split('/')[0].toString()
}
return chunkName
}
}, // 用于从入口点创建的块的打包输出格式[name]表示文件名,[hash]表示该文件内容hash值
entryFileNames: 'js/[name].[hash].js', // 用于命名代码拆分时创建的共享块的输出命名
chunkFileNames: 'js/[name].[hash].js', // 用于输出静态资源的命名,[ext]表示文件扩展名
assetFileNames: '[ext]/[name].[hash].[ext]', // 拆分js到模块文件夹 // chunkFileNames: (chunkInfo) => { // const facadeModuleId = chunkInfo.facadeModuleId ? chunkInfo.facadeModuleId.split('/') : []; // const fileName = facadeModuleId[facadeModuleId.length - 2] || '[name]'; // return `js/${fileName}/[name].[hash].js`; // },
},
},
},
}
})