import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; import path from 'path'; import fs from 'fs'; // SPA fallback middleware - runs after Vite's built-in middleware function spaFallback() { return { name: 'spa-fallback', configureServer: function (server) { // Return a function to execute after Vite's internal middleware return function () { server.middlewares.use(function (req, res, next) { var _a, _b; // Skip API routes and file requests if (((_a = req.url) === null || _a === void 0 ? void 0 : _a.startsWith('/api')) || ((_b = req.url) === null || _b === void 0 ? void 0 : _b.includes('.'))) { return next(); } // Serve index.html for SPA routes var indexPath = path.resolve(__dirname, 'index.html'); if (fs.existsSync(indexPath)) { res.statusCode = 200; res.setHeader('Content-Type', 'text/html'); var html = fs.readFileSync(indexPath, 'utf-8'); server.transformIndexHtml(req.url || '/', html).then(function (transformed) { res.end(transformed); }).catch(next); } else { next(); } }); }; }, configurePreviewServer: function (server) { // Same logic for preview server server.middlewares.use(function (req, res, next) { var _a, _b; if (((_a = req.url) === null || _a === void 0 ? void 0 : _a.startsWith('/api')) || ((_b = req.url) === null || _b === void 0 ? void 0 : _b.includes('.'))) { return next(); } var indexPath = path.resolve(__dirname, 'dist/client/index.html'); if (fs.existsSync(indexPath)) { res.statusCode = 200; res.setHeader('Content-Type', 'text/html'); res.end(fs.readFileSync(indexPath, 'utf-8')); } else { next(); } }); } }; } export default defineConfig({ plugins: [react(), spaFallback()], root: '.', publicDir: 'public', build: { outDir: 'dist/client', }, server: { host: '0.0.0.0', proxy: { '/api': { target: 'http://localhost:3000', changeOrigin: true, }, }, }, preview: { host: '0.0.0.0', }, resolve: { alias: { '@': path.resolve(__dirname, 'src/client'), '@shared': path.resolve(__dirname, 'src/shared'), }, }, });