-
Notifications
You must be signed in to change notification settings - Fork 73
/
vite.config.ts
115 lines (110 loc) · 3 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
import { resolve } from 'path';
import { loadEnv } from 'vite';
import vueJsx from '@vitejs/plugin-vue-jsx';
import legacy from '@vitejs/plugin-legacy';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from 'unplugin-vue-components/resolvers';
import { viteMockServe } from 'vite-plugin-mock';
import vue from '@vitejs/plugin-vue';
import checker from 'vite-plugin-checker';
import type { UserConfig, ConfigEnv } from 'vite';
const CWD = process.cwd();
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,
resolve: {
alias: [
{
find: '@',
replacement: resolve(__dirname, './src'),
},
],
},
plugins: [
vue(),
vueJsx({
// options are passed on to @vue/babel-plugin-jsx
}),
legacy({
targets: ['defaults', 'not IE 11'],
}),
Components({
resolvers: [VantResolver()],
}),
viteMockServe({
ignore: /^_/,
mockPath: 'mock',
localEnabled: !isBuild,
prodEnabled: isBuild,
logger: true,
injectCode: `
import { setupProdMockServer } from '../mock/_createProductionServer';
setupProdMockServer();
`,
}),
// 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: {
modifyVars: {},
javascriptEnabled: true,
},
scss: {
additionalData: `@import "src/styles/vw-rem/_util.scss";
@import "src/styles/vw-rem/_border.scss";
@import "src/styles/func.scss";`,
},
},
// TODO 构建包含@charset问题 https://github.com/vitejs/vite/issues/5833
// postcss: {
// plugins: [
// {
// postcssPlugin: 'internal:charset-removal',
// AtRule: {
// charset: (atRule) => {
// if (atRule.name === 'charset') {
// atRule.remove();
// }
// },
// },
// },
// ],
// },
},
server: {
port: 8888,
proxy: {
'/api': {
// 免费的在线REST API
target: 'http://jsonplaceholder.typicode.com',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
},
},
},
optimizeDeps: {
include: ['vant', '@vant/touch-emulator'],
exclude: ['vue-demi'],
},
build: {
minify: 'terser',
terserOptions: {
compress: {
keep_infinity: true,
drop_console: Object.is(VITE_DROP_CONSOLE, 'true'),
},
},
},
};
};