import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import WindiCSS from 'vite-plugin-windicss'
import Pages from 'vite-plugin-pages'
import { viteMockServe } from 'vite-plugin-mock'
import topLevelAwait from "vite-plugin-top-level-await";

// https://vitejs.dev/config/
export default defineConfig(({ command }) => ({
  base: command === 'build' ? '/page/xkxt/' : '/',
  resolve: {
    alias: {
      '~': `${path.resolve(__dirname, 'src')}`,
      '@': `${path.resolve(__dirname, 'src')}`,
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "~/styles/element/index.scss" as *;`,
      },
    },
  },
  plugins: [
    topLevelAwait({
      // The export name of top-level await promise for each chunk module
      promiseExportName: "__tla",
      // The function to generate import names of top-level await promise in each chunk module
      promiseImportName: i => `__tla_${i}`
    }),
    vue(),
    vueJsx(),
    viteMockServe({
      watchFiles: true,
      localEnabled: true,
      prodEnabled: false,
    }),
    // Pages({
    //   // pagesDir: 'src/views',
    //   exclude: ['**/components/*.vue'],
    //   extendRoute: (route) => {
    //     if (route.path === '') {
    //       return {
    //         ...route,
    //         meta: Object.assign(
    //           {
    //             breadcrumb: false,
    //           },
    //           route?.meta
    //         ),
    //       }
    //     }
    //     return route
    //   },
    // }),
    WindiCSS(),
    AutoImport({
      resolvers: [
        ElementPlusResolver({
          importStyle: 'sass',
        }),
      ],
    }),
    Components({
      resolvers: [
        ElementPlusResolver({
          importStyle: 'sass',
        }),
      ],
    }),
  ],
}))
