From 0247f3ebfae86f74a13240a34e387b1fd3e87f7e Mon Sep 17 00:00:00 2001 From: zclzone Date: Wed, 21 Jun 2023 21:53:33 +0800 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20=E7=AE=80=E5=8C=96proxy?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 7 ++----- build/config/define.js | 13 ------------- build/config/index.js | 2 -- build/config/proxy.js | 15 --------------- build/constant.js | 32 ++++++++++++++++++++++++++++++++ settings/index.js | 1 - settings/proxy-config.js | 18 ------------------ vite.config.js | 15 +++++++++------ 8 files changed, 43 insertions(+), 60 deletions(-) delete mode 100644 build/config/define.js delete mode 100644 build/config/index.js delete mode 100644 build/config/proxy.js delete mode 100644 settings/proxy-config.js diff --git a/.env.development b/.env.development index 679e865..9feaca6 100644 --- a/.env.development +++ b/.env.development @@ -5,10 +5,7 @@ VITE_PUBLIC_PATH = '/' VITE_USE_MOCK = true # 是否启用MOCK -VITE_USE_PROXY = false - -# 代理类型(跟启动和构建环境无关) 'dev' | 'test' | 'prod' -VITE_PROXY_TYPE = 'dev' +VITE_USE_PROXY = true # base api -VITE_BASE_API = '/api' \ No newline at end of file +VITE_BASE_API = '/api' diff --git a/build/config/define.js b/build/config/define.js deleted file mode 100644 index 4b10543..0000000 --- a/build/config/define.js +++ /dev/null @@ -1,13 +0,0 @@ -import dayjs from 'dayjs' - -/** - * * 此处定义的是全局常量,启动或打包后将添加到window中 - * https://vitejs.cn/config/#define - */ - -// 项目构建时间 -const _BUILD_TIME_ = JSON.stringify(dayjs().format('YYYY-MM-DD HH:mm:ss')) - -export const viteDefine = { - _BUILD_TIME_, -} diff --git a/build/config/index.js b/build/config/index.js deleted file mode 100644 index 967ddda..0000000 --- a/build/config/index.js +++ /dev/null @@ -1,2 +0,0 @@ -export * from './define' -export * from './proxy' diff --git a/build/config/proxy.js b/build/config/proxy.js deleted file mode 100644 index f3d0c92..0000000 --- a/build/config/proxy.js +++ /dev/null @@ -1,15 +0,0 @@ -import { getProxyConfig } from '../../settings' - -export function createViteProxy(isUseProxy = true, proxyType) { - if (!isUseProxy) return undefined - - const proxyConfig = getProxyConfig(proxyType) - const proxy = { - [proxyConfig.prefix]: { - target: proxyConfig.target, - changeOrigin: true, - rewrite: (path) => path.replace(new RegExp(`^${proxyConfig.prefix}`), ''), - }, - } - return proxy -} diff --git a/build/constant.js b/build/constant.js index cdf559a..6a798a9 100644 --- a/build/constant.js +++ b/build/constant.js @@ -1 +1,33 @@ export const OUTPUT_DIR = 'dist' + +export const PROXY_CONFIG = { + /** + * @desc 替换匹配值 + * @请求路径 http:localhost:3100/api/user + * @转发路径 http://localhost:8080/user + */ + '/api': { + target: 'http://localhost:8080', + changeOrigin: true, + rewrite: (path) => path.replace(new RegExp('^/api'), ''), + }, + /** + * @desc 不替换匹配值 + * @请求路径 http:localhost:3100/api/v2/user + * @转发路径 http://localhost:8080/api/v2/user + */ + '/api/v2': { + target: 'http://localhost:8080', + changeOrigin: true, + }, + /** + * @desc 替换部分匹配值 + * @请求路径 http:localhost:3100/api/v3/user + * @转发路径 http://localhost:8080/user + */ + '/api/v3': { + target: 'http://localhost:8080', + changeOrigin: true, + rewrite: (path) => path.replace(new RegExp('^/api'), ''), + }, +} diff --git a/settings/index.js b/settings/index.js index 1957e86..913a438 100644 --- a/settings/index.js +++ b/settings/index.js @@ -1,2 +1 @@ export * from './theme.json' -export * from './proxy-config' diff --git a/settings/proxy-config.js b/settings/proxy-config.js deleted file mode 100644 index f65738b..0000000 --- a/settings/proxy-config.js +++ /dev/null @@ -1,18 +0,0 @@ -const proxyConfigMappings = { - dev: { - prefix: '/api', - target: 'http://localhost:8080', - }, - test: { - prefix: '/api', - target: 'http://localhost:8080', - }, - prod: { - prefix: '/api', - target: 'http://localhost:8080', - }, -} - -export function getProxyConfig(envType = 'dev') { - return proxyConfigMappings[envType] -} diff --git a/vite.config.js b/vite.config.js index 005f2f9..b3245cb 100644 --- a/vite.config.js +++ b/vite.config.js @@ -1,9 +1,8 @@ import { defineConfig, loadEnv } from 'vite' import { convertEnv, getSrcPath, getRootPath } from './build/utils' -import { createViteProxy, viteDefine } from './build/config' import { createVitePlugins } from './build/plugin' -import { OUTPUT_DIR } from './build/constant' +import { OUTPUT_DIR, PROXY_CONFIG } from './build/constant' export default defineConfig(({ command, mode }) => { const srcPath = getSrcPath() @@ -12,7 +11,7 @@ export default defineConfig(({ command, mode }) => { const env = loadEnv(mode, process.cwd()) const viteEnv = convertEnv(env) - const { VITE_PORT, VITE_PUBLIC_PATH, VITE_USE_PROXY, VITE_PROXY_TYPE } = viteEnv + const { VITE_PORT, VITE_PUBLIC_PATH, VITE_USE_PROXY, VITE_BASE_API } = viteEnv return { base: VITE_PUBLIC_PATH || '/', @@ -22,17 +21,21 @@ export default defineConfig(({ command, mode }) => { '@': srcPath, }, }, - define: viteDefine, plugins: createVitePlugins(viteEnv, isBuild), server: { host: '0.0.0.0', port: VITE_PORT, open: false, - proxy: createViteProxy(VITE_USE_PROXY, VITE_PROXY_TYPE), + proxy: VITE_USE_PROXY + ? { + [VITE_BASE_API]: PROXY_CONFIG[VITE_BASE_API], + '/api/v2': PROXY_CONFIG['/api/v2'], + } + : undefined, }, build: { target: 'es2015', - outDir: OUTPUT_DIR, + outDir: OUTPUT_DIR || 'dist', reportCompressedSize: false, // 启用/禁用 gzip 压缩大小报告 chunkSizeWarningLimit: 1024, // chunk 大小警告的限制(单位kb) }, From 70eab22f65880569369b1388311b07011548cd7b Mon Sep 17 00:00:00 2001 From: zclzone Date: Wed, 21 Jun 2023 22:06:40 +0800 Subject: [PATCH 2/3] =?UTF-8?q?style:=20=E4=BF=AE=E6=94=B9=E6=B3=A8?= =?UTF-8?q?=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/constant.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build/constant.js b/build/constant.js index 6a798a9..e63072c 100644 --- a/build/constant.js +++ b/build/constant.js @@ -3,7 +3,7 @@ export const OUTPUT_DIR = 'dist' export const PROXY_CONFIG = { /** * @desc 替换匹配值 - * @请求路径 http:localhost:3100/api/user + * @请求路径 http://localhost:3100/api/user * @转发路径 http://localhost:8080/user */ '/api': { @@ -13,7 +13,7 @@ export const PROXY_CONFIG = { }, /** * @desc 不替换匹配值 - * @请求路径 http:localhost:3100/api/v2/user + * @请求路径 http://localhost:3100/api/v2/user * @转发路径 http://localhost:8080/api/v2/user */ '/api/v2': { @@ -22,7 +22,7 @@ export const PROXY_CONFIG = { }, /** * @desc 替换部分匹配值 - * @请求路径 http:localhost:3100/api/v3/user + * @请求路径 http://localhost:3100/api/v3/user * @转发路径 http://localhost:8080/user */ '/api/v3': { From d4a5cffd8170596070347052b9581db427ad5b0d Mon Sep 17 00:00:00 2001 From: zclzone Date: Fri, 23 Jun 2023 18:45:58 +0800 Subject: [PATCH 3/3] mod: update copyright --- src/components/common/AppFooter.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/common/AppFooter.vue b/src/components/common/AppFooter.vue index 9d80972..2a4f6a7 100644 --- a/src/components/common/AppFooter.vue +++ b/src/components/common/AppFooter.vue @@ -1,7 +1,7 @@