import { defineConfig, loadEnv } from "vite"; import laravel from "laravel-vite-plugin"; import tailwindcss from "@tailwindcss/vite"; import { VitePWA } from "vite-plugin-pwa"; export default defineConfig(({ mode }) => { // Load env file based on `mode` in the current working directory const env = loadEnv(mode, process.cwd(), ''); // Extract hostname from APP_URL or VITE_DEV_SERVER_URL const appUrl = env.APP_URL || 'http://localhost'; const viteDevUrl = env.VITE_DEV_SERVER_URL || ''; // Parse hostname from APP_URL (e.g., "http://order-flow-updates.cannabrands.test" -> "order-flow-updates.cannabrands.test") const appHost = appUrl.replace(/^https?:\/\//, '').split(':')[0]; const appOrigin = appUrl.replace(/\/$/, ''); // Full origin with port for CORS (e.g., http://localhost:8080) const viteHost = viteDevUrl ? viteDevUrl.replace(/^https?:\/\//, '').split(':')[0] : `vite.${appHost}`; // Use full VITE_DEV_SERVER_URL if provided, otherwise construct from hostname const viteOrigin = viteDevUrl || `http://${viteHost}`; return { plugins: [ tailwindcss(), laravel({ input: ["resources/css/app.css", "resources/js/app.js"], refresh: true, }), VitePWA({ registerType: 'autoUpdate', includeAssets: ['favicon.ico', 'icons/apple-touch-icon.png'], manifest: { name: 'Cannabrands Hub', short_name: 'Hub', description: 'Cannabis B2B Sales & Distribution Platform', theme_color: '#5C0C36', background_color: '#ffffff', display: 'standalone', start_url: '/', icons: [ { src: '/icons/icon-192x192.png', sizes: '192x192', type: 'image/png' }, { src: '/icons/icon-512x512.png', sizes: '512x512', type: 'image/png' }, { src: '/icons/icon-512x512.png', sizes: '512x512', type: 'image/png', purpose: 'any maskable' } ] }, workbox: { globPatterns: ['**/*.{js,css,html,ico,png,svg,woff2}'], runtimeCaching: [ { urlPattern: /^https:\/\/fonts\.googleapis\.com\/.*/i, handler: 'CacheFirst', options: { cacheName: 'google-fonts-cache', expiration: { maxEntries: 10, maxAgeSeconds: 60 * 60 * 24 * 365 // 1 year }, cacheableResponse: { statuses: [0, 200] } } }, { urlPattern: /^https:\/\/fonts\.gstatic\.com\/.*/i, handler: 'CacheFirst', options: { cacheName: 'gstatic-fonts-cache', expiration: { maxEntries: 10, maxAgeSeconds: 60 * 60 * 24 * 365 // 1 year }, cacheableResponse: { statuses: [0, 200] } } }, { urlPattern: /\/images\/.*/i, handler: 'StaleWhileRevalidate', options: { cacheName: 'images-cache', expiration: { maxEntries: 50, maxAgeSeconds: 60 * 60 * 24 * 30 // 30 days } } } ] } }), ], server: { host: '0.0.0.0', port: parseInt(env.VITE_PORT || '5173'), strictPort: true, origin: viteOrigin, cors: { origin: [ appOrigin, // Full origin with port (e.g., http://localhost:8080) viteOrigin, 'http://localhost:8000', // Sail default port 'http://localhost', 'http://127.0.0.1:8000', ], credentials: true, }, hmr: { host: viteHost, protocol: 'ws', }, allowedHosts: [ viteHost, appHost, '.cannabrands.test', ], }, }; });