import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; import obfuscator from 'rollup-plugin-obfuscator'; // Sử dụng function để kiểm tra xem đang chạy 'serve' (dev) hay 'build' (production) export default defineConfig(({ command }) => { // Biến kiểm tra: chỉ đúng khi đang chạy lệnh build const isBuild = command === 'build'; return { server: { allowedHosts: ['twanapi-testupload.hf.space'], host: '0.0.0.0', port: 5173, }, plugins: [ react(), // Logic quan trọng: Chỉ chạy obfuscator khi đang Build Production // Nếu chạy Dev, plugin này sẽ bị bỏ qua để máy chạy nhanh isBuild && obfuscator({ global: true, options: { // --- CẤU HÌNH BẢO MẬT MẠNH --- compact: true, controlFlowFlattening: true, controlFlowFlatteningThreshold: 1, deadCodeInjection: true, deadCodeInjectionThreshold: 0.4, // Chống debug & Console debugProtection: true, debugProtectionInterval: 4000, disableConsoleOutput: true, // Mã hóa tên biến & chuỗi identifierNamesGenerator: 'hexadecimal', log: false, renameGlobals: false, rotateStringArray: true, selfDefending: true, stringArray: true, stringArrayEncoding: ['rc4'], stringArrayThreshold: 1, unicodeEscapeSequence: false, // Loại bỏ domainLock (để tránh lỗi khi đổi domain trên HF) domainLock: [], }, }), ], esbuild: { // Xóa console.log và debugger ngay từ bước esbuild drop: ['console', 'debugger'], }, build: { sourcemap: false, // Ẩn hoàn toàn map code minify: 'esbuild', // Nén code chunkSizeWarningLimit: 1500, // Tăng giới hạn cảnh báo size file vì obfuscate làm file nặng hơn rollupOptions: { output: { manualChunks: undefined, } } }, }; });