import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import path from "path"
import { componentPathPlugin } from "./vite-plugin-component-path"
import tailwindcss from "@tailwindcss/vite"
import Vuetify, { transformAssetUrls } from "vite-plugin-vuetify"

export default defineConfig({
  server: {
    port: 8081,
  },
  preview: {
    port: 8081,
  },
  envPrefix: "RACLETTE_",
  build: {
    chunkSizeWarningLimit: 1000,
    target: "es2023",
    rollupOptions: {
      output: {
        manualChunks: (id) => {
          if (id.includes("node_modules/vuetify")) {
            return "vuetify"
          }
          if (
            id.includes("node_modules/vue") ||
            id.includes("node_modules/vue-router")
          ) {
            return "vue-vendor"
          }
          return undefined
        },
      },
    },
  },
  optimizeDeps: {
    include: ["vue", "vue-router"],
    exclude: [
      "vuetify",
      "virtual:plugin-key",
      "virtual:plugin-setup",
      "@racletteOrchestrator/composables/usePluginApi",
    ],
  },
  plugins: [
    vue({
      template: { transformAssetUrls },
    }),
    componentPathPlugin(),
    tailwindcss(),
    Vuetify({
      autoImport: true,
    }),
  ],
  resolve: {
    extensions: [".js", ".ts", ".vue"],
    alias: {
      "@app": path.resolve(__dirname, "./src/app"), // plugin import
      "@raclettejs/core/frontend": path.resolve(__dirname, "./src/core"), // plugin import
      "@raclettejs/core/orchestrator": path.resolve(
        __dirname,
        "./src/orchestrator",
      ), // plugin import
      "@raclettejs/workbench/app": path.resolve(__dirname, "./src/app"), // plugin import
      "@raclettejs/workbench": path.resolve(__dirname, "./src"), // plugin import
      "@raclettejs/core": path.resolve(__dirname, "./types"), // type import only
      "@racletteOrchestrator": path.resolve(__dirname, "./src/orchestrator"), // internal core import
      "@racletteCore": path.resolve(__dirname, "./src/core"), // internal core import
      "@shared": path.resolve(__dirname, "./src/shared"), // plugin import
    },
  },
})
