// Library build config — produces a deployable ES module bundle of GlitchPlayer
// for embedding in sibling repos (e.g. gnommoeditor).
//
// Usage:
//   npm run build:lib               — build vendor assets to dist/vendor/
//   npm run deploy:editor           — alias for npm run build:lib

import { resolve } from 'node:path';
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  resolve: {
    alias: {
      '@gnommo/slide-contracts': resolve(
        process.cwd(),
        'packages/slide-contracts/src/index.ts'
      )
    }
  },
  plugins: [react()],
  build: {
    lib: {
      entry: resolve(process.cwd(), 'src/lib-entry.ts'),
      name: 'GnommoPlayer',
      fileName: 'gnommoplayer',
      formats: ['es'],
    },
    rollupOptions: {
      external: ['react', 'react-dom', 'react/jsx-runtime'],
      output: {
        // Bundle all dynamic slide imports inline — produces a single JS file,
        // no async chunks to manage when deploying to another repo.
        inlineDynamicImports: true,
        assetFileNames: (assetInfo) => {
          if (assetInfo.name === 'style.css') {
            return 'gnommoplayer.css';
          }

          return 'assets/[name]-[hash][extname]';
        },
      },
    },
    cssCodeSplit: false,
    outDir: 'dist/vendor',
    emptyOutDir: true,
  }
});
