Repository Public
1import { defineConfig, type UserConfig } from 'vite';2import react from '@vitejs/plugin-react';3import pkg from './package.json' with { type: 'json' };45interface Options {6 /** Dev server port. */7 port: number;8 /** Backend origin for the dev proxy (real service, never a mock). */9 target: string;10 /** Path prefixes forwarded to the backend. */11 paths: string[];12 /** Host header presented to the backend (the control plane routes by host). */13 host?: string;14 /** Public origin of the deployed site. With SPWEB_LIVE=1 the dev/preview proxy reads from it instead (read-only visual checks). */15 live?: string;16}1718export function makeConfig(o: Options): UserConfig {19 // No @types/node in this workspace: read the environment through globalThis.20 const env = (globalThis as { process?: { env?: Record<string, string | undefined> } }).process?.env ?? {};21 const live = env.SPWEB_LIVE === '1' && o.live ? o.live : null;22 const proxy = Object.fromEntries(23 o.paths.map((p) => [24 p,25 live26 ? { target: live, changeOrigin: true, secure: true }27 : { target: o.target, changeOrigin: false, headers: o.host ? { host: o.host } : undefined },28 ]),29 );30 return defineConfig({31 base: '/',32 plugins: [react()],33 define: { __SPWEB_VERSION__: JSON.stringify(pkg.version) },34 server: { port: o.port, strictPort: true, proxy },35 preview: { port: o.port, strictPort: true, proxy },36 build: {37 outDir: 'dist',38 emptyOutDir: true,39 sourcemap: false,40 target: 'es2022',41 assetsInlineLimit: 0,42 cssCodeSplit: false,43 modulePreload: { polyfill: false },44 // React changes far less often than the application: its own file stays cached across deployments.45 rollupOptions: { output: { manualChunks: (id: string) => (/node_modules\/(react|react-dom|scheduler)\//.test(id) ? 'react' : undefined) } },46 },47 });48}49