proxy.ts 885 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /**
  2. * Used to parse the .env.development proxy configuration
  3. */
  4. import type { ProxyOptions } from 'vite'
  5. type ProxyItem = [string, string]
  6. type ProxyList = ProxyItem[]
  7. type ProxyTargetList = Record<string, ProxyOptions & { rewrite: (path: string) => string }>
  8. const httpsRE = /^https:\/\//
  9. /**
  10. * Generate proxy
  11. * @param list
  12. */
  13. export function createProxy(list: ProxyList = []) {
  14. console.log('list', list)
  15. const ret: ProxyTargetList = {}
  16. for (const [prefix, target] of list) {
  17. const isHttps = httpsRE.test(target)
  18. // https://github.com/http-party/node-http-proxy#options
  19. ret[prefix] = {
  20. target: target,
  21. changeOrigin: true,
  22. ws: true,
  23. rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
  24. // https is require secure=false
  25. ...(isHttps ? { secure: false } : {})
  26. }
  27. console.log(ret, 'ret')
  28. }
  29. return ret
  30. }