Files
Artveris/apps/admin/vite.config.ts
T
Nixevol 6813d5bb34 fix: admin dev 代理补齐短链 /t 与 /trace,本地对齐生产单入口
- 5173 开发服务此前未代理 /t 和 /trace,短链在本地无法打开
- /t 转发 API 302 跳转、/trace 转发 C 端 dev server,实测完整链路打通
2026-07-10 09:57:05 +08:00

80 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { defineConfig, type Plugin } from 'vite';
import vue from '@vitejs/plugin-vue';
import { fileURLToPath, URL } from 'node:url';
import type { ServerResponse } from 'node:http';
const BASE = process.env.VITE_BASE || '/admin/';
// dev 下访问根路径或不带尾斜杠的 base(如 /admin)时自动 302 到 /admin/,
// 避免 vite 只提示 "did you mean /admin/" 而不跳转。
function redirectToBase(base: string): Plugin {
const baseNoSlash = base.replace(/\/$/, '');
return {
name: 'redirect-to-base',
configureServer(server) {
server.middlewares.use((req, res, next) => {
const url = req.url || '/';
const path = url.split('?')[0];
if (path === '/' || path === baseNoSlash) {
const query = url.slice(path.length);
res.statusCode = 302;
res.setHeader('Location', base + query);
res.end();
return;
}
next();
});
},
};
}
// API 重启期间(如安装完成自动切换)代理连接被拒属正常,返回 503 让前端静默重试,
// 避免抛出大段 ECONNREFUSED 堆栈。
function onProxyError(proxy: { on: (e: string, cb: (...a: unknown[]) => void) => void }): void {
proxy.on('error', (...args: unknown[]) => {
const res = args[2] as (ServerResponse & { headersSent?: boolean }) | undefined;
if (!res) return;
if (!res.headersSent && typeof res.writeHead === 'function') {
res.writeHead(503, { 'Content-Type': 'application/json' });
}
if (typeof res.end === 'function') {
res.end('{"code":1005,"message":"service restarting","data":null}');
}
});
}
export default defineConfig({
base: BASE,
plugins: [vue(), redirectToBase(BASE)],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
'@artveris/shared': fileURLToPath(new URL('../../packages/shared/src/index.ts', import.meta.url)),
},
},
server: {
port: 5173,
host: true,
// dev 下把 5173 打造成与生产一致的「单入口」:/api、/uploads、短链 /t 走 API,/trace 走 C 端 dev server。
// 生产由 API 单端口托管全部路径,短链 {BASE_URL}/t/xxx 天然可用;此处让本地行为对齐。
proxy: {
'/api': { target: 'http://localhost:3000', changeOrigin: true, configure: onProxyError },
'/uploads': { target: 'http://localhost:3000', changeOrigin: true },
'^/t/.*': { target: 'http://localhost:3000', changeOrigin: true },
'/trace': { target: 'http://localhost:5174', changeOrigin: true, ws: true },
},
},
build: {
// 拆分体积较大的第三方库,避免单一 index chunk 过大、拖慢首屏
rollupOptions: {
output: {
manualChunks: {
naive: ['naive-ui'],
echarts: ['echarts'],
vendor: ['vue', 'vue-router', 'pinia', 'vue-i18n', 'axios', 'dayjs'],
},
},
},
},
});