Files

50 lines
1.5 KiB
TypeScript

import { defineConfig, type Plugin } from 'vite';
import vue from '@vitejs/plugin-vue';
import { fileURLToPath, URL } from 'node:url';
const BASE = process.env.VITE_BASE || '/trace/';
// dev 下访问根路径或不带尾斜杠的 base(如 /trace)时自动 302 到 /trace/。
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();
});
},
};
}
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: 5174,
host: true,
strictPort: true,
watch: process.env.DOCKER_DEV === '1' ? { usePolling: true, interval: 300 } : undefined,
hmr: process.env.VITE_HMR_CLIENT_PORT ? { clientPort: Number(process.env.VITE_HMR_CLIENT_PORT) } : undefined,
proxy: {
'/api': { target: 'http://localhost:3000', changeOrigin: true },
'/uploads': { target: 'http://localhost:3000', changeOrigin: true },
},
},
});