// Vite config factories shared across webapp-template-derived projects. // // Derived projects typically have two bundles: // - the admin / owner SPA (`defineAdminConfig`) // - the public guest site (`defineGuestConfig`) // Both honour the same Apache-proxy-prefix baseUrl convention and keep // their outputs under a conventional path so the `StaticController` and // Apache vhost can serve them without per-project tweaks. import type { UserConfig } from 'vite'; import react from '@vitejs/plugin-react'; import path from 'path'; export interface AdminConfigOptions { /** Directory the vite.config.ts lives in — pass `__dirname` or `import.meta.dirname`. */ root: string; /** Optional manual chunks for large vendor libs. */ vendorChunks?: Record; /** Override the default output dir (default: `/../static/dist`). */ outDir?: string; } export function defineAdminConfig(opts: AdminConfigOptions): UserConfig { const base = process.env.VITE_BASE || '/'; return { base, plugins: [react()], build: { outDir: opts.outDir ?? path.resolve(opts.root, '../static/dist'), emptyOutDir: true, manifest: true, rollupOptions: opts.vendorChunks ? { output: { manualChunks: opts.vendorChunks } } : undefined, }, }; } export interface GuestConfigOptions extends AdminConfigOptions { /** Entry point relative to root (default: `src/guest/main.tsx`). */ entry?: string; } export function defineGuestConfig(opts: GuestConfigOptions): UserConfig { const base = process.env.VITE_BASE || '/'; return { base, plugins: [react()], build: { outDir: opts.outDir ?? path.resolve(opts.root, '../static/guest/dist'), emptyOutDir: true, manifest: true, rollupOptions: { input: path.resolve(opts.root, opts.entry ?? 'src/guest/main.tsx'), output: { entryFileNames: 'guest-app.[hash].js', chunkFileNames: 'guest-chunk-[hash].js', assetFileNames: 'guest-asset-[hash][extname]', manualChunks: opts.vendorChunks, }, }, }, }; }