2022-03-13 16:47:19 +08:00
|
|
|
import type { ProxyOptions } from 'vite';
|
2024-03-02 12:22:10 +08:00
|
|
|
import { createServiceConfig } from '../../src/utils/service';
|
2022-03-13 16:47:19 +08:00
|
|
|
|
|
|
|
/**
|
2023-12-14 21:45:29 +08:00
|
|
|
* Set http proxy
|
|
|
|
*
|
|
|
|
* @param env - The current env
|
2024-03-04 14:01:27 +08:00
|
|
|
* @param isDev - Is development environment
|
2022-03-13 16:47:19 +08:00
|
|
|
*/
|
2024-03-04 13:57:47 +08:00
|
|
|
export function createViteProxy(env: Env.ImportMeta, isDev: boolean) {
|
|
|
|
const isEnableHttpProxy = isDev && env.VITE_HTTP_PROXY === 'Y';
|
2022-03-13 16:47:19 +08:00
|
|
|
|
2023-11-17 08:45:00 +08:00
|
|
|
if (!isEnableHttpProxy) return undefined;
|
|
|
|
|
2024-03-02 12:22:10 +08:00
|
|
|
const { baseURL, proxyPattern, other } = createServiceConfig(env);
|
2023-11-17 08:45:00 +08:00
|
|
|
|
2024-03-02 12:22:10 +08:00
|
|
|
const proxy: Record<string, ProxyOptions> = createProxyItem({ baseURL, proxyPattern });
|
2023-11-17 08:45:00 +08:00
|
|
|
|
2024-03-02 12:22:10 +08:00
|
|
|
other.forEach(item => {
|
|
|
|
Object.assign(proxy, createProxyItem(item));
|
|
|
|
});
|
2022-03-13 16:47:19 +08:00
|
|
|
|
2024-03-02 12:22:10 +08:00
|
|
|
return proxy;
|
|
|
|
}
|
2023-11-17 08:45:00 +08:00
|
|
|
|
2024-03-02 12:22:10 +08:00
|
|
|
function createProxyItem(item: App.Service.ServiceConfigItem) {
|
|
|
|
const proxy: Record<string, ProxyOptions> = {};
|
2023-11-17 08:45:00 +08:00
|
|
|
|
2024-03-02 12:22:10 +08:00
|
|
|
proxy[item.proxyPattern] = {
|
|
|
|
target: item.baseURL,
|
|
|
|
changeOrigin: true,
|
2024-08-16 16:33:11 +08:00
|
|
|
ws: item.ws,
|
2024-03-02 12:22:10 +08:00
|
|
|
rewrite: path => path.replace(new RegExp(`^${item.proxyPattern}`), '')
|
|
|
|
};
|
2023-11-17 08:45:00 +08:00
|
|
|
|
2022-03-13 16:47:19 +08:00
|
|
|
return proxy;
|
|
|
|
}
|