forked from buqiyuan/vite-vue3-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
186 lines (178 loc) · 5.43 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
import { resolve } from 'node:path';
import { loadEnv } from 'vite';
import vueJsx from '@vitejs/plugin-vue-jsx';
import legacy from '@vitejs/plugin-legacy';
import vue from '@vitejs/plugin-vue';
import checker from 'vite-plugin-checker';
import { viteMockServe } from 'vite-plugin-mock';
import Components from 'unplugin-vue-components/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import Unocss from 'unocss/vite';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import dayjs from 'dayjs';
import DefineOptions from 'unplugin-vue-define-options/vite';
import pkg from './package.json';
import type { UserConfig, ConfigEnv } from 'vite';
const CWD = process.cwd();
// 环境变量
// const BASE_ENV_CONFIG = loadEnv('', CWD);
// const DEV_ENV_CONFIG = loadEnv('development', CWD);
// const PROD_ENV_CONFIG = loadEnv('production', CWD);
const __APP_INFO__ = {
pkg,
lastBuildTime: dayjs().format('YYYY-MM-DD HH:mm:ss'),
};
// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfig => {
// 环境变量
const { VITE_BASE_URL, VITE_DROP_CONSOLE } = loadEnv(mode, CWD);
const isBuild = command === 'build';
return {
base: VITE_BASE_URL,
define: {
__APP_INFO__: JSON.stringify(__APP_INFO__),
},
resolve: {
alias: [
{
find: 'vue-i18n',
replacement: 'vue-i18n/dist/vue-i18n.cjs.js',
},
{
find: '@',
replacement: resolve(__dirname, './src'),
},
],
},
plugins: [
vue(),
Unocss(),
DefineOptions(), // https://github.com/sxzz/unplugin-vue-define-options
vueJsx({
// options are passed on to @vue/babel-plugin-jsx
}),
legacy({
targets: ['defaults', 'not IE 11', 'chrome 79', 'maintained node versions'],
additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
// 根据你自己需要导入相应的polyfill: https://github.com/vitejs/vite/tree/main/packages/plugin-legacy#polyfill-specifiers
modernPolyfills: ['es.promise.finally', 'es/array', 'es/map', 'es/set'],
}),
createSvgIconsPlugin({
// Specify the icon folder to be cached
iconDirs: [resolve(CWD, 'src/assets/icons')],
// Specify symbolId format
symbolId: 'svg-icon-[dir]-[name]',
}),
viteMockServe({
ignore: /^_/,
mockPath: 'mock',
localEnabled: !isBuild,
prodEnabled: isBuild,
logger: true,
injectCode: `
import { setupProdMockServer } from '../mock/_createProductionServer';
setupProdMockServer();
`,
}),
// https://github.com/antfu/unplugin-vue-components
//让 unplugin-vue-components 只有在生产环境生效
{
...Components({
resolvers: [AntDesignVueResolver()],
}),
apply: 'build',
},
// 开发环境动态加入ui库框架引入
{
name: 'dev-auto-import-antdv',
transform(code, id) {
if (/src\/main.ts$/.test(id)) {
const result = code.split('\n');
const script = `
import * as components from 'ant-design-vue/es/components';
const filters = ['AButton'];
Object.entries(components).forEach(([key, comp]) => {
if (comp.install && !filters.includes(comp.name)) {
app.use(comp);
}
});
`;
// 解决首次加载isCustomElement的问题
result.splice(result.length - 2, 0, script);
return {
code: result.join('\n'),
map: null,
};
}
},
apply: 'serve',
},
// https://github.com/fi3ework/vite-plugin-checker
checker({
typescript: true,
// vueTsc: true,
eslint: {
lintCommand: 'eslint "./src/**/*.{.vue,ts,tsx}"', // for example, lint .ts & .tsx
},
}),
],
css: {
preprocessorOptions: {
less: {
javascriptEnabled: true,
modifyVars: {},
additionalData: `
@import "ant-design-vue/lib/style/themes/default.less";
@import "@/styles/variables.less";
`,
},
// scss: {
// additionalData: `
// @use 'sass:math';
// @import "src/styles/global.scss";
// `,
// },
},
},
server: {
host: '0.0.0.0',
port: 8088,
proxy: {
'/api': {
target: 'https://nest-api.buqiyuan.site/api/',
// target: 'http://localhost:7001',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
},
'/ws-api': {
target: 'wss://nest-api.buqiyuan.site',
// target: 'http://localhost:7002',
changeOrigin: true, //是否允许跨域
ws: true,
},
},
},
optimizeDeps: {
include: [
'@vue/runtime-core',
'@vue/shared',
'lodash-es',
'ant-design-vue/es/locale/zh_CN',
'ant-design-vue/es/locale/en_US',
],
},
esbuild: {
pure: VITE_DROP_CONSOLE ? ['console.log', 'debugger'] : [],
supported: {
// https://github.com/vitejs/vite/pull/8665
'top-level-await': true,
},
},
build: {
target: 'es2017',
minify: 'esbuild',
cssTarget: 'chrome79',
chunkSizeWarningLimit: 2000,
},
};
};