import { defineConfig, loadEnv } from 'vite'; import react from '@vitejs/plugin-react'; import path from 'path'; export default ({ mode, command }) => { const env = loadEnv(mode, process.cwd(), ''); // 환경 판별 // - development: 로컬 개발 환경 (yarn dev) // - dev: 개발서버 배포 환경 (yarn build:dev) // - qa: QA 환경 (yarn build:qa) // - production: 프로덕션 환경 (yarn build, yarn build:prod) const isLocalDev = mode === 'development'; const isDev = mode === 'dev'; const isQA = mode === 'qa'; const isProd = !isLocalDev && !isDev && !isQA; // production 또는 기타 모드 const isBuild = command === 'build'; // 배포 경로 설정 (예: '/kcgv/', '/' 등) // 로컬 개발 모드(development)에서는 항상 '/' 사용 // 빌드 모드(dev, qa, prod)에서는 VITE_BASE_URL 사용 const base = isLocalDev ? '/' : (env.VITE_BASE_URL || '/'); console.log(`[Vite] Mode: ${mode}, Command: ${command}, Base: ${base}, isLocalDev: ${isLocalDev}`); // 빌드 시 제외할 폴더 패턴 (로컬 개발 모드 제외) // - publish: 퍼블리싱 미리보기 (개발용) // - component/wrap: 레거시 퍼블리시 컴포넌트 // 참고: tracking 폴더의 TS 파일은 JS 파일이 우선 사용되므로 자동 제외됨 const excludePatterns = isBuild && !isLocalDev ? [ /[/\\]publish[/\\]/, /[/\\]component[/\\]wrap[/\\]/, ] : []; return defineConfig({ base, define: { global: 'globalThis', // sockjs-client/stompjs용 global polyfill }, server: { host: true, port: 3000, proxy: { // 지도 타일 서버 '/MAPS': { target: env.VITE_MAP_TILE_URL || 'http://10.26.252.39:9090', changeOrigin: true, secure: false, }, // GeoJSON 데이터 '/geo': { target: env.VITE_MAP_TILE_URL || 'http://10.26.252.39:9090', changeOrigin: true, secure: false, }, // 선박 신호 API (signal-api) // 참조: mda-react-front/vite.config.ts '/signal-api': { target: env.VITE_SIGNAL_API || 'http://10.26.252.39:9090/signal-api', changeOrigin: true, secure: false, rewrite: (path) => path.replace(/^\/signal-api/, ''), }, // 선박 이미지 (국기, 선종 아이콘) // 참조: mda-react-front/vite.config.ts - /ship/image 프록시 '/ship/image': { target: env.VITE_API_URL || 'http://10.26.252.39:9090', changeOrigin: true, secure: false, }, // 항적 조회 API (별도 서버) // 참조: mda-react-front/vite.config.ts - /api/v2/tracks 프록시 '/api/v2/tracks': { target: env.VITE_TRACK_API || 'http://10.26.252.51:8090', changeOrigin: true, secure: false, }, // API 서버 '/api': { target: env.VITE_TRACK_API || 'http://localhost:8090', changeOrigin: true, secure: false, }, }, }, plugins: [ react(), // 빌드 시 개발용 폴더 제외 플러그인 (로컬 개발 모드 제외) isBuild && !isLocalDev && { name: 'exclude-dev-folders', resolveId(source, importer) { // 제외 패턴에 매칭되는 import를 빈 모듈로 대체 const normalizedSource = source.replace(/\\/g, '/'); for (const pattern of excludePatterns) { if (pattern.test(normalizedSource)) { return { id: 'virtual:empty-module', moduleSideEffects: false }; } } return null; }, load(id) { if (id === 'virtual:empty-module') { return 'export default null;'; } return null; }, }, ].filter(Boolean), resolve: { alias: { '@': '/src', }, }, // 빌드 시 console.log, debugger 제거 (로컬 개발 모드 제외) // 개발서버(dev), QA(qa), 프로덕션(prod) 빌드에서만 적용 esbuild: isBuild && !isLocalDev ? { drop: ['console', 'debugger'], } : {}, build: { outDir: 'dist', cssCodeSplit: true, rollupOptions: { output: { manualChunks: { vendor: ['react', 'react-dom', 'react-router-dom'], map: ['ol', 'ol-ext'], state: ['zustand'], }, }, }, }, }); };