LibreChat/client/vite.config.ts
Danny Avila 23237255d8
chore: bump vite to v7 (#12031)
* 🔧 chore: Update @vitejs/plugin-react to version 5.1.4 and clean up package-lock.json

- Upgraded @vitejs/plugin-react from version 4.3.4 to 5.1.4 in both package.json and package-lock.json.
- Removed unused dependencies related to previous plugin versions from package-lock.json.
- Updated @babel/compat-data to version 7.29.0 and added new dependencies for Babel plugins.

* 🔧 chore: Upgrade vite-plugin-pwa to version 1.2.0 in package.json and package-lock.json

- Updated vite-plugin-pwa from version 0.21.2 to 1.2.0 in both package.json and package-lock.json to ensure compatibility with the latest features and improvements.
- Removed outdated dependency entries related to the previous version from package-lock.json.

* 🔧 chore: Upgrade vite to version 7.3.1 in package.json and package-lock.json

- Updated vite from version 6.4.1 to 7.3.1 in both package.json and package-lock.json to leverage new features and improvements.
- Added new esbuild packages for various architectures in package-lock.json to support broader compatibility.

* 🔧 chore: Update @babel dependencies and vite-plugin-node-polyfills version in package.json and package-lock.json

- Upgraded vite-plugin-node-polyfills from version 0.23.0 to 0.25.0 for improved compatibility.
- Added several new @babel packages and updated existing ones to version 7.29.0 and 7.28.6, enhancing Babel's functionality and support.
- Removed outdated semver entries from package-lock.json to streamline dependencies.

* 🔧 chore: Vite configuration with node polyfills resolver and clean up imports

- Added a custom resolver for node polyfills shims to improve compatibility with legacy modules.
- Cleaned up import statements by removing unnecessary comments and organizing imports for better readability.
- Utilized `createRequire` to handle module resolution in a more efficient manner.

* 🔧 chore: Upgrade fast-xml-parser to version 5.3.8 in package.json and package-lock.json

- Updated fast-xml-parser from version 5.3.6 to 5.3.8 in both package.json and package-lock.json to incorporate the latest features and improvements.
- Ensured consistency across dependencies by aligning the version in all relevant files.

* 🔧 chore: Upgrade @types/node to version 20.19.35 in package.json and package-lock.json

- Updated @types/node from version 20.3.0 to 20.19.35 in both package.json and package-lock.json to ensure compatibility with the latest TypeScript features and improvements.

* 🔧 chore: Vite configuration to centralize node polyfills shims

- Moved node polyfills shims into a dedicated constant for improved readability and maintainability.
- Updated the custom resolver to utilize the new centralized shims, enhancing compatibility with legacy modules.
- Added documentation to clarify the purpose of the node polyfills shims mapping.
2026-03-03 10:25:10 -05:00

334 lines
11 KiB
TypeScript

import react from '@vitejs/plugin-react';
import path from 'path';
import { defineConfig } from 'vite';
import { createRequire } from 'module';
import { VitePWA } from 'vite-plugin-pwa';
import { compression } from 'vite-plugin-compression2';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import type { Plugin } from 'vite';
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 7'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<string, string> = {
'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;
const backendURL = process.env.HOST
? `http://${process.env.HOST}:${backendPort}`
: `http://localhost:${backendPort}`;
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_'],
plugins: [
react(),
{
name: 'node-polyfills-shims-resolver',
resolveId(id) {
return NODE_POLYFILL_SHIMS[id] ?? null;
},
},
nodePolyfills(),
VitePWA({
injectRegister: 'auto', // 'auto' | 'manual' | 'disabled'
registerType: 'autoUpdate', // 'prompt' | 'autoUpdate'
devOptions: {
enabled: false, // disable service worker registration in development mode
},
useCredentials: true,
includeManifestIcons: false,
workbox: {
globPatterns: [
'**/*.{js,css,html}',
'assets/favicon*.png',
'assets/icon-*.png',
'assets/apple-touch-icon*.png',
'assets/maskable-icon.png',
'manifest.webmanifest',
],
globIgnores: ['images/**/*', '**/*.map', 'index.html'],
maximumFileSizeToCacheInBytes: 4 * 1024 * 1024,
navigateFallbackDenylist: [/^\/oauth/, /^\/api/],
},
includeAssets: [],
manifest: {
name: 'LibreChat',
short_name: 'LibreChat',
display: 'standalone',
background_color: '#000000',
theme_color: '#009688',
icons: [
{
src: 'assets/favicon-32x32.png',
sizes: '32x32',
type: 'image/png',
},
{
src: 'assets/favicon-16x16.png',
sizes: '16x16',
type: 'image/png',
},
{
src: 'assets/apple-touch-icon-180x180.png',
sizes: '180x180',
type: 'image/png',
},
{
src: 'assets/icon-192x192.png',
sizes: '192x192',
type: 'image/png',
},
{
src: 'assets/maskable-icon.png',
sizes: '512x512',
type: 'image/png',
purpose: 'maskable',
},
],
},
}),
sourcemapExclude({ excludeNodeModules: true }),
compression({
threshold: 10240,
}),
],
publicDir: command === 'serve' ? './public' : false,
build: {
sourcemap: process.env.NODE_ENV === 'development',
outDir: './dist',
minify: 'terser',
rollupOptions: {
preserveEntrySignatures: 'strict',
output: {
manualChunks(id: string) {
const normalizedId = id.replace(/\\/g, '/');
if (normalizedId.includes('node_modules')) {
// High-impact chunking for large libraries
// IMPORTANT: mermaid and ALL its dependencies must be in the same chunk
// to avoid initialization order issues. This includes chevrotain, langium,
// dagre-d3-es, and their nested lodash-es dependencies.
if (
normalizedId.includes('mermaid') ||
normalizedId.includes('dagre-d3-es') ||
normalizedId.includes('chevrotain') ||
normalizedId.includes('langium') ||
normalizedId.includes('lodash-es')
) {
return 'mermaid';
}
if (normalizedId.includes('@codesandbox/sandpack')) {
return 'sandpack';
}
if (normalizedId.includes('react-virtualized')) {
return 'virtualization';
}
if (normalizedId.includes('i18next') || normalizedId.includes('react-i18next')) {
return 'i18n';
}
// Only regular lodash (not lodash-es which goes to mermaid chunk)
if (normalizedId.includes('/lodash/')) {
return 'utilities';
}
if (normalizedId.includes('date-fns')) {
return 'date-utils';
}
if (normalizedId.includes('@dicebear')) {
return 'avatars';
}
if (
normalizedId.includes('react-dnd') ||
normalizedId.includes('dnd-core') ||
normalizedId.includes('react-flip-toolkit') ||
normalizedId.includes('flip-toolkit')
) {
return 'react-interactions';
}
if (normalizedId.includes('react-hook-form')) {
return 'forms';
}
if (normalizedId.includes('react-router-dom')) {
return 'routing';
}
if (
normalizedId.includes('qrcode.react') ||
normalizedId.includes('@marsidev/react-turnstile')
) {
return 'security-ui';
}
if (normalizedId.includes('@codemirror/view')) {
return 'codemirror-view';
}
if (normalizedId.includes('@codemirror/state')) {
return 'codemirror-state';
}
if (normalizedId.includes('@codemirror/language')) {
return 'codemirror-language';
}
if (normalizedId.includes('@codemirror')) {
return 'codemirror-core';
}
if (
normalizedId.includes('react-markdown') ||
normalizedId.includes('remark-') ||
normalizedId.includes('rehype-')
) {
return 'markdown-processing';
}
if (normalizedId.includes('monaco-editor') || normalizedId.includes('@monaco-editor')) {
return 'code-editor';
}
if (normalizedId.includes('react-window') || normalizedId.includes('react-virtual')) {
return 'virtualization';
}
if (
normalizedId.includes('zod') ||
normalizedId.includes('yup') ||
normalizedId.includes('joi')
) {
return 'validation';
}
if (
normalizedId.includes('axios') ||
normalizedId.includes('ky') ||
normalizedId.includes('fetch')
) {
return 'http-client';
}
if (
normalizedId.includes('react-spring') ||
normalizedId.includes('react-transition-group')
) {
return 'animations';
}
if (normalizedId.includes('react-select') || normalizedId.includes('downshift')) {
return 'advanced-inputs';
}
if (normalizedId.includes('heic-to')) {
return 'heic-converter';
}
// Existing chunks
if (normalizedId.includes('@radix-ui')) {
return 'radix-ui';
}
if (normalizedId.includes('framer-motion')) {
return 'framer-motion';
}
if (
normalizedId.includes('node_modules/highlight.js') ||
normalizedId.includes('node_modules/lowlight')
) {
return 'markdown_highlight';
}
if (normalizedId.includes('katex') || normalizedId.includes('node_modules/katex')) {
return 'math-katex';
}
if (normalizedId.includes('node_modules/hast-util-raw')) {
return 'markdown_large';
}
if (normalizedId.includes('@tanstack')) {
return 'tanstack-vendor';
}
if (normalizedId.includes('@headlessui')) {
return 'headlessui';
}
// Everything else falls into a generic vendor chunk.
return 'vendor';
}
// Create a separate chunk for all locale files under src/locales.
if (normalizedId.includes('/src/locales/')) {
return 'locales';
}
// Let Rollup decide automatically for any other files.
return null;
},
entryFileNames: 'assets/[name].[hash].js',
chunkFileNames: 'assets/[name].[hash].js',
assetFileNames: (assetInfo) => {
if (assetInfo.names?.[0] && /\.(woff|woff2|eot|ttf|otf)$/.test(assetInfo.names[0])) {
return 'assets/fonts/[name][extname]';
}
return 'assets/[name].[hash][extname]';
},
},
/**
* Ignore "use client" warning since we are not using SSR
* @see {@link https://github.com/TanStack/query/pull/5161#issuecomment-1477389761 Preserve 'use client' directives TanStack/query#5161}
*/
onwarn(warning, warn) {
if (warning.message.includes('Error when using sourcemap')) {
return;
}
warn(warning);
},
},
chunkSizeWarningLimit: 1500,
},
resolve: {
alias: {
'~': path.join(__dirname, 'src/'),
$fonts: path.resolve(__dirname, 'public/fonts'),
'micromark-extension-math': 'micromark-extension-llm-math',
},
},
}));
interface SourcemapExclude {
excludeNodeModules?: boolean;
}
export function sourcemapExclude(opts?: SourcemapExclude): Plugin {
return {
name: 'sourcemap-exclude',
transform(code: string, id: string) {
if (opts?.excludeNodeModules && id.includes('node_modules')) {
return {
code,
// https://github.com/rollup/rollup/blob/master/docs/plugin-development/index.md#source-code-transformations
map: { mappings: '' },
};
}
},
};
}