proxy.ts 741 B

1234567891011121314151617181920212223242526272829
  1. import type { ProxyOptions } from 'vite'
  2. type ProxyItem = [string, string]
  3. type ProxyList = ProxyItem[]
  4. type ProxyTargetList = Record<string, ProxyOptions>
  5. /**
  6. * 创建代理,用于解析 .env.development 代理配置
  7. * @param list
  8. */
  9. export function initProxy(list: ProxyList = []) {
  10. const ret: ProxyTargetList = {}
  11. for (const [prefix, target] of list) {
  12. const httpsRE = /^https:\/\//
  13. const isHttps = httpsRE.test(target)
  14. console.log('代理配置', list)
  15. ret[prefix] = {
  16. target: target,
  17. changeOrigin: true,
  18. ws: true,
  19. rewrite: path => path.replace(new RegExp(`^${prefix}`), ''),
  20. // https is require secure=false
  21. ...(isHttps ? { secure: false } : {})
  22. }
  23. }
  24. return ret
  25. }