refactor(build): 简化代理配置并更新HTTP错误处理重构代理配置,提取到proxy.ts中以简化vite.config.ts中的代码。同时,修改了faultTolerance.ts中的HTTP错误处理,使用具体的ResultEnum替代类型导入,以提高代码的准确性和可读性。

This commit is contained in:
h_mo 2024-08-20 15:39:19 +08:00
parent 5a705f9a44
commit 79240a29f8
4 changed files with 25 additions and 16 deletions

3
build/index.ts Normal file
View File

@ -0,0 +1,3 @@
export * from './platform';
export * from './proxy';
export * from './vitePlugins';

19
build/proxy.ts Normal file
View File

@ -0,0 +1,19 @@
/**
* Configure according to the proxy list
* @param proxyList
*/
export function resolveProxy(proxyList: [string, string][] = []) {
const proxy: Record<string, ProxyOptions> = {};
for (const [prefix, target] of proxyList) {
const isHttps = /^https:\/\//.test(target);
proxy[prefix] = {
target,
changeOrigin: true,
ws: true,
rewrite: path => path.replace(new RegExp(`^${prefix}`), ''),
// https is require secure=false
...(isHttps ? { secure: false } : {}),
};
}
return proxy;
}

View File

@ -1,5 +1,5 @@
import { Toast } from '@/utils/uniapi/prompt';
import type { ResultEnum } from '@/enums/httpEnum';
import { ResultEnum } from '@/enums/httpEnum';
/**
* Http错误处理

View File

@ -9,15 +9,13 @@ import type { UserConfig } from 'vite';
import { defineConfig, loadEnv } from 'vite';
import TransformPages from 'uni-read-pages-vite';
import postcssPlugins from './postcss.config';
import { createVitePlugins } from './build/vitePlugins';
import { currentPlatform } from './build/platform';
import { createVitePlugins, currentPlatform, resolveProxy } from './build';
export default defineConfig(async ({ mode }) => {
const root = process.cwd();
const env = loadEnv(mode, resolve(root, 'env'));
const isProd = mode === 'production';
const { VITE_PROXY_PREFIX, VITE_UPLOAD_PROXY_PREFIX, VITE_BASE_URL, VITE_UPLOAD_URL, VITE_PORT } = env;
return {
base: './',
envDir: './env', // 自定义env目录
@ -50,18 +48,7 @@ export default defineConfig(async ({ mode }) => {
host: true,
// open: true,
port: Number.parseInt(VITE_PORT!, 10),
proxy: {
[VITE_PROXY_PREFIX!]: {
target: VITE_BASE_URL,
changeOrigin: true,
rewrite: (path: string) => path.replace(new RegExp(`^${VITE_PROXY_PREFIX}`), ''),
},
[VITE_UPLOAD_PROXY_PREFIX!]: {
target: VITE_UPLOAD_URL,
changeOrigin: true,
rewrite: (path: string) => path.replace(new RegExp(`^${VITE_UPLOAD_PROXY_PREFIX}`), ''),
},
},
proxy: resolveProxy([[VITE_PROXY_PREFIX, VITE_BASE_URL], [VITE_UPLOAD_PROXY_PREFIX, VITE_UPLOAD_URL]]),
},
// 构建配置
build: {