diff --git a/.env-config.ts b/.env-config.ts new file mode 100644 index 0000000..10d8c29 --- /dev/null +++ b/.env-config.ts @@ -0,0 +1,36 @@ +/** 请求服务的环境配置 */ +type ServiceEnv = Record; + +/** 不同请求服务的环境配置 */ +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; +} diff --git a/.eslintignore b/.eslintignore index 3d8ae95..a5a8c50 100644 --- a/.eslintignore +++ b/.eslintignore @@ -11,5 +11,5 @@ lib /docs .vscode .local -!.env.config.ts +!.env-config.ts components.d.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index 21317a3..6ecbf36 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -16,5 +16,5 @@ // 加载配置文件 "eslint.options": { "configFile": ".eslintrc.js" }, // 保存自动格式化 - "editor.formatOnSave": true + "editor.formatOnSave": true, } diff --git a/build/config/proxy.ts b/build/config/proxy.ts index 4a31c7b..f3d7dd5 100644 --- a/build/config/proxy.ts +++ b/build/config/proxy.ts @@ -1,38 +1,23 @@ -/** 不同请求服务的环境配置 */ -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/, ''), - }, - }, -}; - +import type { ProxyOptions } from 'vite'; /** * @description: 生成vite代理字段 * @param {*} env - 环境变量配置 */ -export function createViteProxy(env) { - //判断是否需要开启代理 - const isOpenProxy = env.VITE_HTTP_PROXY === 'Y'; +export function createViteProxy(isOpenProxy: boolean, envConfig: ServiceEnvConfig) { if (!isOpenProxy) return undefined; - // 返回对应代理 - const { VITE_SERVICE_ENV = 'dev' } = env; - return serviceEnv[VITE_SERVICE_ENV]; + 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; } diff --git a/src/types/env.d.ts b/src/types/env.d.ts index ada04c3..c621816 100644 --- a/src/types/env.d.ts +++ b/src/types/env.d.ts @@ -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 { /** 项目基本地址 */ readonly VITE_BASE_URL: string; @@ -15,6 +34,8 @@ interface ImportMetaEnv { readonly VITE_HASH_ROUTE?: 'Y' | 'N'; /** 本地存储前缀 */ readonly VITE_STORAGE_PREFIX?: string; + /** 后端服务的环境类型 */ + readonly VITE_SERVICE_ENV?: ServiceEnvType; } interface ImportMeta { diff --git a/tsconfig.json b/tsconfig.json index 4570e7f..f0498e4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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": { - "target": "ESNext", - "useDefineForClassFields": true, + "baseUrl": ".", "module": "ESNext", - "moduleResolution": "Node", + "target": "ESNext", + "lib": ["DOM", "ESNext"], "strict": true, - "jsx": "preserve", - "sourceMap": true, - "resolveJsonModule": true, - "isolatedModules": true, "esModuleInterop": true, - "lib": ["ESNext", "DOM"], - "skipLibCheck": true, + "allowSyntheticDefaultImports": true, + "jsx": "preserve", + "moduleResolution": "node", + "resolveJsonModule": true, + "noUnusedLocals": true, + "strictNullChecks": true, + "forceConsistentCasingInFileNames": true, "paths": { "~/*": ["./*"], "@/*": ["./src/*"] }, - "types": ["naive-ui/volar"] + "types": ["node", "naive-ui/volar"] }, - "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"] + "exclude": ["node_modules", "dist"] } diff --git a/vite.config.ts b/vite.config.ts index fe723b8..8e26e23 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,6 +1,7 @@ import { defineConfig, loadEnv, ConfigEnv } from 'vite'; import { createViteProxy, setVitePlugins } from './build'; import { resolve } from 'path'; +import { getServiceEnvConfig } from './.env-config'; // 当前执行node命令时文件夹的地址(工作目录) const rootPath: string = resolve(process.cwd()); @@ -12,7 +13,10 @@ export default defineConfig(({ command, mode }: ConfigEnv) => { // 根据当前工作目录中的 `mode` 加载 .env 文件 // 设置第三个参数为 '' 来加载所有环境变量,而不管是否有 `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 { base: env.VITE_BASE_URL, @@ -27,7 +31,7 @@ export default defineConfig(({ command, mode }: ConfigEnv) => { host: '0.0.0.0', port: 3000, open: false, - proxy: createViteProxy(env), + proxy: createViteProxy(isOpenProxy, envConfig), }, preview: { port: 5211,