import path from 'path'; import fs from 'fs'; import { defineConfig, loadEnv } from 'vite'; import react from '@vitejs/plugin-react'; export default defineConfig(({ mode }) => { const env = loadEnv(mode, '.', ''); return { server: { port: 3000, host: '0.0.0.0', proxy: { '/api/fetch-data': { target: 'https://www.dropbox.com/scl/fi/b9zxn4z5i7sxwfakk5g5y/Amazon-Sell-Out-2023-2025.csv?rlkey=uoto6v0mm99py8nszy8ldtez8&st=pzn1zkrg&dl=1', changeOrigin: true, rewrite: () => '', // Replace entire path with empty string (target has full URL) followRedirects: true }, '/api/fetch-paneu-stock': { target: 'https://www.dropbox.com/scl/fi/xvsqi89mc8wk2i3jpndll/PANEU-Stock.xlsx?rlkey=djtbez2yi1sohjn203xnbjzl1&st=aogcoiw6&dl=1', changeOrigin: true, rewrite: () => '', followRedirects: true }, '/api/fetch-uk-inventory': { target: 'https://www.dropbox.com/scl/fi/an1ldjmtej7tbt6fuh2ut/UK-Inventory.xlsx?rlkey=5h8e4st885bggibujk0b1lmm8&st=7vbagnwz&dl=1', changeOrigin: true, rewrite: () => '', followRedirects: true }, '/api/fetch-buybox': { target: 'https://www.dropbox.com/scl/fi/dooz56abib51ifaf42cpx/Buy_Box_tracker.xlsx?rlkey=2sx85bne42fju3d8vekfygzau&dl=1', changeOrigin: true, rewrite: () => '', followRedirects: true }, '/api/fetch-ads': { target: 'https://www.dropbox.com/scl/fi/wng8tep7awhvzd65amwad/Ads-Weekly.xlsx?rlkey=kcmoq8dxgibsb2eb8zz47xvyo&dl=1', changeOrigin: true, rewrite: () => '', followRedirects: true }, '/api/fetch-stock': { target: 'https://www.dropbox.com/scl/fi/jaxc1y01ot7wj4ksswaoe/Item-Availability.xlsx?rlkey=v428jlri5jf1z5ggiebze7ovo&dl=1', changeOrigin: true, rewrite: () => '', followRedirects: true }, '/api/fetch-bsr': { target: 'https://www.dropbox.com/scl/fi/r32r9x6r7bvgg6ckiaowk/BSR.xlsx?rlkey=vka50xyhc637riq901qo5bk16&st=y6ys2oz8&dl=1', changeOrigin: true, rewrite: () => '', followRedirects: true } } }, // To support fetching local 'BSR.xlsx' in Vite dev mode, we configure a custom middleware // since we use a local file instead of a dropbox link for BSR: plugins: [ react(), { name: 'serve-local-bsr', configureServer(server) { server.middlewares.use('/api/fetch-bsr', (req, res, next) => { const filePath = path.join(process.cwd(), 'BSR.xlsx'); try { if (fs.existsSync(filePath)) { res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); res.setHeader('Content-Disposition', 'attachment; filename="BSR.xlsx"'); res.setHeader('Cache-Control', 'public, max-age=3600'); const fileStream = fs.createReadStream(filePath); fileStream.pipe(res); } else { res.statusCode = 404; res.end(JSON.stringify({ error: 'BSR data file not found' })); } } catch (err) { res.statusCode = 500; res.end(JSON.stringify({ error: 'Failed' })); } }); } } ], define: { // loadEnv reads .env files; process.env has Vercel/system env vars at build time 'process.env.API_KEY': JSON.stringify( env.GEMINI_API_KEY || env.API_KEY || process.env.GEMINI_API_KEY || process.env.API_KEY || "" ), 'process.env.GEMINI_API_KEY': JSON.stringify( env.GEMINI_API_KEY || env.API_KEY || process.env.GEMINI_API_KEY || process.env.API_KEY || "" ), 'process.env.SUPABASE_URL': JSON.stringify( env.SUPABASE_URL || process.env.SUPABASE_URL || "" ), 'process.env.SUPABASE_ANON_KEY': JSON.stringify( env.SUPABASE_ANON_KEY || process.env.SUPABASE_ANON_KEY || "" ), }, resolve: { alias: { '@': path.resolve(__dirname, '.'), } } }; });