import { fileURLToPath, URL } from "node:url";

import vue from "@vitejs/plugin-vue";
import { defineConfig } from "vite";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { VantResolver } from "@vant/auto-import-resolver";

export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver(), VantResolver()],
    }),
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  server: {
    port: 5173,
    proxy: {
      "/api": "http://127.0.0.1:8080",
    },
    allowedHosts: [
      '221329.cc.cd',
      "d4aa-77-93-89-84.ngrok-free.app",
      "frp-bus.com",
      "www.u499731.nyat.app",
      "fn.221329.xyz"
    ],
  },
  build: {
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (!id.includes("node_modules")) return;
          if (id.includes("element-plus") || id.includes("@element-plus")) {
            return "vendor-element-plus";
          }
          if (id.includes("vant") || id.includes("@vant")) {
            return "vendor-vant";
          }
          if (id.includes("vue") || id.includes("pinia")) {
            return "vendor-vue";
          }
          if (id.includes("axios")) {
            return "vendor-axios";
          }
          return "vendor";
        },
      },
    },
  },
});
