import { defineConfig } from "vite";
import path from "path";
import vue from "@vitejs/plugin-vue";
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import Inspect from "vite-plugin-inspect";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

function pathResolve(dir) {
  return path.resolve(process.cwd(), ".", dir);
}

const pathSrc = path.resolve(__dirname, "src");

export default defineConfig({
  base: "/",
  plugins: [
    vue(),
    AutoImport({
      // 自动导入 Vue 相关函数，如：ref, reactive, toRef 等
      imports: [
        "vue",
        "vue-router",
        "pinia",
        {
          "@vueuse/core": [
            // named imports
            "useMouse", // import { useMouse } from '@vueuse/core',
            "useIntervalFn",
            // alias
            ["useFetch", "useMyFetch"], // import { useFetch as useMyFetch } from '@vueuse/core',
          ],
          axios: [
            // default imports
            ["default", "axios"], // import { default as request } from 'request',
          ],
        },
      ],
      // 自动导入 Element Plus 相关函数，如：ElMessage, ElMessageBox... (带样式)
      resolvers: [
        ElementPlusResolver(),

        // 自动导入图标组件
        IconsResolver({
          prefix: "Icon",
        }),
      ],
      //  eslint报错处理
      eslintrc: {
        enabled: false, // Default `false` 若没此json文件，先开启，生成后在关闭
        filepath: "./.eslintrc-auto-import.json", // Default `./.eslintrc-auto-import.json`
        globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
      },
      dts: path.resolve(pathSrc, "auto-imports.d.ts"),
    }),

    Components({
      resolvers: [
        // 自动注册图标组件
        IconsResolver({
          enabledCollections: ["ep"],
        }),
      ],
      dts: path.resolve(pathSrc, "components.d.ts"),
    }),

    Icons({
      autoInstall: true,
    }),
    Inspect(),
    createSvgIconsPlugin({
      // 指定要缓存的图标文件夹
      iconDirs: [path.resolve(process.cwd(), "src/assets/svg")],
      // 执行icon name的格式
      symbolId: "icon-[name]",
    }),
  ],
  build: {
    outDir: "html",

    minify: "terser",
    target: "es2015",
    // Turning off brotliSize display can slightly reduce packaging time
    reportCompressedSize: false,
    chunkSizeWarningLimit: 2000,
    terserOptions: {
      compress: {
        keep_infinity: true, //通过true以防止Infinity被压缩为1/0，这可能会导致Chrome出现性能问题。
        //生产环境时移除console
        drop_console: true,
        drop_debugger: true,
      },
    },
    rollupOptions: {
      output: {
        manualChunks(id) {
          // 分包
          if (id.includes("node_modules")) {
            return id
              .toString()
              .split("node_modules/")[1]
              .split("/")[0]
              .toString();
          }
        },
      },
    },
  },
  resolve: {
    alias: [
      {
        find: /@\//,
        replacement: pathResolve("src") + "/",
      },
    ],
  },
  server: {
    proxy: {
      "/api": {
        // target: 'http://192.168.1.3:8090',
        // target: "http://192.168.1.168:18080",
        target: 'http://47.95.203.241:8082',
        // target: "http://47.95.203.241:18080/",
        changeOrigin: true,
        // 确保服务端设置的 Cookie 域名被重写为当前开发域（localhost），避免浏览器丢弃 Cookie
        cookieDomainRewrite: "",
      },
    },
  },
  preview: {
    port: 8000,
    open: true,
  },
});
