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