import { fileURLToPath, URL } from "node:url";
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueDevTools from "vite-plugin-vue-devtools";
import vueJsx from "@vitejs/plugin-vue-jsx";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import { codeInspectorPlugin } from "code-inspector-plugin";
import AutoImport from "unplugin-auto-import/vite";
import fs from "fs";
import { resolve } from "path";

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());

  return {
    base: "./",
    plugins: [
      vue(),
      vueJsx(),
      AutoImport({
        imports: ["vue", "vue-router"],
      }),
      // vueDevTools(),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [resolve(process.cwd(), "src/assets/svg")],
        // 指定symbolId格式
        symbolId: "icon-[dir]-[name]",
      }),
      codeInspectorPlugin({
        bundler: "vite",
      }),
    ],
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    css: {
      // css预处理器
      preprocessorOptions: {
        less: {
          charset: false, //  解决中文乱码
          javascriptEnabled: true,
          additionalData:
            '@import "@/assets/css/global.less";@import "@/assets/css/theme.less";',
        },
      },
    },
    build: {
      minify: true, // 生产环境不生成sourcemap
      target: "es2015",
      outDir: "dist",
      assetsDir: "static", // 与 index.html 中的路径匹配
      emptyOutDir: true,
      // 警报门槛，限制大文件大小
      chunkSizeWarningLimit: 5000,
      rollupOptions: {
        output: {
          chunkFileNames: "static/js/[name]-[hash].js",
          entryFileNames: "static/js/[name]-[hash].js",
          assetFileNames: "static/[ext]/[name]-[hash].[ext]",
          manualChunks: {
            vue: ["vue", "vue-router", "pinia"],
            ui: ["vant"], 
            vendor: ["lodash", "axios", "dayjs"],
          },
        },
      },
      // rollupOptions: {
      //   external: [], // 外部化处理那些你不想打包进库的依赖
      //   // 静态资源分类打包
      //   output: {
      //     chunkFileNames: "static/js/[name]-[hash].js",
      //     entryFileNames: "static/js/[name]-[hash].js",
      //     assetFileNames: "static/[ext]/[name]-[hash].[ext]",
      //   },
      // },
    },
    server: {
      host: true,
      port: 5173,
      open: true,
      cors: true,
      hmr: true,
      // https: {
      //   key: fs.readFileSync('./certDev/key.pem'),
      //   cert: fs.readFileSync('./certDev/cert.pem'),
      // },
      proxy: {
        "/api/v2": {
          target: env.VITE_PRO_BACKEND_PATH,
          changeOrigin: true,
          secure: true,
        },
        "/api/v1": {
          target: env.VITE_PRO_IM_PATH,
          changeOrigin: true,
          secure: true,
        },
        "/api": {
          target: env.VITE_PRO_PATH,
          changeOrigin: true,
          secure: true,
        },
      },
    },
  };
});
