proxy.ts 550 B

1234567891011121314151617181920
  1. import type { ProxyOptions } from 'vite';
  2. /**
  3. * 设置网络代理
  4. * @param isOpenProxy - 是否开启代理
  5. * @param envConfig - env环境配置
  6. */
  7. export function createViteProxy(isOpenProxy: boolean, envConfig: ServiceEnvConfigWithProxyPattern) {
  8. if (!isOpenProxy) return undefined;
  9. const proxy: Record<string, string | ProxyOptions> = {
  10. [envConfig.proxyPattern]: {
  11. target: envConfig.url,
  12. changeOrigin: true,
  13. rewrite: path => path.replace(new RegExp(`^${envConfig.proxyPattern}`), '')
  14. }
  15. };
  16. return proxy;
  17. }