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

import vue from '@vitejs/plugin-vue'
import vueJsx from "@vitejs/plugin-vue-jsx";
import vueDevTools from "vite-plugin-vue-devtools";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  return {
    plugins: [
      vue(),
      vueDevTools(),
      vueJsx(),
      AutoImport({
        include: [
          /\.[j]sx?$/, // .ts, .tsx, .js, .jsx
          /\.vue$/,
          /\.vue\?vue/, // .vue
          /\.md$/, // .md
        ],
        // 全局引入插件
        imports: [
          // presets
          "vue",
          "vue-router",
          "pinia",
          // custom
          {
            "@vueuse/core": [
              // named imports
              "useMouse", // import { useMouse } from '@vueuse/core',
              // alias
              ["useFetch", "useMyFetch"], // import { useFetch as useMyFetch } from '@vueuse/core',
            ],
          },
        ],
        eslintrc: {
          enabled: false,
          filepath: './.eslintrc-auto-import.json',
          globalsPropValue: true,
        },
        resolvers: [],
      }),
      Components({
        resolvers: [],
      }),
    ],
    server: {
      port: 8917,
      cors: true,
      proxy: {
        "/console": {
          target: "https://klm-service-dev.apps.iytcloud.com/",
          changeOrigin: true,
          secure: false,
        },
        "/api": {
          target: "https://klm-service-dev.apps.iytcloud.com/",
          changeOrigin: true,
          secure: false,
        },
        "/workspaces": {
          target: "https://klm-service-dev.apps.iytcloud.com/",
          changeOrigin: true,
          secure: false,
        }
      },
    },
    resolve: {
      extensions: ['.vue', '.ts',  '.js', '.json', '.tsx', '.jsx'],
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      }
    },
    esbuild: {
      drop: mode === 'production' ? ['console', 'debugger'] : []
    },
    define: {
      // 启用生产环境构建下激活不匹配的详细警告
      __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: 'false',
      __VUE_OPTIONS_API__: "true",
      __VUE_PROD_DEVTOOLS__: "true"
    }
  }
})
