proxy.ts 834 B

1234567891011121314151617181920212223242526272829303132333435
  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>;
  8. const httpsRE = /^https:\/\//;
  9. /**
  10. * Generate proxy
  11. * @param list
  12. */
  13. export function createProxy(list: ProxyList = []) {
  14. const ret: ProxyTargetList = {};
  15. for (const [prefix, target] of list) {
  16. const isHttps = httpsRE.test(target);
  17. // https://github.com/http-party/node-http-proxy#options
  18. console.log('prefix', prefix);
  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. }
  28. return ret;
  29. }