import { resolve } from "node:path"
import ui from "@nuxt/ui/vite"
import tailwindcss from "@tailwindcss/vite"
import vue from "@vitejs/plugin-vue"
import { defineConfig } from "vite"
import { builtinModules } from "module"

export default defineConfig({
  plugins: [vue(), tailwindcss(), ui()],
  resolve: {
    alias: [{ find: /^@\//, replacement: `${resolve(__dirname, "src")}/` }],
  },
  build: {
    lib: {
      entry: {
        index: resolve(__dirname, "src/index.ts"),
        module: resolve(__dirname, "src/module.ts"),
      },
      formats: ["es", "cjs"],
      fileName: (format, entryName) => `${entryName}.${format === 'es' ? 'js' : 'cjs'}`,
    },
    rollupOptions: {
      external: [
        "vue",
        "@nuxt/ui",
        "tailwindcss",
        "@vueuse/core",
        "@nuxt/ui",
        "@nuxt/kit",
        "@nuxt/schema",
        ...builtinModules,
        ...builtinModules.map(m => `node:${m}`)
      ],
      output: {
        globals: {
          vue: "Vue",
        },
      },
    },
    cssCodeSplit: false,
    cssMinify: "lightningcss",
  },
})
