import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import { viteSingleFile } from 'vite-plugin-singlefile';
import path from 'path';
import fs from 'fs';

const appsDir = path.resolve(__dirname, 'src/apps');
const appNames = fs.readdirSync(appsDir).filter(f => fs.statSync(path.join(appsDir, f)).isDirectory());

export default defineConfig({
  plugins: [react(), viteSingleFile()],
  build: {
    rollupOptions: {
      input: Object.fromEntries(
        appNames.map(name => [name, path.resolve(appsDir, name, 'index.html')])
      ),
    },
    outDir: path.resolve(__dirname, '../dist/app-ui'),
    emptyOutDir: true,
  },
  resolve: {
    alias: {
      '@components': path.resolve(__dirname, 'src/components'),
      '@styles': path.resolve(__dirname, 'src/styles'),
    },
  },
});
