import { fileURLToPath, URL } from "node:url";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import Components from "unplugin-vue-components/vite";
import { AntDesignVueResolver } from "unplugin-vue-components/resolvers";
import qiankun from "vite-plugin-qiankun";
import yargsParser from "yargs-parser";

const argv = yargsParser(process.argv.slice(2));

console.log(process.env.DEV_ENV);

let base: string;
switch (argv.mode) {
  case "staging":
  case "test":
    base = "https://custom-chart.shiyue.com/";
    break;
  case "production":
    base = "https://custom-chart.shiyuegame.com/";
    break;
  default:
    base = "http://localhost:8080/";
}
if (process.env.DEV_ENV === "development") {
  base = "http://localhost:8080/";
}

// https://vitejs.dev/config/
export default defineConfig({
  build: {
    sourcemap: false,
    rollupOptions: {
      output: {
        assetFileNames: "[name]-ycode-[hash:8].[ext]",
        chunkFileNames: "[name]-ycode-[hash:8].js",
        entryFileNames: "[name]-ycode-[hash:8].js",
      },
    },
  },
  css: {
    devSourcemap: true,
    preprocessorOptions: {
      less: {
        modifyVars: {
          "ant-prefix": "ycode-ant",
        },
      },
    },
  },
  plugins: [
    vue(),
    vueJsx(),
    Components({
      resolvers: [
        AntDesignVueResolver({
          importStyle: "less",
        }),
      ],
    }),
    qiankun("y-code-app", {
      useDevMode: process.env.DEV_ENV === "development",
    }),
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  base,
  define: {
    _BASE_HOST_: `'${base}'`,
  },
  optimizeDeps: {
    force: true, // 强制重新预构建依赖
  },
  server: {
    hmr: true,
    host: "0.0.0.0",
    port: 8080,
  },
});
