const NODE_MODULES_SEGMENT = "/node_modules/"; const CHUNK_MATCHERS: Array<{ chunkName: string; packages: string[] }> = [ { chunkName: "vendor-react", packages: ["react", "react-dom", "react-router", "scheduler", "use-sync-external-store"] }, { chunkName: "vendor-core-utils", packages: ["clsx", "tailwind-merge", "class-variance-authority"] }, // Keep the shared TypeScript runtime helpers out of lazily loaded vendor // chunks. Otherwise Rollup can pull a 1+ MiB AWS chunk into SSR startup just // because the main bundle also needs `__assign`/`__extends`. { chunkName: "vendor-tslib", packages: ["tslib"] }, { chunkName: "vendor-charts", packages: ["recharts", "victory-vendor", "d3-", "d3"] }, { chunkName: "vendor-markdown", packages: ["marked", "katex"] }, // Monaco is already behind a client-only dynamic import. Let Rollup keep // that complete dependency graph at the lazy boundary: assigning it to a // manual chunk can absorb Vite's preload helper and make the editor part of // every route's static graph. { chunkName: "vendor-editor-tiptap", packages: ["@tiptap/"] }, { chunkName: "vendor-editor-prosemirror", packages: ["prosemirror-"] }, { chunkName: "vendor-pdf", packages: ["@react-pdf/renderer"] }, { chunkName: "vendor-workflow", packages: ["@xyflow/react"] }, { chunkName: "vendor-aws-sdk", packages: ["@aws-sdk/", "@smithy/"] }, // Lighthouse-CI (#70) — splits the React-Email render path off the // critical render bundle so transactional-email pages don't bloat the // marketing TTI score. Matches the budget declared in scripts/bundle-budgets.json. { chunkName: "vendor-react-email", packages: ["@react-email/", "react-email"] }, ]; const normalizeModuleId = (id: string) => id.replace(/\\/g, "/"); const hasPackageBoundary = (moduleId: string, packageName: string) => { const packageRoot = `node_modules/${packageName}`; if (packageName.endsWith("/") || packageName.endsWith("-")) { return moduleId.includes(packageRoot); } return moduleId.includes(`${packageRoot}/`) || moduleId.endsWith(packageRoot); }; export function getManualChunkName(id: string): string | undefined { const normalizedId = normalizeModuleId(id); if (!normalizedId.includes(NODE_MODULES_SEGMENT)) { return undefined; } for (const matcher of CHUNK_MATCHERS) { if (matcher.packages.some((packageName) => hasPackageBoundary(normalizedId, packageName))) { return matcher.chunkName; } } return undefined; } export function getChunkFileName(moduleIds: readonly string[]): string { const normalizedIds = moduleIds.map(normalizeModuleId); if (normalizedIds.some((id) => id.includes("/node_modules/monaco-editor/") || id.includes("/node_modules/@monaco-editor/react/"))) { return "assets/lazy-monaco-[name]-[hash].js"; } if (normalizedIds.some((id) => id.includes("/node_modules/pdfjs-dist/"))) { return "assets/lazy-pdfjs-[name]-[hash].js"; } return "assets/[name]-[hash].js"; }