proxy.ts 873 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. // if(process.env.NODE_ENV == 'production'){
  17. // }else{
  18. // }
  19. const isHttps = httpsRE.test(target);
  20. // https://github.com/http-party/node-http-proxy#options
  21. ret[prefix] = {
  22. target: target,
  23. changeOrigin: true,
  24. ws: true,
  25. rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
  26. // https is require secure=false
  27. ...(isHttps ? { secure: false } : {}),
  28. };
  29. }
  30. return ret;
  31. }