-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
99 lines (95 loc) · 2.15 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
import type { Plugin } from 'vite';
import { defineConfig } from 'vite';
import path from 'path';
import uniPlugin from '@dcloudio/vite-plugin-uni';
import AutoImport from 'unplugin-auto-import/vite';
import ProxyConfig from './src/common/request/main/proxy';
function createPages() {
return <Plugin>{
name: 'createPages',
buildStart() {
console.log('buildStart');
},
};
}
/**
* 获取teconfig中的重定向配置
* @returns
*/
function createAliasConfig() {
const value = require('./tsconfig.json').compilerOptions.paths;
const result = {};
const match = /(?:^(@?.*)[\/\\]\*$)|(?:^(@?.*[^\\\/][^@])$)/;
function _r(str: string, _default: string = '') {
const r = str.match(match);
if (!r) {
return '';
}
return r[1] || r[2] || _default;
}
for (const key in value) {
const valueKey = _r(key, '@');
const valueTarget = value[key];
let target: string[] = [];
if (!Array.isArray(valueTarget)) {
target.push(_r(valueTarget));
} else if (Array.isArray(valueTarget) && valueTarget.length > 0) {
for (const item of valueTarget) {
target.push(_r(item));
}
}
if (target.length > 0) {
result[valueKey] = target.map(item => {
return path.resolve(__dirname, item);
});
}
}
return result;
}
/**
* h5环境下根据代理配置生成对象
* @returns
*/
function createProxyConfig() {
const keys = Object.keys(ProxyConfig);
const result = keys.reduce((prev, curr) => {
prev[`/${curr}`] = {
// target: `${ProxyConfig[curr]}/`,
target: `http://127.0.0.1:7012/${curr}`, // nginx代理
// target: `http://127.0.0.1:33210/`,
changeOrigin: true,
rewrite: (path: string) => path.replace(new RegExp(`^/${curr}`), ''),
};
return prev;
}, {});
return result;
}
// https://vitejs.dev/config/
export default defineConfig({
resolve: {
alias: {
...createAliasConfig(),
},
},
server: {
proxy:
process.env.UNI_PLATFORM === 'h5'
? {
...createProxyConfig(),
}
: {},
},
build: {
rollupOptions: {
external: ['uni'],
},
},
plugins: [
createPages(),
uniPlugin(),
AutoImport({
imports: ['vue', 'vue-router', 'uni-app'],
dts: './src/@types/auto-import.d.ts',
}),
],
});