import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

export default defineConfig({
  plugins: [react()],
  // vite's `build.lib` mode skips the process.env.NODE_ENV replacement that
  // its normal app build does automatically (see vite.config.ts) — without
  // this, the bundled React runtime evaluates `process.env.NODE_ENV` in the
  // browser and throws "process is not defined" before anything registers.
  define: {
    'process.env.NODE_ENV': JSON.stringify('production'),
  },
  build: {
    lib: {
      entry:    'src/editor/index.tsx',
      name:     'AccessMateEditor',
      fileName: () => 'editor.js',
      formats:  ['iife'],
    },
    outDir:      'assets',
    emptyOutDir: false,
    rollupOptions: {
      // Use the single React instance WordPress already loads (registered as
      // the 'react'/'react-dom' script handles) — same strategy as
      // vite.config.ts's main admin bundle.
      external: ['react', 'react-dom'],
      output: {
        globals: {
          'react':     'React',
          'react-dom': 'ReactDOM',
        },
      },
    },
    minify: 'terser',
    target: 'es2017',
  },
});
