vite.config.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { defineConfig, loadEnv, type Plugin } from 'vite'
  2. import path from 'path'
  3. import react from '@vitejs/plugin-react'
  4. import tailwindcss from '@tailwindcss/vite'
  5. // Use relative imports here. The '@' alias is configured in resolve.alias
  6. // below and only takes effect during bundling — Node cannot resolve it when
  7. // loading vite.config.ts. Bun resolves tsconfig paths natively, masking the
  8. // issue, but Node does not.
  9. import { normalizeApiPrefix, normalizeWebuiPrefix } from './src/lib/pathPrefix'
  10. /**
  11. * Inject `<script>window.__LIGHTRAG_CONFIG__ = ...</script>` into index.html.
  12. *
  13. * This mirrors what the FastAPI server does at request time in production
  14. * (see `SmartStaticFiles._inject_runtime_config` in
  15. * `lightrag/api/lightrag_server.py`). Doing it in dev too means the SPA
  16. * always reads its prefix the same way, so behaviour matches between
  17. * `bun run dev` and a production deploy.
  18. *
  19. * Only `VITE_DEV_API_PREFIX` is read; the WebUI mount path is fixed at
  20. * `/webui` (matching the backend's hardcoded `WEBUI_PATH`), so the
  21. * injected `webuiPrefix` follows the production formula
  22. * `apiPrefix + "/webui/"` automatically.
  23. */
  24. function lightragRuntimeConfigPlugin(env: Record<string, string>): Plugin {
  25. const apiPrefix = normalizeApiPrefix(env.VITE_DEV_API_PREFIX)
  26. const webuiPrefix = normalizeWebuiPrefix(apiPrefix ? `${apiPrefix}/webui/` : '')
  27. const payload = JSON.stringify({ apiPrefix, webuiPrefix }).replace(
  28. /<\//g,
  29. '<\\/'
  30. )
  31. const snippet = `<script>window.__LIGHTRAG_CONFIG__ = ${payload};</script>`
  32. return {
  33. name: 'lightrag-dev-runtime-config',
  34. apply: 'serve',
  35. transformIndexHtml(html: string) {
  36. return html.replace('<!-- __LIGHTRAG_RUNTIME_CONFIG__ -->', snippet)
  37. }
  38. }
  39. }
  40. // https://vite.dev/config/
  41. export default defineConfig(({ mode }) => {
  42. const env = loadEnv(mode, process.cwd(), '')
  43. // Dev-only: prefix every proxied endpoint with the simulated site
  44. // prefix so e.g. `/site01/documents/...` is forwarded to the backend
  45. // running with LIGHTRAG_API_PREFIX=/site01.
  46. const devApiPrefix = normalizeApiPrefix(env.VITE_DEV_API_PREFIX)
  47. return {
  48. plugins: [react(), tailwindcss(), lightragRuntimeConfigPlugin(env)],
  49. resolve: {
  50. alias: {
  51. '@': path.resolve(__dirname, './src')
  52. },
  53. // Force all modules to use the same katex instance
  54. // This ensures mhchem extension registered in main.tsx is available to rehype-katex
  55. dedupe: ['katex']
  56. },
  57. // Relative base: asset URLs in index.html become `./assets/...` so the
  58. // built bundle works under any reverse-proxy mount point. The browser
  59. // resolves them against the current document URL — which means the
  60. // server MUST serve index.html at a URL ending in '/' (the existing
  61. // /webui → /webui/ redirect already handles this).
  62. base: './',
  63. build: {
  64. outDir: path.resolve(__dirname, '../lightrag/api/webui'),
  65. emptyOutDir: true,
  66. chunkSizeWarningLimit: 3800,
  67. rollupOptions: {
  68. // Let Vite handle chunking automatically to avoid circular dependency issues
  69. output: {
  70. // Ensure consistent chunk naming format
  71. chunkFileNames: 'assets/[name]-[hash].js',
  72. // Entry file naming format
  73. entryFileNames: 'assets/[name]-[hash].js',
  74. // Asset file naming format
  75. assetFileNames: 'assets/[name]-[hash].[ext]'
  76. }
  77. }
  78. },
  79. server: {
  80. proxy: env.VITE_API_PROXY === 'true' && env.VITE_API_ENDPOINTS ?
  81. Object.fromEntries(
  82. env.VITE_API_ENDPOINTS.split(',').map(endpoint => [
  83. devApiPrefix + endpoint,
  84. {
  85. target: env.VITE_BACKEND_URL || 'http://localhost:9621',
  86. changeOrigin: true
  87. // No rewrite: the backend already understands its own prefix
  88. // via FastAPI's root_path, so forward the path verbatim.
  89. }
  90. ])
  91. ) : {}
  92. }
  93. }
  94. })