refactor(projects): refactor service env config

This commit is contained in:
Soybean
2024-03-02 12:22:10 +08:00
parent bccd6cb3c3
commit 43193e2808
13 changed files with 145 additions and 574 deletions

View File

@ -1,5 +1,5 @@
import type { ProxyOptions } from 'vite';
import { createProxyPattern, createServiceConfig } from '../../env.config';
import { createServiceConfig } from '../../src/utils/service';
/**
* Set http proxy
@ -11,29 +11,25 @@ export function createViteProxy(env: Env.ImportMeta) {
if (!isEnableHttpProxy) return undefined;
const { baseURL, otherBaseURL } = createServiceConfig(env);
const { baseURL, proxyPattern, other } = createServiceConfig(env);
const defaultProxyPattern = createProxyPattern();
const proxy: Record<string, ProxyOptions> = createProxyItem({ baseURL, proxyPattern });
const proxy: Record<string, ProxyOptions> = {
[defaultProxyPattern]: {
target: baseURL,
changeOrigin: true,
rewrite: path => path.replace(new RegExp(`^${defaultProxyPattern}`), '')
}
};
const otherURLEntries = Object.entries(otherBaseURL);
for (const [key, url] of otherURLEntries) {
const proxyPattern = createProxyPattern(key as App.Service.OtherBaseURLKey);
proxy[proxyPattern] = {
target: url,
changeOrigin: true,
rewrite: path => path.replace(new RegExp(`^${proxyPattern}`), '')
};
}
other.forEach(item => {
Object.assign(proxy, createProxyItem(item));
});
return proxy;
}
function createProxyItem(item: App.Service.ServiceConfigItem) {
const proxy: Record<string, ProxyOptions> = {};
proxy[item.proxyPattern] = {
target: item.baseURL,
changeOrigin: true,
rewrite: path => path.replace(new RegExp(`^${item.proxyPattern}`), '')
};
return proxy;
}