proxy.ts
525 Bytes
import { ProxyOptions } from "vite"
export const createProxy = (viteEnv: ImportMetaEnv) => {
  const { VITE_GLOB_PROXY } = viteEnv 
  const httpsReg = /^https:\/\//;
  const res: Record<string, ProxyOptions> = {}
  for (const [prefix, target] of VITE_GLOB_PROXY) {
    const isHttps = httpsReg.test(prefix)
    res[prefix] = {
      target,
      changeOrigin: true,
      ws: true,
      rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
      ...(isHttps ? { secure: false } : {})
    }
  }
  return res
}