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

const host = process.env.TAURI_DEV_HOST;

export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
      '@shared': path.resolve(__dirname, '../../packages/shared/src'),
      '@db-types': path.resolve(__dirname, '../../packages/db-types/src'),
      '@ui-web': path.resolve(__dirname, '../../packages/ui-web/src'),
    },
  },
  optimizeDeps: {
    // libsodium-wrappers 0.7.16 ships a broken "import" condition in its
    // package exports (the ESM bundle references a sibling ./libsodium.mjs
    // that isn't in the published artefact). Force esbuild to pick the
    // "require" condition so the self-contained CJS build is used.
    include: ['libsodium-wrappers'],
    esbuildOptions: {
      conditions: ['require', 'node', 'default'],
    },
  },
  clearScreen: false,
  server: {
    port: 1420,
    strictPort: true,
    host: host ?? false,
    ...(host ? { hmr: { protocol: 'ws' as const, host, port: 1421 } } : {}),
    watch: {
      ignored: ['**/src-tauri/**'],
    },
  },
});
