import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';

// The client stamps its version on every request so the server can enforce
// major-version compatibility (see src/api/client.js). Read it from
// package.json rather than restating it here, so a release bumps one number.
const pkg = JSON.parse(
  readFileSync(fileURLToPath(new URL('./package.json', import.meta.url)), 'utf8'),
);

export default defineConfig({
  define: {
    __CAMBIUM_VERSION__: JSON.stringify(pkg.version),
  },
  // Relative base so the packaged app can load assets over the custom scheme.
  base: './',
  envPrefix: ['VITE_', 'TAURI_ENV_'],
  // Required for platform.js's top-level `await import(...)` (Tauri bridge
  // setup) — esbuild's default target predates top-level-await support.
  build: {
    target: 'es2022',
  },
  plugins: [react()],
  server: { port: 8765 },
});
