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

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import AutoImport from 'unplugin-auto-import/vite'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  base: '/opui/',
  server: {
    proxy: {
      '/portal': {
        target: 'http://127.0.0.1:4523/m1/6997834-6716269-default',
        changeOrigin: true
      }
    },
    host: '0.0.0.0',
    port: 7113
  },
  build: {
    assetsDir: 'assets', // 指定生成静态文件目录
    emptyOutDir: true, // 构建时清空该目录
    outDir: 'dist/opui/' // 指定输出路径
  },
  plugins: [
    vue(),
    vueJsx(),
    AutoImport({
      imports: [
        'vue',
        'vue-router',
        { from: '/src/utils/page/config', imports: ['pageConfig'] },
        { from: '/src/utils/page/index', imports: ['handleInit', 'handleMessageBox'] },
        { from: '/src/api/index.ts', imports: ['api'] }
      ],
      dts: 'src/auto-import.d.ts'
    }),
    createSvgIconsPlugin({
      // 指定需要缓存的图标文件夹
      iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
      // 指定symbolId格式
      symbolId: 'icon-[dir]-[name]'
    })
  ],
  css: {
    preprocessorOptions: {
      scss: {
        silenceDeprecations: ['legacy-js-api', 'import']
      }
    }
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
})
