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

export default defineConfig({
  // customElement: true makes vue compile SFC <style> into the custom element
  plugins: [vue({ customElement: true })],
  resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) } },
  // Replace process.env.NODE_ENV so the bundle works in browser without a Node.js shim
  define: {
    'process.env.NODE_ENV': JSON.stringify('production'),
  },
  build: {
    outDir: 'dist-wc',
    lib: {
      entry: fileURLToPath(new URL('./src/web-components/register.ts', import.meta.url)),
      name: 'TvuWebComponents',
      fileName: (format) => (format === 'es' ? 'tvu-web-components.js' : 'tvu-web-components.umd.cjs'),
      formats: ['es', 'umd'],
    },
  },
})
