vben
2021-08-24 56a966cfbf8db5b29a42185f0f25a0e800c30dbb
提交 | 用户 | age
fcee7d 1 import type { GlobEnvConfig } from '/#/config';
bb3b8f 2
b7ce74 3 import { warn } from '/@/utils/log';
8a9ca4 4 import pkg from '../../package.json';
f6cef1 5 import { getConfigFileName } from '../../build/getConfigFileName';
8a9ca4 6
f6cef1 7 export function getCommonStoragePrefix() {
b7ce74 8   const { VITE_GLOB_APP_SHORT_NAME } = getAppEnvConfig();
V 9   return `${VITE_GLOB_APP_SHORT_NAME}__${getEnv()}`.toUpperCase();
144ab5 10 }
bb3b8f 11
8a9ca4 12 // Generate cache key according to version
V 13 export function getStorageShortName() {
f6cef1 14   return `${getCommonStoragePrefix()}${`__${pkg.version}`}__`.toUpperCase();
V 15 }
16
17 export function getAppEnvConfig() {
18   const ENV_NAME = getConfigFileName(import.meta.env);
19
00fca0 20   const ENV = (import.meta.env.DEV
f6cef1 21     ? // Get the global configuration (the configuration will be extracted independently when packaging)
00fca0 22       (import.meta.env as unknown as GlobEnvConfig)
V 23     : window[ENV_NAME as any]) as unknown as GlobEnvConfig;
b7ce74 24
V 25   const {
26     VITE_GLOB_APP_TITLE,
27     VITE_GLOB_API_URL,
28     VITE_GLOB_APP_SHORT_NAME,
29     VITE_GLOB_API_URL_PREFIX,
30     VITE_GLOB_UPLOAD_URL,
31   } = ENV;
32
a7c8c6 33   if (!/^[a-zA-Z\_]*$/.test(VITE_GLOB_APP_SHORT_NAME)) {
b7ce74 34     warn(
56a966 35       `VITE_GLOB_APP_SHORT_NAME Variables can only be characters/underscores, please modify in the environment variables and re-running.`,
b7ce74 36     );
V 37   }
215d8b 38
b7ce74 39   return {
V 40     VITE_GLOB_APP_TITLE,
41     VITE_GLOB_API_URL,
42     VITE_GLOB_APP_SHORT_NAME,
43     VITE_GLOB_API_URL_PREFIX,
44     VITE_GLOB_UPLOAD_URL,
45   };
8a9ca4 46 }
V 47
2f6253 48 /**
e8eefd 49  * @description: Development mode
2f6253 50  */
51 export const devMode = 'development';
52
53 /**
236575 54  * @description: Production mode
2f6253 55  */
56 export const prodMode = 'production';
57
58 /**
236575 59  * @description: Get environment variables
2f6253 60  * @returns:
61  * @example:
62  */
144ab5 63 export function getEnv(): string {
V 64   return import.meta.env.MODE;
65 }
2f6253 66
67 /**
236575 68  * @description: Is it a development mode
2f6253 69  * @returns:
70  * @example:
71  */
144ab5 72 export function isDevMode(): boolean {
V 73   return import.meta.env.DEV;
74 }
2f6253 75
76 /**
236575 77  * @description: Is it a production mode
2f6253 78  * @returns:
79  * @example:
80  */
144ab5 81 export function isProdMode(): boolean {
V 82   return import.meta.env.PROD;
83 }