proxy.ts 797 B

12345678910111213141516171819202122232425262728293031
  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 createProxy(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', list)
  15. // https://github.com/http-party/node-http-proxy#options
  16. ret[prefix] = {
  17. target: target,
  18. changeOrigin: true,
  19. ws: true,
  20. rewrite: path => path.replace(new RegExp(`^${prefix}`), ''),
  21. // https is require secure=false
  22. ...(isHttps ? { secure: false } : {})
  23. }
  24. }
  25. return ret
  26. }