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

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
  clearScreen: false,
  server: {
    port: 1420,
    strictPort: true,
  },
  envPrefix: ['VITE_', 'TAURI_'],
  build: {
    target: process.env.TAURI_PLATFORM === 'windows' ? 'chrome105' : 'safari13',
    minify: !process.env.TAURI_DEBUG ? 'esbuild' : false,
    sourcemap: !!process.env.TAURI_DEBUG,
    rollupOptions: {
      output: {
        manualChunks(id) {
          if (!id.includes('node_modules')) return undefined;
          if (id.includes('/react') || id.includes('/zustand')) return 'react-vendor';
          if (id.includes('@xterm')) return 'terminal-vendor';
          if (id.includes('/marked') || id.includes('/dompurify')) return 'markdown-vendor';
          if (id.includes('@tauri-apps')) return 'tauri-vendor';
          if (id.includes('/allotment')) return 'layout-vendor';
          return 'vendor';
        },
      },
    },
  },
});
