import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'

export default defineConfig(({ mode }) => ({
  base: '/',
  plugins: [vue()],
  resolve: {
    alias: [
      { find: '@', replacement: resolve(__dirname, 'src') },
      // logicflow 仅提供 UMD bundle，Rollup 当 ESM 处理会 TDZ，强制走 CJS 入口
      { find: /^@logicflow\/core$/, replacement: '@logicflow/core/dist/entry.js' },
      { find: /^@logicflow\/extension$/, replacement: '@logicflow/extension/cjs/index.js' }
    ]
  },
  build: {
    sourcemap: mode !== 'production',
    chunkSizeWarningLimit: 2000,
    rollupOptions: {
      output: {
        // logicflow UMD bundle 被 Rollup 分 chunk 后会产生 TDZ 报错，
        // 不做代码分割，打成一个包避免跨 chunk 变量引用顺序问题
        inlineDynamicImports: true
      }
    }
  },
  server: {
    port: 3000,
    proxy: {
      '/api': {
        target: 'http://localhost:8080',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      },
      '/uploads': {
        target: 'http://localhost:8080',
        changeOrigin: true
      },
      '/analysis': {
        target: 'http://localhost:8080',
        changeOrigin: true
      }
    }
  }
}))
