Blame view

build/vite/proxy.ts 799 Bytes
vben authored
1
2
3
/**
 * Used to parse the .env.development proxy configuration
 */
Vben authored
4
import type { ProxyOptions } from 'vite';
vben authored
5
陈文彬 authored
6
7
8
9
type ProxyItem = [string, string];

type ProxyList = ProxyItem[];
miofly authored
10
type ProxyTargetList = Record<string, ProxyOptions>;
11
12

const httpsRE = /^https:\/\//;
vben authored
13
14
15
16
17

/**
 * Generate proxy
 * @param list
 */
nebv authored
18
export function createProxy(list: ProxyList = []) {
19
  const ret: ProxyTargetList = {};
陈文彬 authored
20
  for (const [prefix, target] of list) {
21
    const isHttps = httpsRE.test(target);
vben authored
22
vben authored
23
    // https://github.com/http-party/node-http-proxy#options
陈文彬 authored
24
25
26
    ret[prefix] = {
      target: target,
      changeOrigin: true,
vben authored
27
      ws: true,
28
      rewrite: (path) => path.replace(new RegExp(`^${prefix}`), ''),
vben authored
29
      // https is require secure=false
vben authored
30
      ...(isHttps ? { secure: false } : {}),
陈文彬 authored
31
32
33
34
    };
  }
  return ret;
}