import { defineConfig } from "vite";
import * as path from "@std/path";
import { svelte } from "@sveltejs/vite-plugin-svelte";

// https://vite.dev/config/
export default defineConfig({
  plugins: [svelte()],
  build: {
    rollupOptions: {
      input: Object.fromEntries([
        ...Deno.readDirSync("./src")
          .filter(({ isFile, name }) => isFile && name.endsWith(".webc.svelte"))
          .map((
            { name },
          ) => [name.replace(/\.svelte$/, ""), path.join("./src", name)]),
        ...Deno.readDirSync("./src")
          .filter(({ isDirectory, name }) =>
            isDirectory && name.endsWith(".webc")
          )
          .map((
            { name },
          ) => [name, path.join("./src", name, "index.svelte")]),
      ]),
      output: {
        dir: path.fromFileUrl(new URL("../js_dist", import.meta.url)),
        entryFileNames: "[name].mjs",
        chunkFileNames: "[name]-[hash].mjs",
        assetFileNames: "[name]-[hash].[ext]",
      },
    },
    sourcemap: true,
  },
});
