import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import eslintPlugin from "vite-plugin-eslint";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import {
  ElementPlusResolver,
  VantResolver,
} from "unplugin-vue-components/resolvers";
// import basicSsl from "@vitejs/plugin-basic-ssl";
import removeConsole from "vite-plugin-remove-console";
import EnhanceLog from "vite-plugin-enhance-log";

export default ({ mode }) => {
  return defineConfig({
    plugins: [
      vue(),
      eslintPlugin(),
      removeConsole({
        includes: [
          "assert",
          "clear",
          "count",
          "countReset",
          "createTask",
          "debug",
          "dir",
          "dirxml",
          "error",
          "group",
          "groupCollapsed",
          "groupEnd",
          "info",
          "log",
          "profile",
          "profileEnd",
          "table",
          "time",
          "timeEnd",
          "timeLog",
          "timeStamp",
          "trace",
          "warn",
        ],
      }),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver(), VantResolver()],
      }),
      EnhanceLog({
        preTip: "🚀🚀🚀🚀🚀🚀🚀🚀🚀🚀",
      }),
      // basicSsl(),
    ],
    server: {
      // https: true,
      host: true, // 本机的局域网IP，不然其他人无法通过IP访问到，0.0.0.0或true会自动获取本机的IP
      port: 8099, // 端口号
      open: true, // 是否自动打开浏览器
      proxy: {
        [loadEnv(mode, process.cwd()).VITE_PROXY]: {
          target: loadEnv(mode, process.cwd()).VITE_BASE_URL,
          changeOrigin: true,
          ws: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
    resolve: {
      alias: {
        "@": "/src", // 别名，@代表src目录
      },
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"], // 引入文件时哪些后缀名可以不写
    },
    build: {
      rollupOptions: {
        // 打包多个入口文件
        // input: {
        //   admin: path.resolve(__dirname, "index.html"),
        //   其它入口文件路径需要为src/views/*/index.html
        // },
        output: {
          chunkFileNames: "static/js/[name]-[hash].js",
          entryFileNames: "static/js/[name]-[hash].js",
          assetFileNames: "static/[ext]/name-[hash].[ext]",
        },
      },
    },
  });
};
