import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import { resolve } from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [uni()],
  
  // 路径解析
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
      '@/components': resolve(__dirname, 'src/components'),
      '@/pages': resolve(__dirname, 'src/pages'),
      '@/utils': resolve(__dirname, 'src/utils'),
      '@/api': resolve(__dirname, 'src/api'),
      '@/stores': resolve(__dirname, 'src/stores'),
      '@/styles': resolve(__dirname, 'src/styles'),
      '@/types': resolve(__dirname, 'src/types'),
      '@/assets': resolve(__dirname, 'src/assets')
    }
  },
  
  // CSS 配置
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `
          @import "@/styles/variables.scss";
          @import "@/styles/mixins.scss";
        `
      }
    }
  },
  
  // 构建配置
  build: {
    // 输出目录
    outDir: 'dist',
    
    // 生成源码映射
    sourcemap: process.env.NODE_ENV === 'development',
    
    // 构建后是否生成 source map
    minify: 'terser',
    
    // terser 配置
    terserOptions: {
      compress: {
        // 生产环境移除 console
        drop_console: process.env.NODE_ENV === 'production',
        drop_debugger: process.env.NODE_ENV === 'production'
      }
    },
    
    // 分包策略
    rollupOptions: {
      output: {
        // 分包
        manualChunks: {
          // 将 node_modules 中的代码单独打包
          vendor: ['vue', 'pinia'],
          utils: ['axios', 'dayjs', 'lodash-es'],
          charts: ['echarts']
        }
      }
    },
    
    // 资源内联阈值
    assetsInlineLimit: 4096,
    
    // 启用/禁用 CSS 代码拆分
    cssCodeSplit: true,
    
    // 构建时显示文件大小警告的阈值
    chunkSizeWarningLimit: 1000
  },
  
  // 开发服务器配置
  server: {
    host: '0.0.0.0',
    port: 3000,
    open: false,
    cors: true,
    
    // 代理配置
    proxy: {
      '/api': {
        target: process.env.VUE_APP_API_BASE_URL || 'http://localhost:8080',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      },
      '/upload': {
        target: process.env.VUE_APP_UPLOAD_URL || 'http://localhost:8080',
        changeOrigin: true
      }
    }
  },
  
  // 预览服务器配置
  preview: {
    host: '0.0.0.0',
    port: 4173,
    cors: true
  },
  
  // 环境变量配置
  envPrefix: 'VUE_APP_',
  
  // 依赖优化
  optimizeDeps: {
    include: [
      'vue',
      'pinia',
      'axios',
      'dayjs',
      'lodash-es',
      'crypto-js'
    ],
    exclude: [
      '@dcloudio/uni-app'
    ]
  },
  
  // 定义全局常量
  define: {
    __VUE_OPTIONS_API__: true,
    __VUE_PROD_DEVTOOLS__: false,
    __UNI_FEATURE_I18N_EN__: false,
    __UNI_FEATURE_I18N_ES__: false,
    __UNI_FEATURE_I18N_FR__: false,
    __UNI_FEATURE_I18N_ZH_HANS__: true,
    __UNI_FEATURE_I18N_ZH_HANT__: false
  },
  
  // 实验性功能
  experimental: {
    renderBuiltUrl(filename, { hostType }) {
      if (hostType === 'js') {
        return { js: `/${filename}` }
      } else {
        return { relative: true }
      }
    }
  },
  
  // 日志级别
  logLevel: 'info',
  
  // 清除屏幕
  clearScreen: false
})