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

const SERVER_URL = "http://192.168.10.224";
// const SERVER_URL = "http://192.168.11.103:8090";

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({
  plugins: [
    vue({
      reactivityTransform: true,
    }),
    ViteComponents({
      resolvers: [AntDesignVueResolver()],
      dts: true,
    }),
    mockDevLogin(),
  ],
  server: {
    port: 9000,
    // strictPort: true,
    fs: {
      strict: true,
      allow: ["./"],
    },
    proxy: {
      "/login": {
        target: SERVER_URL,
        changeOrigin: true,
      },
      "/mark": {
        target: SERVER_URL,
        changeOrigin: true,
      },
      "/admin": {
        target: SERVER_URL,
        changeOrigin: true,
      },
      "/api": {
        target: SERVER_URL,
        changeOrigin: true,
      },
    },
  },
  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
  // },
});
