import { defineConfig } from 'vite';
import { resolve } from 'path';

const isProd = process.env.NODE_ENV === 'production' || process.argv.includes('build');

export default defineConfig({
  root: '.',
  base: './',
  publicDir: false,
  css: {
    devSourcemap: !isProd,
  },
  build: {
    outDir: 'assets/dist',
    emptyOutDir: true,
    manifest: 'manifest.json',
    sourcemap: !isProd,
    minify: isProd ? 'esbuild' : false,
    cssMinify: isProd,
    rollupOptions: {
      input: {
        main: resolve(__dirname, 'src/main.js'),
        admin: resolve(__dirname, 'src/admin.js'),
      },
      output: {
        entryFileNames: isProd ? '[name].min.[hash].js' : '[name].js',
        chunkFileNames: isProd ? 'chunks/[name].[hash].js' : 'chunks/[name].js',
        assetFileNames: ({ name }) => {
          if (name && name.endsWith('.css')) {
            return isProd ? '[name].min.[hash][extname]' : '[name][extname]';
          }
          return isProd ? '[name].[hash][extname]' : '[name][extname]';
        },
      },
    },
  },
  server: {
    origin: 'http://localhost:5173',
    cors: true,
    strictPort: true,
    port: 5173,
  },
});
