ruoyi-plus-soybean/build/config/proxy.ts

39 lines
1.0 KiB
TypeScript
Raw Normal View History

import type { ProxyOptions } from 'vite';
2023-11-17 08:45:00 +08:00
import { createServiceConfig, createProxyPattern } from '../../env.config';
/**
2023-11-17 08:45:00 +08:00
* set http proxy
* @param env - the current env
*/
2023-11-17 08:45:00 +08:00
export function createViteProxy(env: Env.ImportMeta) {
const isEnableHttpProxy = env.VITE_HTTP_PROXY === 'Y';
2023-11-17 08:45:00 +08:00
if (!isEnableHttpProxy) return undefined;
const { baseURL, otherBaseURL } = createServiceConfig(env);
const defaultProxyPattern = createProxyPattern();
const proxy: Record<string, ProxyOptions> = {
[defaultProxyPattern]: {
target: baseURL,
changeOrigin: true,
2023-11-17 08:45:00 +08:00
rewrite: path => path.replace(new RegExp(`^${defaultProxyPattern}`), '')
}
};
2023-11-17 08:45:00 +08:00
const otherURLEntries = Object.entries(otherBaseURL);
for (const [key, url] of otherURLEntries) {
2023-11-20 21:32:55 +08:00
const proxyPattern = createProxyPattern(key as App.Service.OtherBaseURLKey);
2023-11-17 08:45:00 +08:00
proxy[proxyPattern] = {
target: url,
changeOrigin: true,
rewrite: path => path.replace(new RegExp(`^${proxyPattern}`), '')
};
}
return proxy;
}