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

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    VitePWA({
      registerType: 'autoUpdate',
      workbox: {
        cleanupOutdatedCaches: true,
        skipWaiting: true,
        clientsClaim: true
      }
    })
  ],
  
  // 基础路径
  base: '/schoolNewsWeb/',
  file: {
    downloadUrl: "http://127.0.0.1:8081/schoolNewsServ/file/download/"
  },
  
  // 输出目录
  build: {
    outDir: 'dist',
    assetsDir: 'static',
    sourcemap: true, // 开启 source map 用于调试
    chunkSizeWarningLimit: 1500,
    rollupOptions: {
      output: {
        manualChunks: {
          'vue-vendor': ['vue', 'vue-router', 'vuex'],
          'element-plus': ['element-plus']
        }
      }
    }
  },
  
  // 路径别名
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src')
    },
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
  },
  
  // 环境变量
  define: {
    'process.env.BASE_URL': JSON.stringify('/schoolNewsWeb/'),
    'process.env.VITE_API_BASE_URL': JSON.stringify('/api'),
    'process.env.VITE_APP_TITLE': JSON.stringify('校园新闻管理系统')
  },
  
  // 开发服务器配置
  server: {
    host: '0.0.0.0',
    port: 8080,
    open: '/schoolNewsWeb/',
    
    // 代理配置
    proxy: {
      '/api': {
        target: 'http://127.0.0.1:8081/schoolNewsServ',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ''),
        configure: (proxy, options) => {
          proxy.on('proxyReq', (proxyReq, req, res) => {
            console.log('代理请求:', req.method, req.url, '->', proxyReq.path);
          });
        }
      }
    }
  },
  
  // CSS 配置
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: ''
      }
    }
  }
})

