interface GlobConfig { // Site title title: string; // Service interface url apiUrl: string; // Upload url uploadUrl?: string; // Service interface url prefix urlPrefix?: string; // Project abbreviation shortName: string; } interface GlobEnvConfig { // Site title VITE_GLOB_APP_TITLE: string; // Service interface url VITE_GLOB_API_URL: string; // Service interface url prefix VITE_GLOB_API_URL_PREFIX?: string; // Upload url VITE_GLOB_UPLOAD_URL?: string; } let version = '0.0.1'; const setPackageVersion = (_version: string) => { version = _version; }; const getVariableName = (title: string) => { return `__PRODUCTION__${title.replace(/\s/g, '_').replace(/-/g, '_') || '__APP'}__CONF__` .toUpperCase() .replace(/\s/g, ''); }; function getCommonStoragePrefix() { const { VITE_GLOB_APP_TITLE } = getAppEnvConfig(); return `${VITE_GLOB_APP_TITLE.replace(/\s/g, '_')}__${getEnv()}`.toUpperCase(); } // Generate cache key according to version function getStorageShortName() { return `${getCommonStoragePrefix()}${`__${version}`}__`.toUpperCase(); } function getAppEnvConfig() { const ENV_NAME = getVariableName(import.meta.env.VITE_GLOB_APP_TITLE); const ENV = (import.meta.env.DEV ? // 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; const { VITE_GLOB_APP_TITLE, VITE_GLOB_API_URL, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL } = ENV; return { VITE_GLOB_APP_TITLE, VITE_GLOB_API_URL, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL, }; } /** * @description: Development mode */ const devMode = 'development'; /** * @description: Production mode */ const prodMode = 'production'; /** * @description: Get environment variables * @returns: * @example: */ function getEnv(): string { return import.meta.env.MODE; } /** * @description: Is it a development mode * @returns: * @example: */ function isDevMode(): boolean { return import.meta.env.DEV; } /** * @description: Is it a production mode * @returns: * @example: */ function isProdMode(): boolean { return import.meta.env.PROD; } const useGlobSetting = (): Readonly => { const { VITE_GLOB_APP_TITLE, VITE_GLOB_API_URL, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL } = getAppEnvConfig(); // Take global configuration const glob: Readonly = { title: VITE_GLOB_APP_TITLE, apiUrl: VITE_GLOB_API_URL, shortName: VITE_GLOB_APP_TITLE.replace(/\s/g, '_').replace(/-/g, '_'), urlPrefix: VITE_GLOB_API_URL_PREFIX, uploadUrl: VITE_GLOB_UPLOAD_URL, }; return glob as Readonly; }; export { devMode, getAppEnvConfig, getCommonStoragePrefix, getEnv, getStorageShortName, isDevMode, isProdMode, prodMode, setPackageVersion, useGlobSetting, };