mirror of
https://github.com/chansee97/nova-admin.git
synced 2025-04-05 19:41:59 +08:00
chore(projects): 修改proxy返回方式
This commit is contained in:
parent
cbe7ed73ae
commit
07bbdc6b18
36
.env-config.ts
Normal file
36
.env-config.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/** 请求服务的环境配置 */
|
||||||
|
type ServiceEnv = Record<ServiceEnvType, ServiceEnvConfig>;
|
||||||
|
|
||||||
|
/** 不同请求服务的环境配置 */
|
||||||
|
const serviceEnv: ServiceEnv = {
|
||||||
|
dev: {
|
||||||
|
url: 'http://localhost:8080',
|
||||||
|
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'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前环境模式下的请求服务的配置
|
||||||
|
* @param env 环境
|
||||||
|
*/
|
||||||
|
export function getServiceEnvConfig(env: ImportMetaEnv) {
|
||||||
|
const { VITE_SERVICE_ENV = 'dev' } = env;
|
||||||
|
|
||||||
|
const config = serviceEnv[VITE_SERVICE_ENV];
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
@ -11,5 +11,5 @@ lib
|
|||||||
/docs
|
/docs
|
||||||
.vscode
|
.vscode
|
||||||
.local
|
.local
|
||||||
!.env.config.ts
|
!.env-config.ts
|
||||||
components.d.ts
|
components.d.ts
|
||||||
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -16,5 +16,5 @@
|
|||||||
// 加载配置文件
|
// 加载配置文件
|
||||||
"eslint.options": { "configFile": ".eslintrc.js" },
|
"eslint.options": { "configFile": ".eslintrc.js" },
|
||||||
// 保存自动格式化
|
// 保存自动格式化
|
||||||
"editor.formatOnSave": true
|
"editor.formatOnSave": true,
|
||||||
}
|
}
|
||||||
|
@ -1,38 +1,23 @@
|
|||||||
/** 不同请求服务的环境配置 */
|
import type { ProxyOptions } from 'vite';
|
||||||
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/, ''),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description: 生成vite代理字段
|
* @description: 生成vite代理字段
|
||||||
* @param {*} env - 环境变量配置
|
* @param {*} env - 环境变量配置
|
||||||
*/
|
*/
|
||||||
export function createViteProxy(env) {
|
export function createViteProxy(isOpenProxy: boolean, envConfig: ServiceEnvConfig) {
|
||||||
//判断是否需要开启代理
|
|
||||||
const isOpenProxy = env.VITE_HTTP_PROXY === 'Y';
|
|
||||||
if (!isOpenProxy) return undefined;
|
if (!isOpenProxy) return undefined;
|
||||||
|
|
||||||
// 返回对应代理
|
const proxy: Record<string, string | ProxyOptions> = {
|
||||||
const { VITE_SERVICE_ENV = 'dev' } = env;
|
[envConfig.urlPattern]: {
|
||||||
return serviceEnv[VITE_SERVICE_ENV];
|
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;
|
||||||
}
|
}
|
||||||
|
21
src/types/env.d.ts
vendored
21
src/types/env.d.ts
vendored
@ -1,3 +1,22 @@
|
|||||||
|
/**
|
||||||
|
*后台服务的环境类型
|
||||||
|
* - dev: 后台开发环境
|
||||||
|
* - test: 后台测试环境
|
||||||
|
* - prod: 后台生产环境
|
||||||
|
*/
|
||||||
|
type ServiceEnvType = 'dev' | 'test' | 'prod';
|
||||||
|
|
||||||
|
/** 后台服务的环境配置 */
|
||||||
|
interface ServiceEnvConfig {
|
||||||
|
/** 请求地址 */
|
||||||
|
url: string;
|
||||||
|
/** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */
|
||||||
|
urlPattern: '/url-pattern';
|
||||||
|
/** 另一个后端请求地址(有多个不同的后端服务时) */
|
||||||
|
secondUrl: string;
|
||||||
|
/** 匹配路径的正则字符串, 用于拦截地址转发代理(任意以 /开头 + 字符串, 单个/不起作用) */
|
||||||
|
secondUrlPattern: '/second-url-pattern';
|
||||||
|
}
|
||||||
interface ImportMetaEnv {
|
interface ImportMetaEnv {
|
||||||
/** 项目基本地址 */
|
/** 项目基本地址 */
|
||||||
readonly VITE_BASE_URL: string;
|
readonly VITE_BASE_URL: string;
|
||||||
@ -15,6 +34,8 @@ interface ImportMetaEnv {
|
|||||||
readonly VITE_HASH_ROUTE?: 'Y' | 'N';
|
readonly VITE_HASH_ROUTE?: 'Y' | 'N';
|
||||||
/** 本地存储前缀 */
|
/** 本地存储前缀 */
|
||||||
readonly VITE_STORAGE_PREFIX?: string;
|
readonly VITE_STORAGE_PREFIX?: string;
|
||||||
|
/** 后端服务的环境类型 */
|
||||||
|
readonly VITE_SERVICE_ENV?: ServiceEnvType;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ImportMeta {
|
interface ImportMeta {
|
||||||
|
@ -1,22 +1,46 @@
|
|||||||
|
// {
|
||||||
|
// "compilerOptions": {
|
||||||
|
// "baseUrl": ".",
|
||||||
|
// "target": "ESNext",
|
||||||
|
// "useDefineForClassFields": true,
|
||||||
|
// "module": "ESNext",
|
||||||
|
// "moduleResolution": "Node",
|
||||||
|
// "strict": true,
|
||||||
|
// "jsx": "preserve",
|
||||||
|
// "sourceMap": true,
|
||||||
|
// "resolveJsonModule": true,
|
||||||
|
// "isolatedModules": true,
|
||||||
|
// "esModuleInterop": true,
|
||||||
|
// "lib": ["ESNext", "DOM"],
|
||||||
|
// "skipLibCheck": true,
|
||||||
|
// "paths": {
|
||||||
|
// "~/*": ["./*"],
|
||||||
|
// "@/*": ["./src/*"]
|
||||||
|
// },
|
||||||
|
// "types": ["node", "vite/client", "naive-ui/volar"]
|
||||||
|
// }
|
||||||
|
// // "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.vue"]
|
||||||
|
// }
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "ESNext",
|
"baseUrl": ".",
|
||||||
"useDefineForClassFields": true,
|
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
"moduleResolution": "Node",
|
"target": "ESNext",
|
||||||
|
"lib": ["DOM", "ESNext"],
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"jsx": "preserve",
|
|
||||||
"sourceMap": true,
|
|
||||||
"resolveJsonModule": true,
|
|
||||||
"isolatedModules": true,
|
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"lib": ["ESNext", "DOM"],
|
"allowSyntheticDefaultImports": true,
|
||||||
"skipLibCheck": true,
|
"jsx": "preserve",
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"resolveJsonModule": true,
|
||||||
|
"noUnusedLocals": true,
|
||||||
|
"strictNullChecks": true,
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
"paths": {
|
"paths": {
|
||||||
"~/*": ["./*"],
|
"~/*": ["./*"],
|
||||||
"@/*": ["./src/*"]
|
"@/*": ["./src/*"]
|
||||||
},
|
},
|
||||||
"types": ["naive-ui/volar"]
|
"types": ["node", "naive-ui/volar"]
|
||||||
},
|
},
|
||||||
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"]
|
"exclude": ["node_modules", "dist"]
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { defineConfig, loadEnv, ConfigEnv } from 'vite';
|
import { defineConfig, loadEnv, ConfigEnv } from 'vite';
|
||||||
import { createViteProxy, setVitePlugins } from './build';
|
import { createViteProxy, setVitePlugins } from './build';
|
||||||
import { resolve } from 'path';
|
import { resolve } from 'path';
|
||||||
|
import { getServiceEnvConfig } from './.env-config';
|
||||||
|
|
||||||
// 当前执行node命令时文件夹的地址(工作目录)
|
// 当前执行node命令时文件夹的地址(工作目录)
|
||||||
const rootPath: string = resolve(process.cwd());
|
const rootPath: string = resolve(process.cwd());
|
||||||
@ -12,7 +13,10 @@ export default defineConfig(({ command, mode }: ConfigEnv) => {
|
|||||||
|
|
||||||
// 根据当前工作目录中的 `mode` 加载 .env 文件
|
// 根据当前工作目录中的 `mode` 加载 .env 文件
|
||||||
// 设置第三个参数为 '' 来加载所有环境变量,而不管是否有 `VITE_` 前缀。
|
// 设置第三个参数为 '' 来加载所有环境变量,而不管是否有 `VITE_` 前缀。
|
||||||
const env = loadEnv(mode, process.cwd(), '');
|
const env = loadEnv(mode, process.cwd(), '') as unknown as ImportMetaEnv;
|
||||||
|
|
||||||
|
const isOpenProxy = env.VITE_HTTP_PROXY === 'Y';
|
||||||
|
const envConfig = getServiceEnvConfig(env);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
base: env.VITE_BASE_URL,
|
base: env.VITE_BASE_URL,
|
||||||
@ -27,7 +31,7 @@ export default defineConfig(({ command, mode }: ConfigEnv) => {
|
|||||||
host: '0.0.0.0',
|
host: '0.0.0.0',
|
||||||
port: 3000,
|
port: 3000,
|
||||||
open: false,
|
open: false,
|
||||||
proxy: createViteProxy(env),
|
proxy: createViteProxy(isOpenProxy, envConfig),
|
||||||
},
|
},
|
||||||
preview: {
|
preview: {
|
||||||
port: 5211,
|
port: 5211,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user