import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';

export default defineConfig({
  plugins: [react()],
  define: {
    __BUILD_TIME__: JSON.stringify(new Date().toISOString()),
  },
  build: {
    rollupOptions: {
      output: {
        // 큰 라이브러리를 별도 청크로 분리 (변수 충돌 방지 + 로딩 최적화)
        // Vite 8(rolldown)은 manualChunks를 함수 형식으로만 허용
        manualChunks(id) {
          if (id.includes('node_modules/react-dom') || id.includes('node_modules/react/')) {
            return 'vendor-react';
          }
          if (id.includes('node_modules/video.js')) return 'vendor-videojs';
          if (id.includes('node_modules/hls.js')) return 'vendor-hls';
        },
      },
    },
  },
  resolve: {
    alias: {
      '@abcvideo/shared': path.resolve(__dirname, '../shared/src/index.ts'),
    },
  },
  server: {
    port: 5173,
    host: '0.0.0.0',
    allowedHosts: true,
    proxy: {
      '/api': {
        target: 'http://localhost:3030',
        changeOrigin: true,
      },
    },
  },
});
