index.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // 处理环境变量
  2. const warpperEnv = (envConf: Recordable): ViteEnv => {
  3. // 此处为默认值,无需修改
  4. const ret: ViteEnv = {
  5. VITE_PORT: 8848,
  6. VITE_PUBLIC_PATH: "",
  7. VITE_PROXY_DOMAIN: "",
  8. VITE_PROXY_DOMAIN_REAL: "",
  9. VITE_PROXY_USER_REAL: "",
  10. VITE_PURCHASE_REAL: "",
  11. VITE_ROUTER_HISTORY: "",
  12. VITE_CORP_ID: "",
  13. VITE_APP_ID: "",
  14. REDIRECT_BASE_URL: "",
  15. VITE_LEGACY: false,
  16. VITE_PURCHASE_URL: "",
  17. VITE_SECRET_KEY: "",
  18. WECHAT_URL: "",
  19. BACK_URL: ""
  20. };
  21. for (const envName of Object.keys(envConf)) {
  22. let realName = envConf[envName].replace(/\\n/g, "\n");
  23. realName =
  24. realName === "true" ? true : realName === "false" ? false : realName;
  25. if (envName === "VITE_PORT") {
  26. realName = Number(realName);
  27. }
  28. ret[envName] = realName;
  29. if (typeof realName === "string") {
  30. process.env[envName] = realName;
  31. } else if (typeof realName === "object") {
  32. process.env[envName] = JSON.stringify(realName);
  33. }
  34. }
  35. return ret;
  36. };
  37. // 跨域代理重写
  38. const regExps = (value: string, reg: string): string => {
  39. return value.replace(new RegExp(`^${reg}`, "g"), "");
  40. };
  41. // 环境变量
  42. const loadEnv = (): ViteEnv => {
  43. return import.meta.env;
  44. };
  45. export { warpperEnv, regExps, loadEnv };