import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueSetupExtend from 'vite-plugin-vue-setup-extend'

// element-plus按需要导入
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'

// https://vitejs.dev/config/
export default defineConfig({
  build: {
    outDir: 'web',
    chunkSizeWarningLimit: 1600
  },
  base: '/web',
  plugins: [
    vue(),
    vueSetupExtend(),
    AutoImport({
      //自动引入vue的ref、toRefs、onmounted等，无需在页面中再次引入
      imports: ['vue', 'vue-router', 'pinia'],
      resolvers: [
        IconsResolver({
          prefix: 'Icon'
        }),
        ElementPlusResolver()
      ]
    }),
    Components({
      // 配置element-plus采用sass样式配置系统
      resolvers: [
        IconsResolver({
          enabledCollections: ['ep']
        }),
        ElementPlusResolver({ importStyle: 'sass' })
      ]
    }),
    Icons({
      autoInstall: true
    })
  ],
  resolve: {
    // 实际路径转换 @ -> src
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  css: {
    preprocessorOptions: {
      scss: {
        // 自动导入定制化样式文件进行样式覆盖
        additionalData: `
          @use "@/styles/element/index.scss" as *;
          @use "@/styles/var.scss" as *;
        `
      }
    }
  },
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:8070/',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  }
})
