import { defineConfig, loadEnv } from 'vite'; import react from '@vitejs/plugin-react'; import path from 'path'; import fs from 'fs'; // https://vitejs.dev/config/ export default defineConfig(({ mode }) => { // Nạp các biến môi trường từ thư mục gốc const env = loadEnv(mode, path.resolve(__dirname, '..'), ''); // Cấu hình HTTPS nếu tìm thấy file chứng chỉ (ví dụ đặt tại thư mục gốc của dự án) const httpsConfig = fs.existsSync('../key.pem') && fs.existsSync('../cert.pem') ? { key: fs.readFileSync('../key.pem'), cert: fs.readFileSync('../cert.pem'), } : undefined; return { plugins: [react()], resolve: { alias: { '@': path.resolve(__dirname, './src'), }, }, build: { chunkSizeWarningLimit: 1500, rollupOptions: { output: { manualChunks(id) { if (id.includes('node_modules')) { if (id.includes('react') || id.includes('scheduler')) { return 'vendor-react'; } if (id.includes('leaflet')) { return 'vendor-maps'; } if (id.includes('jspdf')) { return 'vendor-pdf'; } if (id.includes('lucide-react')) { return 'vendor-icons'; } if (id.includes('quill')) { return 'vendor-editor'; } return 'vendor-others'; } } } } }, server: { port: 3002, host: true, // Cho phép các host từ file .env allowedHosts: env.ALLOWED_HOSTS ? env.ALLOWED_HOSTS.split(',') : true, https: httpsConfig, proxy: { '/api': { target: env.VITE_BACKEND_URL || 'http://localhost:3001', changeOrigin: true, }, '/uploads': { target: env.VITE_BACKEND_URL || 'http://localhost:3001', changeOrigin: true, }, '/socket.io': { target: env.VITE_BACKEND_URL || 'http://localhost:3001', ws: true, changeOrigin: true, }, }, headers: { 'Cross-Origin-Opener-Policy': 'same-origin-allow-popups', 'Cross-Origin-Embedder-Policy': 'unsafe-none', }, }, }; });