/*
 * @Author: LiZhiWei
 * @Date: 2025-04-24 15:29:01
 * @LastEditors: LiZhiWei
 * @LastEditTime: 2025-12-30 12:19:24
 * @Description:
 */
import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import AutoImport from "unplugin-auto-import/vite"
import Components from "unplugin-vue-components/vite"
import { ElementPlusResolver } from "unplugin-vue-components/resolvers"

export default defineConfig(({ mode }) => {
  const plugins = [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ]

  const resolve = {
    alias: {
      "@": "/src",
      ...(mode === "lib"
        ? {}
        : {
            "huijia-viewfile": "/src/components/view_file/lib.js",
            "huijia-viewfile/style.css": "/src/components/view_file/index.less",
          }),
    },
  }

  if (mode === "lib") {
    return {
      plugins,
      resolve,
      publicDir: false,
      build: {
        outDir: "dist-lib",
        emptyOutDir: true,
        lib: {
          entry: "src/components/view_file/lib.js",
          name: "ViewFile",
          formats: ["es", "cjs"],
          fileName: (format) => (format === "cjs" ? "index.cjs" : "index.js"),
        },
        rollupOptions: {
          external: ["vue"],
          output: {
            exports: "named",
            globals: {
              vue: "Vue",
            },
          },
        },
      },
    }
  }

  return {
    plugins,
    resolve,
  }
})
