import react from '@vitejs/plugin-react'; import { defineConfig, loadEnv, type Plugin, type ProxyOptions } from 'vite'; import { VitePWA } from 'vite-plugin-pwa'; // Web build: public at https://dev.aggtaa.com/m/, the reverse proxy strips "/m" (X-Forwarded-Prefix). // Capacitor build (--mode capacitor): served from the app bundle root. // // Env (mobile-client/.env or process env): // PORT dev/preview server port (default 10003) // ALLOWED_HOSTS comma-separated public host names (default dev.aggtaa.com) // BACKEND_URL local dev only: server-be address (default http://127.0.0.1:10002) const WEB_BASE = '/m/'; /** The proxy strips /m; put it back so Vite's base-aware dev and preview servers find the files. */ function restoreStrippedBase(): Plugin { const rewrite = (req: { url?: string }, _res: unknown, next: () => void) => { const url = req.url ?? '/'; if (!url.startsWith(WEB_BASE) && !url.startsWith('/api/') && url !== '/api') req.url = WEB_BASE.slice(0, -1) + url; next(); }; return { name: 'cardy-restore-stripped-base', configureServer: (server) => void server.middlewares.use(rewrite), configurePreviewServer: (server) => void server.middlewares.use(rewrite), }; } export default defineConfig(({ mode }) => { const env = loadEnv(mode, process.cwd(), ''); const capacitor = mode === 'capacitor'; // Local development without the reverse proxy: /api/* -> server-be with /api stripped. const proxy: Record = { '/api': { target: env.BACKEND_URL || 'http://127.0.0.1:10002', rewrite: (path) => path.replace(/^\/api/, '') || '/', headers: { 'X-Forwarded-Prefix': '/api' }, }, }; const server = { host: true, port: Number(env.PORT) || 10003, strictPort: true, allowedHosts: (env.ALLOWED_HOSTS || 'dev.aggtaa.com').split(',').map((h) => h.trim()), proxy, }; return { base: capacitor ? './' : WEB_BASE, plugins: [ react(), restoreStrippedBase(), VitePWA({ disable: capacitor, registerType: 'autoUpdate', injectRegister: 'auto', includeAssets: ['icon.svg'], manifest: { name: 'Cardy', short_name: 'Cardy', description: 'Discount cards', theme_color: '#0f1115', background_color: '#0f1115', display: 'standalone', orientation: 'portrait', start_url: '.', scope: '.', icons: [ { src: 'icon.svg', sizes: 'any', type: 'image/svg+xml', purpose: 'any' }, { src: 'icon.svg', sizes: 'any', type: 'image/svg+xml', purpose: 'maskable' }, ], }, workbox: { globPatterns: ['**/*.{js,css,html,svg,wasm,webmanifest}'], // bwip-js and the zxing wasm module are large but needed offline. maximumFileSizeToCacheInBytes: 8 * 1024 * 1024, navigateFallback: 'index.html', navigateFallbackDenylist: [/^\/api(\/|$)/], }, }), ], build: { outDir: capacitor ? 'dist-capacitor' : 'dist', emptyOutDir: true, chunkSizeWarningLimit: 1100, }, server, preview: server, }; });