/// <reference types="vite/client" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { resolve } from 'path';

/**
 * Vite library-mode config for the <kerf-embed> Web Component.
 *
 * Bundles everything (React, Konva, opentype.js) into a self-contained
 * output — nothing is externalized. Outputs ES module + IIFE formats.
 */
export default defineConfig({
  plugins: [react()],
  build: {
    outDir: 'dist-embed',
    emptyOutDir: true,
    lib: {
      entry: resolve(__dirname, 'src/embed.tsx'),
      name: 'KerfEmbed',
      formats: ['es', 'iife'],
      fileName: (format) => {
        if (format === 'es') return 'kerf-embed.js';
        return 'kerf-embed.iife.js';
      },
    },
    // Don't inline CSS — extract to style.css for optional manual loading
    cssCodeSplit: false,
    rollupOptions: {
      // Explicitly empty to prevent any auto-externalization
      external: [],
      output: {
        // Prevent chunk splitting — single self-contained bundle
        codeSplitting: false,
        // Name the CSS output predictably
        assetFileNames: (assetInfo) => {
          if (assetInfo.names?.[0]?.endsWith('.css')) return 'style.css';
          return assetInfo.names?.[0] ?? '[name][extname]';
        },
      },
    },
  },
});
