env.ts 2.0 KB
Newer Older
V
Vben 已提交
1
import type { GlobEnvConfig } from '/#/config';
2

3
import { warn } from '/@/utils/log';
V
Vben 已提交
4
import pkg from '../../package.json';
5
import { getConfigFileName } from '../../build/getConfigFileName';
V
Vben 已提交
6

7
export function getCommonStoragePrefix() {
8 9
  const { VITE_GLOB_APP_SHORT_NAME } = getAppEnvConfig();
  return `${VITE_GLOB_APP_SHORT_NAME}__${getEnv()}`.toUpperCase();
V
vben 已提交
10
}
11

V
Vben 已提交
12 13
// Generate cache key according to version
export function getStorageShortName() {
14 15 16 17 18 19
  return `${getCommonStoragePrefix()}${`__${pkg.version}`}__`.toUpperCase();
}

export function getAppEnvConfig() {
  const ENV_NAME = getConfigFileName(import.meta.env);

V
Vben 已提交
20
  const ENV = (import.meta.env.DEV
21
    ? // Get the global configuration (the configuration will be extracted independently when packaging)
V
Vben 已提交
22 23
      (import.meta.env as unknown as GlobEnvConfig)
    : window[ENV_NAME as any]) as unknown as GlobEnvConfig;
24 25 26 27 28 29 30 31 32

  const {
    VITE_GLOB_APP_TITLE,
    VITE_GLOB_API_URL,
    VITE_GLOB_APP_SHORT_NAME,
    VITE_GLOB_API_URL_PREFIX,
    VITE_GLOB_UPLOAD_URL,
  } = ENV;

V
vben 已提交
33
  if (!/^[a-zA-Z_]*$/.test(VITE_GLOB_APP_SHORT_NAME)) {
34
    warn(
V
vben 已提交
35
      `VITE_GLOB_APP_SHORT_NAME Variables can only be characters/underscores, please modify in the environment variables and re-running.`,
36 37
    );
  }
V
Vben 已提交
38

39 40 41 42 43 44 45
  return {
    VITE_GLOB_APP_TITLE,
    VITE_GLOB_API_URL,
    VITE_GLOB_APP_SHORT_NAME,
    VITE_GLOB_API_URL_PREFIX,
    VITE_GLOB_UPLOAD_URL,
  };
V
Vben 已提交
46 47
}

陈文彬 已提交
48
/**
W
Wang Weitao 已提交
49
 * @description: Development mode
陈文彬 已提交
50 51 52 53
 */
export const devMode = 'development';

/**
V
vben 已提交
54
 * @description: Production mode
陈文彬 已提交
55 56 57 58
 */
export const prodMode = 'production';

/**
V
vben 已提交
59
 * @description: Get environment variables
陈文彬 已提交
60 61 62
 * @returns:
 * @example:
 */
V
vben 已提交
63 64 65
export function getEnv(): string {
  return import.meta.env.MODE;
}
陈文彬 已提交
66 67

/**
V
vben 已提交
68
 * @description: Is it a development mode
陈文彬 已提交
69 70 71
 * @returns:
 * @example:
 */
V
vben 已提交
72 73 74
export function isDevMode(): boolean {
  return import.meta.env.DEV;
}
陈文彬 已提交
75 76

/**
V
vben 已提交
77
 * @description: Is it a production mode
陈文彬 已提交
78 79 80
 * @returns:
 * @example:
 */
V
vben 已提交
81 82 83
export function isProdMode(): boolean {
  return import.meta.env.PROD;
}