From 0b3f5601c85eecb2f2a76a805421242bc0523014 Mon Sep 17 00:00:00 2001 From: h_mo <596417202@qq.com> Date: Tue, 28 Mar 2023 23:16:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20[H5]:=E8=BF=90=E8=A1=8C=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=88=A4=E6=96=AD=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 3 +-- .env.production | 3 +-- src/types/env.d.ts | 2 -- src/utils/env.ts | 11 +++++------ 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/.env.development b/.env.development index 54055fc..8ed0d0c 100644 --- a/.env.development +++ b/.env.development @@ -1,6 +1,5 @@ # 运行环境 -VITE_PROD = false -VITE_DEV = true +VITE_ENV = development VITE_PORT = 3000 diff --git a/.env.production b/.env.production index 8be5611..b1c6c19 100644 --- a/.env.production +++ b/.env.production @@ -1,6 +1,5 @@ # 运行环境 -VITE_PROD = true -VITE_DEV = false +VITE_ENV = production # api域名 VITE_BASE_URL = https://api-catch.ranesuangyu.top diff --git a/src/types/env.d.ts b/src/types/env.d.ts index 37a74ab..b8473b2 100644 --- a/src/types/env.d.ts +++ b/src/types/env.d.ts @@ -12,8 +12,6 @@ interface ImportMetaEnv { readonly VITE_APP_TITLE: string; readonly VITE_BASE_URL: string; readonly VITE_UPLOAD_URL: string; - readonly VITE_PROD: boolean; - readonly VITE_DEV: boolean; readonly VITE_APP_CACHE_PREFIX: string; readonly VITE_PORT: number; } diff --git a/src/utils/env.ts b/src/utils/env.ts index 077b99e..9847900 100644 --- a/src/utils/env.ts +++ b/src/utils/env.ts @@ -25,7 +25,7 @@ export const prodMode = 'production'; * @example: */ export function getEnvMode(): string { - return isDevMode() ? devMode : prodMode; + return getEnvValue('VITE_ENV'); } /** @@ -33,9 +33,8 @@ export function getEnvMode(): string { * @returns: * @example: */ -export function getEnvValue(key: string): T { - // @ts-ignore - return import.meta.env[key]; +export function getEnvValue(key: keyof ImportMetaEnv): T { + return import.meta.env[key] as unknown as T; } /** @@ -44,7 +43,7 @@ export function getEnvValue(key: string): T { * @example: */ export function isDevMode(): boolean { - return getEnvValue('VITE_DEV'); + return getEnvMode() === devMode; } /** @@ -53,7 +52,7 @@ export function isDevMode(): boolean { * @example: */ export function isProdMode(): boolean { - return getEnvValue('VITE_PROD'); + return getEnvMode() === prodMode; } /**