import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { imagetools } from 'vite-imagetools';
import retrieveGitVersion from './retrieve-git-version.ts';

const brandImageFileNames = ['icon.png', 'logo.png'];

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    react(),
    imagetools({
      defaultDirectives: () => {
        return new URLSearchParams({
          as: 'metadata',
        });
      },
    }),
    retrieveGitVersion('react-rwp'),
  ],
  build: {
    rollupOptions: {
      output: {
        assetFileNames: (assetInfo) => {
          // Preserve the original name + extension for image files
          if (brandImageFileNames.includes(assetInfo.name as string)) {
            return 'assets/[name][extname]';
          }
          return 'assets/[name]-[hash][extname]';
        },
      },
    },
  },
});
