forked from shawnxiao94/fullstack-nest-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.ts
149 lines (147 loc) · 4.61 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
/*
* @Author: shawnxiao [email protected]
* @Date: 2022-11-26 22:59:57
* @LastEditors: shawnxiao [email protected]
* @LastEditTime: 2022-11-27 14:56:45
* @FilePath: \react\react18-vite3-ts-antd4\vite.config.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import { defineConfig, loadEnv, ConfigEnv, UserConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path, { resolve } from 'path'
import vitePluginImp from 'vite-plugin-imp'
import { wrapperEnv } from './src/utils/getEnv'
import { visualizer } from 'rollup-plugin-visualizer'
import { createHtmlPlugin } from 'vite-plugin-html'
import viteCompression from 'vite-plugin-compression'
import eslintPlugin from 'vite-plugin-eslint'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { viteMockServe } from 'vite-plugin-mock'
// https://vitejs.dev/config/
export default defineConfig((mode: ConfigEnv): UserConfig => {
const env = loadEnv(mode.mode, process.cwd())
const viteEnv = wrapperEnv(env)
return {
plugins: [
react(),
// antd 按需加载
vitePluginImp({
libList: [
{
libName: 'antd',
style(name) {
return `antd/es/${name}/style/index.less`
}
}
]
}),
createHtmlPlugin({
inject: {
data: {
title: viteEnv.VITE_GLOB_APP_TITLE
}
}
}),
// * 使用 svg 图标
createSvgIconsPlugin({
iconDirs: [resolve(process.cwd(), 'src/assets/icons')],
symbolId: 'icon-[dir]-[name]'
}),
// * EsLint 报错信息显示在浏览器界面上
eslintPlugin(),
// * 是否生成包预览
viteEnv.VITE_REPORT && visualizer(),
// * gzip compress
viteEnv.VITE_BUILD_GZIP &&
viteCompression({
verbose: true,
disable: false,
threshold: 10240,
algorithm: 'gzip',
ext: '.gz'
}),
viteMockServe({
mockPath: './src/mock', // 解析mock文件的位置
localEnabled: true // 是否开启开发环境
}) // add mock support
],
esbuild: {
pure: viteEnv.VITE_DROP_CONSOLE ? ['console.log', 'debugger'] : []
},
// build configure
build: {
target: 'es2015',
outDir: 'dist',
// esbuild 打包更快,但是不能去除 console.log,去除 console 使用 terser 模式
minify: 'esbuild',
// minify: "terser",
// terserOptions: {
// compress: {
// drop_console: viteEnv.VITE_DROP_CONSOLE,
// drop_debugger: true
// }
// },
rollupOptions: {
output: {
// Static resource classification and packaging
chunkFileNames: 'assets/js/[name]-[hash].js', // 引入文件名的名称
entryFileNames: 'assets/js/[name]-[hash].js', // 包的入口文件名称
assetFileNames: 'assets/[ext]/[name]-[hash].[ext]', // 资源文件像 字体,图片等
manualChunks(id) {
if (id.includes('node_modules')) {
return id.toString().split('node_modules/')[1].split('/')[0].toString()
}
}
}
}
},
resolve: {
alias: {
'~': path.resolve(__dirname, './'), // 根路径
'@': path.resolve(__dirname, 'src') // src 路径
}
},
server: {
host: '0.0.0.0', // 服务器主机名,如果允许外部访问,可设置为"0.0.0.0"
port: viteEnv.VITE_PORT,
open: viteEnv.VITE_OPEN,
cors: true,
// https: true,
// 代理跨域(mock 不需要配置,这里只是个事列)
proxy: {
'^/proxy/.*': {
target: 'http://127.0.0.1:9527', // easymock
changeOrigin: true,
rewrite: path => path.replace(/^\/proxy/, '')
},
'/uploads': {
target: 'http://localhost:3001',
changeOrigin: true
}
}
},
css: {
// global css
preprocessorOptions: {
less: {
javascriptEnabled: true, // 支持内联 JavaScript
// 方式1
// modifyVars: {
// 'primary-color': '#ff3636',
// },
// 方式2
// modifyVars: {
// hack: `true; @import "${path.resolve(__dirname, 'src/styles/var.less')}"`,
// },
additionalData: '@import "@/styles/var.less";'
}
},
modules: {
localsConvention: 'camelCase'
}
}
// optimizeDeps: {
// include: ['react', 'react-dom', 'axios']
// }
}
})