From 2319b65b715e49c66fc036062e5c49a3a77e07ca Mon Sep 17 00:00:00 2001 From: Will Binns-Smith Date: Mon, 28 Sep 2026 09:33:03 -0700 Subject: [PATCH 01/13] Virtualize bundle analyzer source table (#98539) Virtualize source rows with `react-virtuoso` to avoid mounting the full table at once. Preserve sorting, filtering, grouped package expansion, sticky headers, totals, selection, and semantic table markup. Render an additional viewport around the visible range to avoid blank content during fast scrolling. Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com> Co-authored-by: Will Binns-Smith <755844+wbinnssmith@users.noreply.github.com> --- apps/bundle-analyzer/components/analyzer.tsx | 15 +- .../bundle-analyzer/components/diff-table.tsx | 432 +++++++++++------- .../components/ui/skeleton.tsx | 65 ++- apps/bundle-analyzer/package.json | 1 + pnpm-lock.yaml | 14 + 5 files changed, 340 insertions(+), 187 deletions(-) diff --git a/apps/bundle-analyzer/components/analyzer.tsx b/apps/bundle-analyzer/components/analyzer.tsx index 166995351635..f54a399830d5 100644 --- a/apps/bundle-analyzer/components/analyzer.tsx +++ b/apps/bundle-analyzer/components/analyzer.tsx @@ -22,7 +22,11 @@ import { TopBar, Environment, CompareView } from '@/components/top-bar' import { TreemapVisualizer } from '@/components/treemap-visualizer' import { Badge } from '@/components/ui/badge' -import { TableSkeleton, TreemapSkeleton } from '@/components/ui/skeleton' +import { + AnalyzerChromeSkeleton, + TableSkeleton, + TreemapSkeleton, +} from '@/components/ui/skeleton' import { AnalyzeData, ModulesData } from '@/lib/analyze-data' import { analyzeDataUrl, @@ -109,14 +113,7 @@ class AnalyzerErrorBoundary extends Component< } function AnalyzerFallback({ view }: { view: CompareView }) { - return ( -
-
-
- {view === CompareView.Table ? : } -
-
- ) + return } function useAnalyzerModel(compare: boolean) { diff --git a/apps/bundle-analyzer/components/diff-table.tsx b/apps/bundle-analyzer/components/diff-table.tsx index eb83b49bafaf..b09b77e1120f 100644 --- a/apps/bundle-analyzer/components/diff-table.tsx +++ b/apps/bundle-analyzer/components/diff-table.tsx @@ -1,6 +1,7 @@ 'use client' -import { useMemo, useState, type ReactNode } from 'react' +import { useMemo, useState, type ComponentProps, type ReactNode } from 'react' +import { TableVirtuoso } from 'react-virtuoso' import { ArrowDown, ArrowUp, @@ -74,6 +75,24 @@ interface DiffTableProps { searchQuery?: string } +const virtuosoComponents = { + Table: ({ children, style }: ComponentProps<'table'>) => ( + + {children} +
+ ), + TableHead: ({ children, style, ref }: ComponentProps<'thead'>) => ( + + {children} + + ), + TableFoot: ({ children, style, ref }: ComponentProps<'tfoot'>) => ( + + {children} + + ), +} + /** * Compact table comparing two builds, modeled on the vite-compare module-diff * view. Columns: @@ -162,163 +181,137 @@ export function DiffTable({ () => buildRenderItems(filtered, sortColumn, sortDirection, useCompressed), [filtered, sortColumn, sortDirection, useCompressed] ) + const visibleItems = useMemo( + () => expandRenderItems(renderItems, expandedPackages), + [renderItems, expandedPackages] + ) const totalDelta = useCompressed ? summary.totalCompressedB - summary.totalCompressedA : summary.totalB - summary.totalA return ( -
+
{caption ? (
{caption}
) : null} -
- - - + ( + + + ) : null + } + /> + {!isSingle ? ( - ) : null - } + title="Historical build (before)" + align="right" /> - {!isSingle ? ( - - ) : null} + ) : null} + + {!isSingle ? ( - {!isSingle ? ( - - ) : null} - - - - {renderItems.flatMap((item) => { - if (item.kind === 'group') { - const isExpanded = expandedPackages.has(item.packageName) - const header = ( - togglePackage(item.packageName)} - mode={mode} - /> - ) - if (!isExpanded) return [header] - return [ - header, - ...item.rows.map((row) => ( - onRowSelect(row) : undefined} - isSelected={selectedKey === row.key} - mode={mode} - /> - )), - ] - } - return [ - onRowSelect(item.row) : undefined - } - isSelected={selectedKey === item.row.key} - mode={mode} - />, - ] - })} - {renderItems.length === 0 ? ( - - - ) : null} - - - {/* - Footer cells match the body's `text-xs` + `whitespace-nowrap` - so size totals (e.g. `259.74 KB`) can't wrap — at `text-sm` - the unit broke onto a second line in compare mode where the - numbers are larger. - */} - - - {!isSingle ? ( - - ) : null} + + )} + itemContent={(_, item) => { + if (item.kind === 'group') { + return ( + togglePackage(item.packageName)} + mode={mode} + /> + ) + } + return ( + onRowSelect(item.row) : undefined} + isSelected={selectedKey === item.row.key} + mode={mode} + /> + ) + }} + fixedFooterContent={() => ( + + + {!isSingle ? ( - {!isSingle ? ( - - ) : null} - - -
- No matching rows. -
Total - {formatBytes( - useCompressed ? summary.totalCompressedA : summary.totalA - )} -
Total {formatBytes( - useCompressed ? summary.totalCompressedB : summary.totalB + useCompressed ? summary.totalCompressedA : summary.totalA )} 0 && 'text-red-600 dark:text-red-400', - totalDelta < 0 && 'text-green-600 dark:text-green-400' - )} - > - {formatDelta(totalDelta)} -
-
+ ) : null} + + {formatBytes( + useCompressed ? summary.totalCompressedB : summary.totalB + )} + + {!isSingle ? ( + 0 && 'text-red-600 dark:text-red-400', + totalDelta < 0 && 'text-green-600 dark:text-green-400' + )} + > + {formatDelta(totalDelta)} + + ) : null} + + )} + /> + {visibleItems.length === 0 ? ( +
+ No matching rows. +
+ ) : null}
) } @@ -372,16 +365,25 @@ function aggregatePackage(rows: Row[]): PackageAggregate { return agg } +function packageFullPath( + packageName: string, + rows: Row[] +): string { + const fullPath = rows[0]?.key ?? packageName + const packageSegment = `node_modules/${packageName}` + const packageIndex = fullPath.lastIndexOf(packageSegment) + return packageIndex === -1 + ? fullPath + : fullPath.slice(0, packageIndex + packageSegment.length) +} + /** * Builds the flat list of items to render in ``. Source rows that * share a `packageName` are grouped under a single header item; project * rows are passed through as `row` items. * - * Ordering: groups and ungrouped rows are interleaved in the order their - * top contributor appeared in `filtered`. This means the existing column - * sort drives the overall ranking — a package's position is determined by - * its highest-impact member, which keeps the most interesting groups near - * the top regardless of which column is active. + * Ordering: groups and ungrouped rows are sorted together using each group's + * aggregate values. Children retain the same sort as the parent table. */ function buildRenderItems( filtered: Row[], @@ -410,8 +412,7 @@ function buildRenderItems( } // Ensure children within each group are sorted using the same column & - // direction as the parent table. The first member of each group already - // appeared in the right top-level position thanks to the pre-sorted input. + // direction as the parent table. if (groupIndex.size > 0) { for (const { rows } of groupIndex.values()) { const sortedChildren = sortRows( @@ -425,7 +426,75 @@ function buildRenderItems( } } - return items + return sortRenderItems(items, sortColumn, sortDirection, useCompressed) +} + +function sortRenderItems( + items: RenderItem[], + column: SortColumn, + direction: SortDirection, + useCompressed: boolean +): RenderItem[] { + const sign = direction === 'asc' ? 1 : -1 + const values = (item: RenderItem) => { + if (item.kind === 'row') { + const row = item.row + return { + name: row.name, + a: useCompressed ? row.compressedA : row.sizeA, + b: useCompressed ? row.compressedB : row.sizeB, + delta: delta(row, useCompressed), + identical: row.status === 'identical', + } + } + const aggregate = aggregatePackage(item.rows) + const a = useCompressed ? aggregate.compressedA : aggregate.sizeA + const b = useCompressed ? aggregate.compressedB : aggregate.sizeB + return { + name: item.packageName, + a, + b, + delta: b - a, + identical: a === b, + } + } + + return [...items].sort((left, right) => { + const a = values(left) + const b = values(right) + if (a.identical && !b.identical) return 1 + if (b.identical && !a.identical) return -1 + + if (column === 'name') { + const comparison = a.name.localeCompare(b.name) + if (comparison !== 0) return comparison * sign + } else { + const aValue = a[column] + const bValue = b[column] + const comparison = + column === 'delta' && direction === 'desc' + ? Math.abs(bValue) - Math.abs(aValue) + : (aValue - bValue) * sign + if (comparison !== 0) return comparison + } + + return a.name < b.name ? -1 : 1 + }) +} + +function expandRenderItems( + items: RenderItem[], + expandedPackages: ReadonlySet +): RenderItem[] { + return items.flatMap((item) => { + if (item.kind === 'row' || !expandedPackages.has(item.packageName)) { + return [item] + } + return [ + item, + ...item.rows.map((row) => ({ kind: 'row' as const, row, indent: true })), + ] + }) } /** @@ -463,15 +532,15 @@ function DiffPackageHeaderRow({ : 'changed' return ( - - + <> + {isExpanded ? ( ({ /> )} {!isSingle ? : null} - + {packageName} @@ -500,26 +572,33 @@ function DiffPackageHeaderRow({ {!isSingle ? ( - + {aggregateStatus === 'added' ? '—' : formatBytes(a)} ) : null} - + {aggregateStatus === 'removed' ? '—' : formatBytes(b)} {!isSingle ? ( 0 && 'text-red-600 dark:text-red-400', d < 0 && 'text-green-600 dark:text-green-400', d === 0 && 'text-muted-foreground' )} + onClick={onToggle} > {formatDelta(d)} ) : null} - + ) } @@ -647,6 +726,7 @@ function SortableHeader({ ({ const isSingle = mode === 'single' const d = delta(row, useCompressed) const isInteractive = onClick != null + const cellClassName = cn( + 'border-b border-border transition-colors', + isInteractive && 'cursor-pointer hover:bg-muted/50', + indent && 'bg-muted/10', + isSelected && 'bg-primary/10 hover:bg-primary/15' + ) return ( - + <> ({ /> {!isSingle ? ( - + {row.status === 'added' ? '—' : formatBytes(useCompressed ? row.compressedA : row.sizeA)} ) : null} - + {row.status === 'removed' ? '—' : formatBytes(useCompressed ? row.compressedB : row.sizeB)} @@ -744,15 +835,17 @@ function DiffTableRow({ 0 && 'text-red-600 dark:text-red-400', d < 0 && 'text-green-600 dark:text-green-400', d === 0 && 'text-muted-foreground' )} + onClick={onClick} > {formatDelta(d)} ) : null} - + ) } @@ -858,12 +951,7 @@ function EnvBadges({ client, server }: { client: boolean; server: boolean }) { return ( `s), so this is purely a height-stability fix. - 'ml-auto invisible inline-flex shrink-0 items-center gap-1 pl-2', + 'invisible inline-flex shrink-0 items-center gap-1', 'group-hover:visible' )} aria-hidden diff --git a/apps/bundle-analyzer/components/ui/skeleton.tsx b/apps/bundle-analyzer/components/ui/skeleton.tsx index 9bed1f2c94c9..e150f6ac9ed2 100644 --- a/apps/bundle-analyzer/components/ui/skeleton.tsx +++ b/apps/bundle-analyzer/components/ui/skeleton.tsx @@ -35,16 +35,18 @@ export function TreemapSkeleton() { export function TableSkeleton() { return (