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

const __dirname = decodeURIComponent(new URL('.', import.meta.url).pathname);

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  server: {
    port: 3000,
    proxy: {
      '/api': {
        target: 'http://localhost:8001',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '/api/v1'),
        timeout: 600000 // 10分钟超时
      }
    }
  },
  build: {
    outDir: 'dist',
    assetsDir: 'assets',
    minify: 'terser',
    sourcemap: false,
    // 代码分割配置
    rollupOptions: {
      output: {
        manualChunks: {
          // 第三方库分割
          vendor: ['vue', 'vue-router', 'pinia'],
          // UI库分割
          element: ['element-plus'],
          // 图表库分割
          charts: ['echarts'],
          // 网络请求库分割
          http: ['axios']
        },
        // 缓存策略
        chunkFileNames: 'assets/js/[name]-[hash].js',
        entryFileNames: 'assets/js/[name]-[hash].js',
        assetFileNames: 'assets/[ext]/[name]-[hash].[ext]'
      }
    },
    // 压缩配置
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true
      }
    }
  },
  // 别名配置
  resolve: {
    alias: {
      '@': resolve(process.cwd(), 'src')
    }
  }
})
