import react from '@vitejs/plugin-react'; import fs from 'fs'; import path from 'path'; import { constants } from 'zlib'; import { defineConfig } from 'vite'; import { Features } from 'lightningcss'; import { THEME_BOOT_FILE } from 'librechat-data-provider'; import { createRequire } from 'module'; import { VitePWA } from 'vite-plugin-pwa'; import { nodePolyfills } from 'vite-plugin-node-polyfills'; import { compression, defineAlgorithm } from 'vite-plugin-compression2'; import type { Plugin, Rollup } from 'vite'; import { sandpackConsoleIsolation } from './src/lib/sandpack.ts'; import { bundledThemeBoot, injectThemeCacheVersion } from './src/Providers/bootVersion.ts'; import { localeAliases, resolveLocale, supportedLocales } from './src/locales/resolve.ts'; const require = createRequire(import.meta.url); /** * vite-plugin-node-polyfills uses @rollup/plugin-inject to replace bare globals (e.g. `process`) * with imports like `import process from 'vite-plugin-node-polyfills/shims/process'`. When the * consuming module (e.g. recoil) is hoisted to the monorepo root, Vite's ESM resolver walks up * from there and never finds the shims (installed only in client/node_modules). This map resolves * the shim specifiers to absolute paths via CJS require.resolve anchored to the client directory. */ const NODE_POLYFILL_SHIMS: Record = { 'vite-plugin-node-polyfills/shims/process': require.resolve( 'vite-plugin-node-polyfills/shims/process', ), 'vite-plugin-node-polyfills/shims/buffer': require.resolve( 'vite-plugin-node-polyfills/shims/buffer', ), 'vite-plugin-node-polyfills/shims/global': require.resolve( 'vite-plugin-node-polyfills/shims/global', ), }; // https://vitejs.dev/config/ const backendPort = (process.env.BACKEND_PORT && Number(process.env.BACKEND_PORT)) || 3080; /** IPv6 hosts arrive unbracketed (valid for the listen address) but must be * bracketed inside a URL, or the proxy target parses as host `:` port soup. */ const backendHost = process.env.HOST?.includes(':') ? `[${process.env.HOST}]` : process.env.HOST; const backendURL = backendHost ? `http://${backendHost}:${backendPort}` : `http://localhost:${backendPort}`; const buildSourceMap = process.env.NODE_ENV === 'development'; const QUERY_DEVTOOLS_CHUNK_MODULES = [ '@tanstack/react-query-devtools', '@tanstack/match-sorter-utils', 'node_modules/superjson', 'node_modules/copy-anything', 'node_modules/is-what', ]; export default defineConfig(({ command }) => ({ base: '', server: { allowedHosts: (process.env.VITE_ALLOWED_HOSTS && process.env.VITE_ALLOWED_HOSTS.split(',')) || [], host: process.env.HOST || 'localhost', port: (process.env.PORT && Number(process.env.PORT)) || 3090, strictPort: false, proxy: { '/api': { target: backendURL, changeOrigin: true, }, '/oauth': { target: backendURL, changeOrigin: true, }, }, }, // Set the directory where environment variables are loaded from and restrict prefixes envDir: '../', envPrefix: ['VITE_', 'SCRIPT_', 'DOMAIN_', 'ALLOW_', 'REACT_APP_THEME_'], plugins: [ sandpackConsoleIsolation(), react(), { name: 'node-polyfills-shims-resolver', resolveId(id) { return NODE_POLYFILL_SHIMS[id] ?? null; }, }, nodePolyfills(), { name: 'emit-sw-heal', apply: 'build', generateBundle() { this.emitFile({ type: 'asset', fileName: 'sw-heal.js', source: fs.readFileSync(path.resolve(import.meta.dirname, 'sw/heal.js'), 'utf8'), }); }, }, themeCacheVersion(), bootLoadingLabels(), lazyChunkGuard(), copyPublicAssets(), VitePWA({ /** `auto` emits a parser-blocking `