import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { fileURLToPath, URL } from 'node:url'

// Relative base so the built bundle works both from the preview server and
// when enqueued inside a WordPress admin page.
export default defineConfig({
  base: './',
  plugins: [react()],
  resolve: {
    alias: {
      // Keeps the standard __( 'text', 'domain' ) call shape in source, which wp i18n make-pot
      // extracts, while the runtime reads WordPress's own wp.i18n instead of bundling the library.
      '@wordpress/i18n': fileURLToPath(new URL('./src/wp-i18n.ts', import.meta.url)),
      '@wordpress/element': fileURLToPath(new URL('./src/wp-element.ts', import.meta.url)),
    },
  },
  build: {
    outDir: 'dist',
    sourcemap: false,
    // Stable, single-file output so the WordPress enqueue paths never change.
    rollupOptions: {
      output: {
        inlineDynamicImports: true,
        entryFileNames: 'assets/app.js',
        assetFileNames: 'assets/app.[ext]',
      },
    },
  },
})
