Files
openchamber/packages/web/vite.config.ts
T
Cursor AgentandSerhii Dziupin 3d27c0a0ca perf(build): fix bun .bun chunk parsing and isolate Vite preload helper
The manualChunks package-name parser used the first node_modules/ segment,
which under bun's isolated install (.bun/<pkg>@<ver>/node_modules/<pkg>) is
always '.bun'. This collapsed nearly every dependency — including lazy-only
heavy libs (Shiki langs/themes, transformers, katex, mermaid, elkjs, etc.) —
into one 18.5 MB eager 'vendor-.bun' chunk, defeating all code splitting.

- Resolve the real package from the LAST node_modules/ segment.
- Pin Vite's __vitePreload helper to its own chunk so Rollup stops
  co-locating it inside vendor-shiki and dragging Shiki core + the 629 KB
  oniguruma engine into the eager bootstrap.
- Drop the now-dead react-syntax-highlighter chunk rule.

Eager bootstrap graph drops from ~18.5 MB to ~0.44 MB.

Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com>
2026-06-26 11:26:11 +00:00

148 lines
5.4 KiB
TypeScript

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFileSync } from 'node:fs';
import { VitePWA } from 'vite-plugin-pwa';
import { themeStoragePlugin } from '../../vite-theme-plugin';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const packageJson = JSON.parse(readFileSync(path.resolve(__dirname, 'package.json'), 'utf-8'));
const pwaDevEnabled = process.env.OPENCHAMBER_DISABLE_PWA_DEV !== '1';
const reactScanToggle = (process.env.VITE_ENABLE_REACT_SCAN ?? '').toLowerCase();
const enableReactScan = reactScanToggle === '1' || reactScanToggle === 'true' || reactScanToggle === 'on' || reactScanToggle === 'yes';
export default defineConfig({
root: path.resolve(__dirname, '.'),
plugins: [
react({
babel: {
plugins: ['babel-plugin-react-compiler'],
},
}),
{
name: 'inject-react-scan-script',
transformIndexHtml() {
if (!enableReactScan) {
return;
}
return [
{
tag: 'script',
attrs: {
crossorigin: 'anonymous',
src: '//unpkg.com/react-scan/dist/auto.global.js',
},
injectTo: 'head-prepend',
},
];
},
},
themeStoragePlugin(),
VitePWA({
strategies: 'injectManifest',
srcDir: 'src',
filename: 'sw.ts',
registerType: 'autoUpdate',
injectRegister: false,
manifest: false,
injectManifest: {
globPatterns: ['**/*.{js,css,html,ico,png,svg,woff,woff2,ttf,otf,eot}'],
// iOS Safari/PWA is much more reliable with a classic (non-module) SW bundle.
rollupFormat: 'iife',
// We already keep a custom manifest in index.html
injectionPoint: undefined,
},
devOptions: {
enabled: pwaDevEnabled,
type: 'module',
},
}),
],
resolve: {
alias: [
{ find: '@opencode-ai/sdk/v2', replacement: path.resolve(__dirname, '../../node_modules/@opencode-ai/sdk/dist/v2/client.js') },
{ find: '@openchamber/ui', replacement: path.resolve(__dirname, '../ui/src') },
{ find: '@web', replacement: path.resolve(__dirname, './src') },
{ find: '@', replacement: path.resolve(__dirname, '../ui/src') },
],
},
worker: {
format: 'es',
},
define: {
'process.env': {},
global: 'globalThis',
__APP_VERSION__: JSON.stringify(packageJson.version),
},
optimizeDeps: {
include: ['@opencode-ai/sdk/v2'],
},
server: {
port: 5173,
proxy: {
'/auth': {
target: `http://127.0.0.1:${process.env.OPENCHAMBER_PORT || 3001}`,
changeOrigin: true,
},
'/health': {
target: `http://127.0.0.1:${process.env.OPENCHAMBER_PORT || 3001}`,
changeOrigin: true,
},
'/api': {
target: `http://127.0.0.1:${process.env.OPENCHAMBER_PORT || 3001}`,
changeOrigin: true,
ws: true,
},
},
},
build: {
outDir: path.resolve(__dirname, 'dist'),
emptyOutDir: true,
chunkSizeWarningLimit: 500,
rollupOptions: {
input: {
main: path.resolve(__dirname, 'index.html'),
mobile: path.resolve(__dirname, 'mobile.html'),
miniChat: path.resolve(__dirname, 'mini-chat.html'),
},
external: ['node:child_process', 'node:fs', 'node:path', 'node:url'],
output: {
manualChunks(id) {
// Pin Vite's tiny runtime helpers to their own stable chunk. Otherwise
// Rollup co-locates the `__vitePreload` helper into an arbitrary vendor
// chunk (e.g. `shiki`), and since every dynamic import pulls the helper,
// that whole vendor (here Shiki core + the 629KB oniguruma engine) gets
// dragged into the eager bootstrap graph.
if (id.includes('vite/preload-helper') || id.includes('vite/modulepreload-polyfill')) {
return 'vendor-vite-runtime';
}
if (!id.includes('node_modules')) return undefined;
// Resolve the real package from the LAST `node_modules/` segment.
// bun's isolated install nests packages as
// `node_modules/.bun/<pkg>@<ver>/node_modules/<pkg>/...`, so the first
// `node_modules/` segment is `.bun` — using it collapses every dependency
// (incl. lazy-only ones) into a single giant eager `vendor-.bun` chunk.
const lastNodeModules = id.lastIndexOf('node_modules/');
const match = id.slice(lastNodeModules + 'node_modules/'.length);
if (!match) return undefined;
const segments = match.split('/');
const packageName = match.startsWith('@') ? `${segments[0]}/${segments[1]}` : segments[0];
if (packageName === 'react' || packageName === 'react-dom') return 'vendor-react';
if (packageName === 'zustand' || packageName === 'zustand/middleware') return 'vendor-zustand';
if (packageName === '@opencode-ai/sdk') return 'vendor-opencode-sdk';
if (packageName.includes('remark') || packageName.includes('rehype') || packageName === 'react-markdown') return 'vendor-markdown';
if (packageName === '@base-ui/react' || packageName.startsWith('@base-ui')) return 'vendor-base-ui';
const sanitized = packageName.replace(/^@/, '').replace(/\//g, '-');
return `vendor-${sanitized}`;
},
},
},
},
});