import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import tailwindcss from '@tailwindcss/vite'
 // @ts-expect-error process is a nodejs global
import path from 'node:path'

import ElementPlus from 'unplugin-element-plus/vite'
 // @ts-expect-error process is a nodejs global
const host = process.env.TAURI_DEV_HOST

// https://vitejs.dev/config/
export default defineConfig(async () => ({
  resolve: {
    alias: {
      // @ts-expect-error process is a nodejs global
      '@': path.join(process.cwd(), './src'),
    },
  },
  plugins: [
    vue(),
    tailwindcss(),
    ElementPlus({
      useSource: true, // 可选，是否使用源码
    }),
  ],

  // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
  //
  // 1. prevent vite from obscuring rust errors
  clearScreen: false,
  // 2. tauri expects a fixed port, fail if that port is not available
  server: {
    port: 1420,
    strictPort: true,
    host: host || false,
    hmr: host
      ? {
          protocol: 'ws',
          host,
          port: 1421,
        }
      : undefined,
    watch: {
      // 3. tell vite to ignore watching `src-tauri`
      ignored: ['**/src-tauri/**'],
    },
    // css: {
    //   postcss: {
    //     plugins: [
    //       tailwindcss(),
    //       autoprefixer(),
    //     ],
    //   },
    // },
    // css: {
    //   preprocessorOptions: {
    //     scss: {
    //       additionalData: `@use "~/styles/element/index.scss" as *;`,
    //     },
    //   },
    // },
  },
}))
