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

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  // 加载环境变量
  const env = loadEnv(mode, process.cwd(), '')

  return {
    plugins: [vue()],

    // 路径别名配置
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src')
      }
    },

    // 开发服务器配置
    server: {
      port: 8080,
      open: true,
      host: true
    },

    // 构建配置
    build: {
      outDir: 'dist/admin',
      sourcemap: mode === 'development',
      // 分包配置
      rollupOptions: {
        output: {
          manualChunks: {
            'chunk-libs': ['vue', 'vue-router', 'vuex'],
            'chunk-antd': ['@hwork/ant-design-vue', '@ant-design/icons-vue']
          }
        }
      },
      // 压缩配置
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: mode === 'production',
          drop_debugger: mode === 'production'
        }
      }
    },

    // CSS 配置
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true
        }
      }
    },

    // 环境变量配置
    define: {
      __VUE_OPTIONS_API__: true,
      __VUE_PROD_DEVTOOLS__: false
    },

    // 优化配置
    optimizeDeps: {
      include: [
        'vue',
        'vue-router',
        'vuex',
        '@hwork/ant-design-vue',
        '@ant-design/icons-vue',
        'axios',
        'dayjs',
        'lodash.get'
      ]
    }
  }
})
