|
1 | | -import { defineConfig } from 'vite' |
| 1 | +import { defineConfig, loadEnv } from 'vite' |
2 | 2 | import path from 'path' |
3 | | -import { webuiPrefix } from '@/lib/constants' |
4 | 3 | import react from '@vitejs/plugin-react-swc' |
5 | 4 | import tailwindcss from '@tailwindcss/vite' |
6 | 5 |
|
| 6 | +// Use relative import instead of '@/lib/constants' path alias. |
| 7 | +// The '@' alias is configured in this file's resolve.alias and only takes effect |
| 8 | +// during bundling — Node.js cannot resolve it when loading vite.config.ts itself. |
| 9 | +// Bun resolves tsconfig paths natively, masking the issue, but Node.js does not. |
| 10 | +import { webuiPrefix } from './src/lib/constants' |
| 11 | + |
7 | 12 | // https://vite.dev/config/ |
8 | | -export default defineConfig({ |
9 | | - plugins: [react(), tailwindcss()], |
10 | | - resolve: { |
11 | | - alias: { |
12 | | - '@': path.resolve(__dirname, './src') |
| 13 | +// Use functional config form so we can call loadEnv(). import.meta.env is only |
| 14 | +// available inside Bun's runtime; Node.js leaves it undefined, crashing the build. |
| 15 | +export default defineConfig(({ mode }) => { |
| 16 | + const env = loadEnv(mode, process.cwd(), '') |
| 17 | + |
| 18 | + return { |
| 19 | + plugins: [react(), tailwindcss()], |
| 20 | + resolve: { |
| 21 | + alias: { |
| 22 | + '@': path.resolve(__dirname, './src') |
| 23 | + }, |
| 24 | + // Force all modules to use the same katex instance |
| 25 | + // This ensures mhchem extension registered in main.tsx is available to rehype-katex |
| 26 | + dedupe: ['katex'] |
13 | 27 | }, |
14 | | - // Force all modules to use the same katex instance |
15 | | - // This ensures mhchem extension registered in main.tsx is available to rehype-katex |
16 | | - dedupe: ['katex'] |
17 | | - }, |
18 | | - // base: import.meta.env.VITE_BASE_URL || '/webui/', |
19 | | - base: webuiPrefix, |
20 | | - build: { |
21 | | - outDir: path.resolve(__dirname, '../lightrag/api/webui'), |
22 | | - emptyOutDir: true, |
23 | | - chunkSizeWarningLimit: 3800, |
24 | | - rollupOptions: { |
25 | | - // Let Vite handle chunking automatically to avoid circular dependency issues |
26 | | - output: { |
27 | | - // Ensure consistent chunk naming format |
28 | | - chunkFileNames: 'assets/[name]-[hash].js', |
29 | | - // Entry file naming format |
30 | | - entryFileNames: 'assets/[name]-[hash].js', |
31 | | - // Asset file naming format |
32 | | - assetFileNames: 'assets/[name]-[hash].[ext]' |
| 28 | + // base: env.VITE_BASE_URL || '/webui/', |
| 29 | + base: webuiPrefix, |
| 30 | + build: { |
| 31 | + outDir: path.resolve(__dirname, '../lightrag/api/webui'), |
| 32 | + emptyOutDir: true, |
| 33 | + chunkSizeWarningLimit: 3800, |
| 34 | + rollupOptions: { |
| 35 | + // Let Vite handle chunking automatically to avoid circular dependency issues |
| 36 | + output: { |
| 37 | + // Ensure consistent chunk naming format |
| 38 | + chunkFileNames: 'assets/[name]-[hash].js', |
| 39 | + // Entry file naming format |
| 40 | + entryFileNames: 'assets/[name]-[hash].js', |
| 41 | + // Asset file naming format |
| 42 | + assetFileNames: 'assets/[name]-[hash].[ext]' |
| 43 | + } |
33 | 44 | } |
| 45 | + }, |
| 46 | + server: { |
| 47 | + proxy: env.VITE_API_PROXY === 'true' && env.VITE_API_ENDPOINTS ? |
| 48 | + Object.fromEntries( |
| 49 | + env.VITE_API_ENDPOINTS.split(',').map(endpoint => [ |
| 50 | + endpoint, |
| 51 | + { |
| 52 | + target: env.VITE_BACKEND_URL || 'http://localhost:9621', |
| 53 | + changeOrigin: true, |
| 54 | + rewrite: endpoint === '/api' ? |
| 55 | + (p: string) => p.replace(/^\/api/, '') : |
| 56 | + endpoint === '/docs' || endpoint === '/redoc' || endpoint === '/openapi.json' || endpoint === '/static' ? |
| 57 | + (p: string) => p : undefined |
| 58 | + } |
| 59 | + ]) |
| 60 | + ) : {} |
34 | 61 | } |
35 | | - }, |
36 | | - server: { |
37 | | - proxy: import.meta.env.VITE_API_PROXY === 'true' && import.meta.env.VITE_API_ENDPOINTS ? |
38 | | - Object.fromEntries( |
39 | | - import.meta.env.VITE_API_ENDPOINTS.split(',').map(endpoint => [ |
40 | | - endpoint, |
41 | | - { |
42 | | - target: import.meta.env.VITE_BACKEND_URL || 'http://localhost:9621', |
43 | | - changeOrigin: true, |
44 | | - rewrite: endpoint === '/api' ? |
45 | | - (path) => path.replace(/^\/api/, '') : |
46 | | - endpoint === '/docs' || endpoint === '/redoc' || endpoint === '/openapi.json' || endpoint === '/static' ? |
47 | | - (path) => path : undefined |
48 | | - } |
49 | | - ]) |
50 | | - ) : {} |
51 | 62 | } |
52 | 63 | }) |
0 commit comments