From 15beb4b41897b3787c612bfeb0f58a8e4ebbe682 Mon Sep 17 00:00:00 2001
From: Coffee-crocodile <1147347984@qq.com>
Date: Fri, 5 Aug 2022 14:23:19 +0800
Subject: [PATCH] =?UTF-8?q?chore(projects):=20=E5=A2=9E=E5=8A=A0=E6=9E=84?=
 =?UTF-8?q?=E5=BB=BA=E9=85=8D=E7=BD=AE=E5=92=8C=E7=8E=AF=E5=A2=83=E6=9D=A1?=
 =?UTF-8?q?=E4=BB=B6=E4=BB=A3=E7=90=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .env                   |  3 ---
 .env.development       |  1 +
 build/config/index.ts  |  1 +
 build/config/proxy.ts  | 34 ++++++++++++++++++++++++++++++++++
 build/index.ts         |  1 +
 build/plugins/index.ts |  0
 build/utils/index.ts   |  0
 package.json           |  5 ++++-
 vite.config.ts         |  7 +++++--
 9 files changed, 46 insertions(+), 6 deletions(-)
 create mode 100644 build/config/index.ts
 create mode 100644 build/config/proxy.ts
 create mode 100644 build/index.ts
 create mode 100644 build/plugins/index.ts
 create mode 100644 build/utils/index.ts

diff --git a/.env b/.env
index f1f59af..fe8b10e 100644
--- a/.env
+++ b/.env
@@ -1,5 +1,2 @@
-# 项目本地运行端口号
-VITE_PORT = 5200
-
 # 项目根目录
 VITE_BASE_URL=/
\ No newline at end of file
diff --git a/.env.development b/.env.development
index e69de29..3478013 100644
--- a/.env.development
+++ b/.env.development
@@ -0,0 +1 @@
+VITE_HTTP_PROXY=Y
\ No newline at end of file
diff --git a/build/config/index.ts b/build/config/index.ts
new file mode 100644
index 0000000..9bcd868
--- /dev/null
+++ b/build/config/index.ts
@@ -0,0 +1 @@
+export * from './proxy';
diff --git a/build/config/proxy.ts b/build/config/proxy.ts
new file mode 100644
index 0000000..66976da
--- /dev/null
+++ b/build/config/proxy.ts
@@ -0,0 +1,34 @@
+/** 不同请求服务的环境配置 */
+const serviceEnv = {
+  dev: {
+    '/api': {
+      target: 'http://localhost:8080',
+      changeOrigin: true,
+      rewrite: (path) => path.replace(/^\/api/, ''),
+    },
+  },
+  test: {
+    '/api': {
+      target: 'http://localhost:8080',
+      changeOrigin: true,
+      rewrite: (path) => path.replace(/^\/api/, ''),
+    },
+  },
+  prod: {
+    '/api': {
+      target: 'http://localhost:8080',
+      changeOrigin: true,
+      rewrite: (path) => path.replace(/^\/api/, ''),
+    },
+  },
+};
+
+export function createViteProxy(env) {
+  //判断是否需要开启代理
+  const isOpenProxy = env.VITE_HTTP_PROXY === 'Y';
+  if (!isOpenProxy) return undefined;
+
+  // 返回对应代理
+  const { VITE_SERVICE_ENV = 'dev' } = env;
+  return serviceEnv[VITE_SERVICE_ENV];
+}
diff --git a/build/index.ts b/build/index.ts
new file mode 100644
index 0000000..f03c228
--- /dev/null
+++ b/build/index.ts
@@ -0,0 +1 @@
+export * from './config';
diff --git a/build/plugins/index.ts b/build/plugins/index.ts
new file mode 100644
index 0000000..e69de29
diff --git a/build/utils/index.ts b/build/utils/index.ts
new file mode 100644
index 0000000..e69de29
diff --git a/package.json b/package.json
index 3edca74..5e2a029 100644
--- a/package.json
+++ b/package.json
@@ -3,7 +3,9 @@
   "private": true,
   "version": "0.0.0",
   "scripts": {
-    "dev": "vite",
+    "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",
     "build": "vue-tsc --noEmit && vite build",
     "preview": "vite preview",
     "prettier": "prettier --write  \"src/**/*.{js,json,tsx,css,less,scss,vue,html,md}\"",
@@ -33,6 +35,7 @@
     "@vue/eslint-config-prettier": "^7.0.0",
     "@vue/eslint-config-typescript": "^11.0.0",
     "commitizen": "^4.2.5",
+    "cross-env": "^7.0.3",
     "cz-conventional-changelog": "^3.3.0",
     "cz-customizable": "^6.9.1",
     "eslint": "^8.21.0",
diff --git a/vite.config.ts b/vite.config.ts
index 5a5249c..9837502 100644
--- a/vite.config.ts
+++ b/vite.config.ts
@@ -1,4 +1,5 @@
 import { defineConfig, loadEnv, ConfigEnv } from 'vite';
+import { createViteProxy } from './build';
 import vue from '@vitejs/plugin-vue';
 import { resolve } from 'path';
 
@@ -9,6 +10,7 @@ const srcPath: string = `${rootPath}/src`;
 // https://vitejs.dev/config/
 export default defineConfig(({ command, mode }: ConfigEnv) => {
   // 在开发环境下 command 的值为 serve 生产环境下为 build
+
   // 根据当前工作目录中的 `mode` 加载 .env 文件
   // 设置第三个参数为 '' 来加载所有环境变量,而不管是否有 `VITE_` 前缀。
   const env = loadEnv(mode, process.cwd(), '');
@@ -23,8 +25,9 @@ export default defineConfig(({ command, mode }: ConfigEnv) => {
     },
     server: {
       host: '0.0.0.0',
-      port: Number(env.VITE_PORT),
-      open: true,
+      port: 5200,
+      open: false,
+      proxy: createViteProxy(env),
     },
     preview: {
       port: 5211,