From 22c85f2848de731f8c915cf3597f7a9b1ba14e8f Mon Sep 17 00:00:00 2001 From: "chen.home" <1147347984@qq.com> Date: Sat, 14 Jan 2023 19:33:09 +0800 Subject: [PATCH] =?UTF-8?q?chore(project):=20=E7=A7=BB=E9=99=A4corss-env?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/config/proxy.ts | 30 +++++++++++++------------- package.json | 7 +++---- src/config/env.ts | 44 +++++++++++++++++++-------------------- src/service/http/index.ts | 2 +- src/typings/env.d.ts | 8 +++---- vite.config.ts | 6 +++--- 6 files changed, 46 insertions(+), 51 deletions(-) diff --git a/build/config/proxy.ts b/build/config/proxy.ts index f3d7dd5..3c85b8b 100644 --- a/build/config/proxy.ts +++ b/build/config/proxy.ts @@ -3,21 +3,19 @@ import type { ProxyOptions } from 'vite'; * @description: 生成vite代理字段 * @param {*} env - 环境变量配置 */ -export function createViteProxy(isOpenProxy: boolean, envConfig: ServiceEnvConfig) { - if (!isOpenProxy) return undefined; +export function createViteProxy(envConfig: ServiceEnvConfig) { + const proxy: Record = { + [envConfig.urlPattern]: { + target: envConfig.url, + changeOrigin: true, + rewrite: (path) => path.replace(new RegExp(`^${envConfig.urlPattern}`), ''), + }, + [envConfig.secondUrlPattern]: { + target: envConfig.secondUrl, + changeOrigin: true, + rewrite: (path) => path.replace(new RegExp(`^${envConfig.secondUrlPattern}`), ''), + }, + }; - const proxy: Record = { - [envConfig.urlPattern]: { - target: envConfig.url, - changeOrigin: true, - rewrite: (path) => path.replace(new RegExp(`^${envConfig.urlPattern}`), ''), - }, - [envConfig.secondUrlPattern]: { - target: envConfig.secondUrl, - changeOrigin: true, - rewrite: (path) => path.replace(new RegExp(`^${envConfig.secondUrlPattern}`), ''), - }, - }; - - return proxy; + return proxy; } diff --git a/package.json b/package.json index b98472a..3a5b07a 100644 --- a/package.json +++ b/package.json @@ -12,9 +12,9 @@ "admin" ], "scripts": { - "dev": "cross-env VITE_SERVICE_ENV=dev vite", - "dev:test": "cross-env VITE_SERVICE_ENV=test vite", - "dev:prod": "cross-env VITE_SERVICE_ENV=prod vite", + "dev": "vite", + "dev:test": "vite --mode test", + "dev:prod": "vite --mode production", "build": "vue-tsc --noEmit && vite build", "preview": "vite preview", "lint": "eslint . --fix", @@ -63,7 +63,6 @@ "@vitejs/plugin-vue-jsx": "^3.0.0", "@vue/eslint-config-typescript": "^11.0.2", "commitizen": "^4.2.6", - "cross-env": "^7.0.3", "cz-conventional-changelog": "^3.3.0", "cz-customizable": "^7.0.0", "eslint": "^8.31.0", diff --git a/src/config/env.ts b/src/config/env.ts index 7c58faf..dea6d02 100644 --- a/src/config/env.ts +++ b/src/config/env.ts @@ -3,34 +3,32 @@ type ServiceEnv = Record; /** 不同请求服务的环境配置 */ const serviceEnv: ServiceEnv = { - dev: { - url: 'https://mock.mengxuegu.com/mock/61e4df7c17249f68847fc191/api', - urlPattern: '/url-pattern', - secondUrl: 'http://localhost:8081', - secondUrlPattern: '/second-url-pattern', - }, - test: { - url: 'http://localhost:8080', - urlPattern: '/url-pattern', - secondUrl: 'http://localhost:8081', - secondUrlPattern: '/second-url-pattern', - }, - prod: { - url: 'http://localhost:8080', - urlPattern: '/url-pattern', - secondUrl: 'http://localhost:8081', - secondUrlPattern: '/second-url-pattern', - }, + development: { + url: 'https://mock.mengxuegu.com/mock/61e4df7c17249f68847fc191/api', + urlPattern: '/url-pattern', + secondUrl: 'http://localhost:8081', + secondUrlPattern: '/second-url-pattern', + }, + test: { + url: 'http://localhost:8080', + urlPattern: '/url-pattern', + secondUrl: 'http://localhost:8081', + secondUrlPattern: '/second-url-pattern', + }, + production: { + url: 'http://localhost:8080', + urlPattern: '/url-pattern', + secondUrl: 'http://localhost:8081', + secondUrlPattern: '/second-url-pattern', + }, }; /** * 获取当前环境模式下的请求服务的配置 * @param env 环境 */ -export function getServiceEnvConfig(env: ImportMetaEnv) { - const { VITE_SERVICE_ENV = 'dev' } = env; +export function getServiceEnvConfig(mode: ServiceEnvType = 'development') { + const config = serviceEnv[mode]; - const config = serviceEnv[VITE_SERVICE_ENV]; - - return config; + return config; } diff --git a/src/service/http/index.ts b/src/service/http/index.ts index 5876b84..839d30b 100644 --- a/src/service/http/index.ts +++ b/src/service/http/index.ts @@ -1,7 +1,7 @@ import { getServiceEnvConfig } from '@/config'; import { createRequest } from './request'; -const { url, urlPattern } = getServiceEnvConfig(import.meta.env); +const { url, urlPattern } = getServiceEnvConfig(import.meta.env.MODE); const isHttpProxy = import.meta.env.VITE_HTTP_PROXY === 'Y'; diff --git a/src/typings/env.d.ts b/src/typings/env.d.ts index c6c4a3e..a2f371e 100644 --- a/src/typings/env.d.ts +++ b/src/typings/env.d.ts @@ -1,10 +1,10 @@ /** *后台服务的环境类型 - * - dev: 后台开发环境 + * - development: 后台开发环境 * - test: 后台测试环境 - * - prod: 后台生产环境 + * - production: 后台生产环境 */ -type ServiceEnvType = 'dev' | 'test' | 'prod'; +type ServiceEnvType = 'development' | 'test' | 'production'; /** 后台服务的环境配置 */ interface ServiceEnvConfig { @@ -44,7 +44,7 @@ interface ImportMetaEnv { readonly VITE_STORAGE_ENCRYPT_SECRET: string; /** 后端服务的环境类型 */ - readonly VITE_SERVICE_ENV?: ServiceEnvType; + readonly MODE?: ServiceEnvType; } interface ImportMeta { diff --git a/vite.config.ts b/vite.config.ts index 580d1f3..0072f0e 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -14,9 +14,9 @@ export default defineConfig(({ command, mode }: ConfigEnv) => { // 根据当前工作目录中的 `mode` 加载 .env 文件 // 设置第三个参数为 '' 来加载所有环境变量,而不管是否有 `VITE_` 前缀。 const env = loadEnv(mode, process.cwd(), '') as unknown as ImportMetaEnv; - const isOpenProxy = env.VITE_HTTP_PROXY === 'Y'; - const envConfig = getServiceEnvConfig(env); + + const envConfig = getServiceEnvConfig(mode as ServiceEnvType); return { base: env.VITE_BASE_URL, @@ -31,7 +31,7 @@ export default defineConfig(({ command, mode }: ConfigEnv) => { host: '0.0.0.0', port: 3000, open: true, - proxy: createViteProxy(isOpenProxy, envConfig), + proxy: isOpenProxy ? createViteProxy(envConfig) : undefined, }, preview: { port: 5211,