Vben
2021-03-31 8a14069e71d5393cfa5b758f46a1c5c001fa172b
提交 | 用户 | 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
b25ceb 20   const ENV = ((import.meta.env.DEV
f6cef1 21     ? // Get the global configuration (the configuration will be extracted independently when packaging)
V 22       ((import.meta.env as unknown) as GlobEnvConfig)
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
33   if (!/[a-zA-Z\_]*/.test(VITE_GLOB_APP_SHORT_NAME)) {
34     warn(
35       `VITE_GLOB_APP_SHORT_NAME Variables can only be characters/underscores, please modify in the environment variables and re-running.`
36     );
37   }
38   return {
39     VITE_GLOB_APP_TITLE,
40     VITE_GLOB_API_URL,
41     VITE_GLOB_APP_SHORT_NAME,
42     VITE_GLOB_API_URL_PREFIX,
43     VITE_GLOB_UPLOAD_URL,
44   };
8a9ca4 45 }
V 46
2f6253 47 /**
236575 48  * @description: Development model
2f6253 49  */
50 export const devMode = 'development';
51
52 /**
236575 53  * @description: Production mode
2f6253 54  */
55 export const prodMode = 'production';
56
57 /**
236575 58  * @description: Get environment variables
2f6253 59  * @returns:
60  * @example:
61  */
144ab5 62 export function getEnv(): string {
V 63   return import.meta.env.MODE;
64 }
2f6253 65
66 /**
236575 67  * @description: Is it a development mode
2f6253 68  * @returns:
69  * @example:
70  */
144ab5 71 export function isDevMode(): boolean {
V 72   return import.meta.env.DEV;
73 }
2f6253 74
75 /**
236575 76  * @description: Is it a production mode
2f6253 77  * @returns:
78  * @example:
79  */
144ab5 80 export function isProdMode(): boolean {
V 81   return import.meta.env.PROD;
82 }