From bf5c0cdf04dec5d5a6f94ab8a8ec3cfae4377d2c Mon Sep 17 00:00:00 2001 From: Redon <790348264@qq.com> Date: Wed, 22 Feb 2023 14:29:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=89=8B=E5=8A=A8=E6=89=93=E5=8C=85=20P?= =?UTF-8?q?roxy=20=E9=97=AE=E9=A2=98(#91)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf: 检查代码 * feat: proxy setting * chore: 调整为测试环境使用 `proxy` --- .env | 5 ++--- .env.development | 1 + config/index.ts | 1 + config/proxy.ts | 16 ++++++++++++++++ src/typings/env.d.ts | 2 +- src/utils/functions/includeCode.ts | 4 +--- src/utils/request/axios.ts | 2 +- vite.config.ts | 13 +++++-------- 8 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 .env.development create mode 100644 config/index.ts create mode 100644 config/proxy.ts diff --git a/.env b/.env index 4d00c89..0d8d53f 100644 --- a/.env +++ b/.env @@ -1,6 +1,5 @@ # Glob API URL -VITE_GLOB_API_URL=/api - -VITE_APP_API_BASE_URL=http://localhost:3002/ +VITE_GLOB_API_URL=http://localhost:3002 +# Glob API Timeout (ms) VITE_GLOB_API_TIMEOUT=100000 diff --git a/.env.development b/.env.development new file mode 100644 index 0000000..ea204a2 --- /dev/null +++ b/.env.development @@ -0,0 +1 @@ +VITE_GLOB_HTTP_PROXY=Y diff --git a/config/index.ts b/config/index.ts new file mode 100644 index 0000000..e739ac8 --- /dev/null +++ b/config/index.ts @@ -0,0 +1 @@ +export * from './proxy' diff --git a/config/proxy.ts b/config/proxy.ts new file mode 100644 index 0000000..cfb1e08 --- /dev/null +++ b/config/proxy.ts @@ -0,0 +1,16 @@ +import type { ProxyOptions } from 'vite' + +export function createViteProxy(isOpenProxy: boolean, viteEnv: ImportMetaEnv) { + if (!isOpenProxy) + return + + const proxy: Record = { + '/api': { + target: viteEnv.VITE_GLOB_API_URL, + changeOrigin: true, + rewrite: path => path.replace(/^\/api/, ''), + }, + } + + return proxy +} diff --git a/src/typings/env.d.ts b/src/typings/env.d.ts index 54408d7..f390ca6 100644 --- a/src/typings/env.d.ts +++ b/src/typings/env.d.ts @@ -3,5 +3,5 @@ interface ImportMetaEnv { readonly VITE_GLOB_API_URL: string; readonly VITE_GLOB_API_TIMEOUT: string; - readonly VITE_APP_API_BASE_URL: string; + readonly VITE_GLOB_HTTP_PROXY: 'Y' | 'N'; } diff --git a/src/utils/functions/includeCode.ts b/src/utils/functions/includeCode.ts index af0949f..6958f91 100644 --- a/src/utils/functions/includeCode.ts +++ b/src/utils/functions/includeCode.ts @@ -1,8 +1,6 @@ function includeCode(text: string | null | undefined) { const regexp = /^(?:\s{4}|\t).+/gm - if (text?.includes(' = ') || text?.match(regexp)) - return true - return false + return !!(text?.includes(' = ') || text?.match(regexp)) } export default includeCode diff --git a/src/utils/request/axios.ts b/src/utils/request/axios.ts index 06f993c..ab7b991 100644 --- a/src/utils/request/axios.ts +++ b/src/utils/request/axios.ts @@ -1,7 +1,7 @@ import axios, { type AxiosResponse } from 'axios' const service = axios.create({ - baseURL: import.meta.env.VITE_GLOB_API_URL, + baseURL: import.meta.env.VITE_GLOB_HTTP_PROXY === 'Y' ? '/api' : import.meta.env.VITE_GLOB_API_URL, timeout: !isNaN(+import.meta.env.VITE_GLOB_API_TIMEOUT) ? Number(import.meta.env.VITE_GLOB_API_TIMEOUT) : 60 * 1000, }) diff --git a/vite.config.ts b/vite.config.ts index affdf9f..d96f9a9 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,10 +1,13 @@ import path from 'path' import { defineConfig, loadEnv } from 'vite' import vue from '@vitejs/plugin-vue' +import { createViteProxy } from './config' export default defineConfig((env) => { const viteEnv = loadEnv(env.mode, process.cwd()) as unknown as ImportMetaEnv + const isOpenProxy = viteEnv.VITE_GLOB_HTTP_PROXY === 'Y' + return { resolve: { alias: { @@ -13,16 +16,10 @@ export default defineConfig((env) => { }, plugins: [vue()], server: { - port: 1002, host: '0.0.0.0', + port: 1002, open: false, - proxy: { - '/api': { - target: viteEnv.VITE_APP_API_BASE_URL, - changeOrigin: true, // 允许跨域 - rewrite: path => path.replace('/api/', '/'), - }, - }, + proxy: createViteProxy(isOpenProxy, viteEnv), }, build: { reportCompressedSize: false,