import { defineConfig, type Plugin } from 'vite' import react from '@vitejs/plugin-react' import tailwindcss from '@tailwindcss/vite' import path from 'path' /** 避免 /demo-shop/ 被 SPA 回退成 React,导致 404;改为提供 public 静态页 */ function demoShopStatic(): Plugin { return { name: 'demo-shop-static', configureServer(server) { // 必须挂在内部中间件之前,否则会先进 React SPA server.middlewares.use((req, _res, next) => { const raw = req.url || '' const pathname = raw.split('?')[0] if (pathname === '/demo-shop' || pathname === '/demo-shop/') { const qs = raw.includes('?') ? raw.slice(raw.indexOf('?')) : '' req.url = `/demo-shop/index.html${qs}` } next() }) }, } } export default defineConfig({ plugins: [react(), tailwindcss(), demoShopStatic()], resolve: { alias: { '@': path.resolve(__dirname, 'src'), }, }, server: { proxy: { // 显式开启 WebSocket 代理,确保 /api/ws、/api/widget/ws 在开发环境可升级 '/api': { target: 'http://localhost:8080', changeOrigin: true, ws: true, }, '/health': 'http://localhost:8080', }, }, })