// vite.config.popup.ts
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import tailwindcss from "@tailwindcss/vite";
import path, { resolve } from "path";
import pkg from "./package.json";
import mnf from "./public/manifest.json";

// https://vite.dev/config/
export default defineConfig({
  plugins: [react(), tailwindcss()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
  build: {
    outDir: `${pkg.name}_${mnf.version}`,
    rollupOptions: {
      input: {
        popup: resolve(__dirname, "index.html"),
        content: resolve(__dirname, "src/content/content.tsx"),
        "inject-ui": resolve(__dirname, "src/content/inject-ui.tsx"),
        background: resolve(__dirname, "src/background/background.ts"),
      },
      output: {
        entryFileNames: (chunk) => {
          if (chunk.name === "content") return "content/content.js";
          if (chunk.name === "inject-ui") return "content/inject-ui.js";
          if (chunk.name === "background") return "background/background.js";
          return "assets/[name].js";
        },
        assetFileNames: (assetInfo) => {
          if (assetInfo.name && assetInfo.name.endsWith(".css")) {
            return "assets/style.css"; // luôn build thành style.css
          }
          return "assets/[name]-[hash][extname]";
        },
      },
    },
  },
});
