refactor(vite): 修复代理配置类型错误

This commit is contained in:
wcg
2026-02-11 10:16:12 +08:00
parent 16b2238f5b
commit 1fd75a198d

View File

@@ -4,7 +4,8 @@ import { loadEnv } from "vite";
// import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
// import Components from 'unplugin-vue-components/vite';
export default defineConfig(async ({ command, mode }) => {
const config: Awaited<ReturnType<typeof defineConfig>> = defineConfig(async (configEnv) => {
const { command, mode = 'development' } = configEnv ?? { command: 'serve', mode: 'development' };
const env = loadEnv(mode, process.cwd(), "");
return {
application: {},
@@ -24,20 +25,21 @@ export default defineConfig(async ({ command, mode }) => {
],
server: {
proxy: {
[env.VITE_GLOB_API_URL]: {
[env.VITE_GLOB_API_URL as string]: {
target: env.VITE_APP_URL,
changeOrigin: true,
rewrite: (path) => path.replace(`${[env.VITE_GLOB_API_URL]}`, ""),
//查看真实代理url
bypass(req, res, options) {
const proxyUrl = options.target + options.rewrite(req.url);
const rewrittenPath = options.rewrite?.(req.url ?? '') ?? req.url ?? '';
const proxyUrl = (options.target ?? '') + rewrittenPath;
console.log(proxyUrl);
req.headers['X-req-proxyURL'] = proxyUrl;
res.setHeader('X-req-proxyURL', proxyUrl);
res?.setHeader('X-req-proxyURL', proxyUrl);
}
},
[env.VITE_APP_BASE_WS]: {
[env.VITE_APP_BASE_WS as string]: {
target: env.VITE_APP_BASE_URL_WS,
changeOrigin: true,
rewrite: (p) => p.replace( `${[env.VITE_APP_BASE_WS]}`, ""),
@@ -45,10 +47,11 @@ export default defineConfig(async ({ command, mode }) => {
//查看真实代理url
bypass(req, res, options) {
const proxyUrl = options.target + options.rewrite(req.url);
const rewrittenPath = options.rewrite?.(req.url ?? '') ?? req.url ?? '';
const proxyUrl = (options.target ?? '') + rewrittenPath;
// console.log(proxyUrl);
req.headers['X-req-proxyURL'] = proxyUrl;
res.setHeader('X-req-proxyURL', proxyUrl);
res?.setHeader('X-req-proxyURL', proxyUrl);
}
},
@@ -57,3 +60,5 @@ export default defineConfig(async ({ command, mode }) => {
},
};
});
export default config;