diff --git a/.alexignore b/.alexignore index 970f816cffa8..5d2560c0d0a6 100644 --- a/.alexignore +++ b/.alexignore @@ -3,5 +3,4 @@ examples/ **/*/LICENSE.md .claude/ AGENTS.md -CLAUDE.md packages/next/dist/docs/ diff --git a/.github/CLAUDE.md b/.github/CLAUDE.md deleted file mode 120000 index 47dc3e3d863c..000000000000 --- a/.github/CLAUDE.md +++ /dev/null @@ -1 +0,0 @@ -AGENTS.md \ No newline at end of file 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..15843729966c 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, @@ -16,7 +17,6 @@ import { import { delta, formatDelta, - sortByImpact, type DiffRow, type DiffSummary, type DiffStatus, @@ -29,6 +29,7 @@ import { Badge } from '@/components/ui/badge' type SortColumn = 'name' | 'a' | 'b' | 'delta' /** Sort direction. */ type SortDirection = 'asc' | 'desc' +type StatusFilter = DiffStatus | 'changes' | 'all' interface DiffTableProps { summary: DiffSummary @@ -74,13 +75,31 @@ 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: * * | Name | A (before) | B (after) | Δ | * - * Rows are sorted by absolute delta (largest impact first). + * In compare mode, rows are sorted by signed delta (largest increase first). */ export function DiffTable({ summary, @@ -95,9 +114,11 @@ export function DiffTable({ searchQuery, }: DiffTableProps) { const isSingle = mode === 'single' - const [statusFilter, setStatusFilter] = useState('all') + // Compare mode shows only changed sources by default. In single mode every + // row is `identical` against itself, so the status filter is ignored. + const [statusFilter, setStatusFilter] = useState('changes') // Default sort: - // - compare mode: rank by largest absolute delta (most-impactful change first). + // - compare mode: rank by signed delta (largest increase first). // - single mode: rank by size descending (largest contributors first). const [sortColumn, setSortColumn] = useState( isSingle ? 'b' : 'delta' @@ -118,16 +139,10 @@ export function DiffTable({ }) } - const sorted = useMemo(() => { - // The default `delta`/`desc` mode preserves the existing impact-ranked - // sort, which keeps `identical` rows pinned at the bottom regardless of - // direction. Other columns sort lexicographically (name) or numerically - // (A, B) using the user's chosen direction. - if (sortColumn === 'delta' && sortDirection === 'desc') { - return sortByImpact(summary.rows, useCompressed) - } - return sortRows(summary.rows, sortColumn, sortDirection, useCompressed) - }, [summary.rows, sortColumn, sortDirection, useCompressed]) + const sorted = useMemo( + () => sortRows(summary.rows, sortColumn, sortDirection, useCompressed), + [summary.rows, sortColumn, sortDirection, useCompressed] + ) const onHeaderClick = (column: SortColumn) => { if (column === sortColumn) { @@ -143,7 +158,13 @@ export function DiffTable({ const filtered = useMemo(() => { const q = searchQuery?.trim().toLowerCase() ?? '' return sorted.filter((row) => { - if (statusFilter !== 'all' && row.status !== statusFilter) return false + if (!isSingle && statusFilter !== 'all') { + if (statusFilter === 'changes' && row.status === 'identical') + return false + if (statusFilter !== 'changes' && row.status !== statusFilter) { + return false + } + } if (q !== '') { const key = row.key.toLowerCase() const name = row.name.toLowerCase() @@ -151,7 +172,7 @@ export function DiffTable({ } return true }) - }, [sorted, statusFilter, searchQuery]) + }, [sorted, isSingle, statusFilter, searchQuery]) // Group filtered rows by package, preserving the user's chosen sort order. // Groups themselves are sorted by their aggregate using the same column + @@ -162,163 +183,139 @@ 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 ? ( +
+ {!isSingle && statusFilter === 'changes' && !searchQuery?.trim() + ? 'No changes to show.' + : 'No matching rows.'} +
+ ) : null}
) } @@ -372,16 +369,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 +416,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 +430,73 @@ 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.toSorted((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 = (aValue - bValue) * sign + if (comparison !== 0) return comparison + } + + // Break numeric ties using the same locale-aware order as the name column. + return a.name.localeCompare(b.name) + }) +} + +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 +534,15 @@ function DiffPackageHeaderRow({ : 'changed' return ( - - + <> + {isExpanded ? ( ({ /> )} {!isSingle ? : null} - + {packageName} @@ -500,26 +574,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} - + ) } @@ -573,9 +654,9 @@ function PackageCountBreakdown({ /** * Sorts rows by the chosen column and direction. The `name` column compares * using a locale-aware lexicographic ordering; numeric columns compare by - * value. `identical` rows are always pinned to the bottom — they aren't part - * of the "what changed" story and would otherwise dominate the top of an - * ascending sort. + * value. `identical` rows are pinned to the bottom for every column when + * visible (in the All filter or single mode), since they aren't part of the + * "what changed" story. */ function sortRows( rows: Row[], @@ -590,7 +671,7 @@ function sortRows( if (column === 'b') return useCompressed ? row.compressedB : row.sizeB return delta(row, useCompressed) } - return [...rows].sort((a, b) => { + return rows.toSorted((a, b) => { if (a.status === 'identical' && b.status !== 'identical') return 1 if (b.status === 'identical' && a.status !== 'identical') return -1 const av = compareKey(a) @@ -647,6 +728,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 +837,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 +953,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 @@ -922,17 +1012,18 @@ function DiffStatusFilter({ onChange, counts, }: { - value: DiffStatus | 'all' - onChange: (value: DiffStatus | 'all') => void + value: StatusFilter + onChange: (value: StatusFilter) => void counts: Record }) { const total = counts.added + counts.removed + counts.changed + counts.identical const options: Array<{ - value: DiffStatus | 'all' + value: StatusFilter label: string count: number }> = [ + { value: 'changes', label: 'Changes', count: total - counts.identical }, { value: 'all', label: 'All', count: total }, { value: 'added', label: 'Added', count: counts.added }, { value: 'removed', label: 'Removed', count: counts.removed }, 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 (