import { defineConfig, normalizePath, Plugin, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import ViteComponents from "unplugin-vue-components/vite";
import { AntDesignVueResolver } from "unplugin-vue-components/resolvers";

const viteEnv = loadEnv("development", process.cwd());
const SERVER_URL = viteEnv.VITE_APP_PROXY_URL;
console.log("SERVER_URL:", SERVER_URL);
// const isDev = process.env.NODE_ENV === "development";

const path = require("path");

function mockDevLogin(): Plugin {
  return {
    name: "mockDevLogin",
    apply: "build",
    load(id) {
      if (id === normalizePath(path.resolve(__dirname, "src/devLogin.ts"))) {
        return `export const initLogin = () => {}`;
      }
    },
  };
}

// https://vitejs.dev/config/
export default defineConfig({
  base: "/mark/",
  plugins: [
    vue({
      reactivityTransform: true,
    }),
    ViteComponents({
      resolvers: [AntDesignVueResolver()],
      dts: true,
    }),
    mockDevLogin(),
  ],
  server: {
    port: 8059,
    // strictPort: true,
    fs: {
      strict: true,
      allow: ["./"],
    },
    proxy: {
      "/api/": {
        target: SERVER_URL,
        changeOrigin: true,
        // rewrite: (path) => path.replace(/^\/api/, ""),
      },
    },
  },
  resolve: {
    alias: [{ find: "@", replacement: path.resolve(__dirname, "./src") }],
    extensions: [".js", ".mjs", ".ts", ".vue", ".json", ".scss", ".css"],
  },
  build: {
    ssr: false,
    commonjsOptions: { include: [] },
  },
  optimizeDeps: {
    disabled: false,
  },
  // define: {
  //   __VUE_PROD_DEVTOOLS__: true, // no effect
  // },
});
