Unverified Commit b16348a2 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Add bundle analyzer route summary (#98787)

### What?

Add a route summary homepage to the Turbopack bundle analyzer and move
individual route analysis to `/analyze`.

Precompute raw, compressed, and client bundle sizes for every route in
Rust and emit them in one `route-summaries.json` payload. Use that
payload to rank the largest client routes, label the route picker, and
calculate route deltas in comparisons.

### Why?

Opening the analyzer at `/` previously showed an empty treemap until a
route was selected. Loading route sizes also required fetching every
route's `analyze.data` payload.

### How?

- Show the 15 largest client payloads with progressive disclosure and
route search.
- Keep route rows as links so they support browser navigation and
opening in a new tab.
- Use dedicated loading UI for the summary page.
- Keep comparison controls on route analysis pages where a route is
selected.

<!-- NEXT_JS_LLM -->

---------
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent 9b963777
import { SingleAnalyzer } from '@/components/analyzer'
export default function AnalyzePage() {
return <SingleAnalyzer />
}
import { SingleAnalyzer } from '@/components/analyzer' import { RouteSummaryPage } from '@/components/route-summary'
export default function HomePage() { export default function HomePage() {
return <SingleAnalyzer /> return <RouteSummaryPage />
} }
...@@ -34,8 +34,7 @@ import { ...@@ -34,8 +34,7 @@ import {
useHistoryIndex, useHistoryIndex,
useSuspenseJsonData, useSuspenseJsonData,
} from '@/lib/analyzer-data' } from '@/lib/analyzer-data'
import { diffRoutesWithSizes, diffSources } from '@/lib/diff' import { diffRoutesWithSizes, diffSources, type RouteSummary } from '@/lib/diff'
import { useRouteTotals } from '@/lib/use-route-totals'
import { useSidebarResize } from '@/lib/use-sidebar-resize' import { useSidebarResize } from '@/lib/use-sidebar-resize'
import { useAnalyzerRoute } from '@/lib/use-analyzer-route' import { useAnalyzerRoute } from '@/lib/use-analyzer-route'
import { computeActiveEntries, computeModuleDepthMap } from '@/lib/module-graph' import { computeActiveEntries, computeModuleDepthMap } from '@/lib/module-graph'
...@@ -72,12 +71,14 @@ function CompareAnalyzerController() { ...@@ -72,12 +71,14 @@ function CompareAnalyzerController() {
function AnalyzerBoundary({ function AnalyzerBoundary({
children, children,
defaultView, defaultView,
fallback,
}: { }: {
children: ReactNode children: ReactNode
defaultView: CompareView defaultView: CompareView
fallback?: ReactNode
}) { }) {
return ( return (
<Suspense fallback={<AnalyzerFallback view={defaultView} />}> <Suspense fallback={fallback ?? <AnalyzerFallback view={defaultView} />}>
{children} {children}
</Suspense> </Suspense>
) )
...@@ -260,12 +261,30 @@ function useAnalyzerModel(compare: boolean) { ...@@ -260,12 +261,30 @@ function useAnalyzerModel(compare: boolean) {
}) })
}, [analyzeData, baselineSnapshot, filterSource]) }, [analyzeData, baselineSnapshot, filterSource])
// Per-route totals for both sides, used to size the route-level diff so const routeSummaries = useSuspenseJsonData<RouteSummary[]>(
// that routes whose modules changed can be reported as `changed` rather `${comparisonBaseDir}/route-summaries.json`,
// than `identical`. Only fetched in compare mode. { revalidateOnFocus: false, revalidateOnReconnect: false }
const { totals: currentRouteTotals } = useRouteTotals( )
baselineSnapshot ? currentRoutes : null, const clientRouteTotals = new Map(
baselineSnapshot ? comparisonBaseDir : null routeSummaries.map(({ route, client }) => [
route,
{ size: client.size, compressedSize: client.compressed_size },
])
)
const serverRouteTotals = new Map(
routeSummaries.map(({ route, size, compressed_size, client }) => [
route,
{
size: size - client.size,
compressedSize: compressed_size - client.compressed_size,
},
])
)
const currentRouteTotals = new Map(
routeSummaries.map(({ route, size, compressed_size }) => [
route,
{ size, compressedSize: compressed_size },
])
) )
return { return {
analyzeData, analyzeData,
...@@ -284,6 +303,8 @@ function useAnalyzerModel(compare: boolean) { ...@@ -284,6 +303,8 @@ function useAnalyzerModel(compare: boolean) {
moduleDepthMap, moduleDepthMap,
modulesData, modulesData,
currentRouteTotals, currentRouteTotals,
clientRouteTotals,
serverRouteTotals,
searchQuery: searchInput, searchQuery: searchInput,
selectedRoute, selectedRoute,
selectedSourceIndex, selectedSourceIndex,
...@@ -341,6 +362,7 @@ function AnalyzerTopBar({ ...@@ -341,6 +362,7 @@ function AnalyzerTopBar({
compareView={model.compareView} compareView={model.compareView}
onCompareViewChange={model.setCompareView} onCompareViewChange={model.setCompareView}
selectedRoute={model.selectedRoute} selectedRoute={model.selectedRoute}
getRouteHref={model.routeState.getRouteHref}
setSelectedRoute={model.routeState.setRoute} setSelectedRoute={model.routeState.setRoute}
environmentFilter={model.environmentFilter} environmentFilter={model.environmentFilter}
setEnvironmentFilter={model.setEnvironmentFilter} setEnvironmentFilter={model.setEnvironmentFilter}
...@@ -351,13 +373,17 @@ function AnalyzerTopBar({ ...@@ -351,13 +373,17 @@ function AnalyzerTopBar({
searchQuery={model.searchQuery} searchQuery={model.searchQuery}
setSearchQuery={model.setSearchQuery} setSearchQuery={model.setSearchQuery}
baselineSnapshot={model.baselineSnapshot} baselineSnapshot={model.baselineSnapshot}
onBaselineChange={(snapshot) => { getBaselineHref={model.routeState.getBaselineHref}
if (snapshot) model.routeState.startComparison(snapshot) onBaselineChange={model.routeState.setBaselineSnapshot}
else model.routeState.stopComparison() stopComparisonHref={model.routeState.stopComparisonHref}
}}
comparisonSnapshot={model.comparisonSnapshot} comparisonSnapshot={model.comparisonSnapshot}
onComparisonChange={model.routeState.setComparisonSnapshot} onComparisonChange={model.routeState.setComparisonSnapshot}
routeDiff={routeDiff} routeDiff={routeDiff}
routeTotals={
model.environmentFilter === Environment.Client
? model.clientRouteTotals
: model.serverRouteTotals
}
/> />
) )
} }
...@@ -413,18 +439,22 @@ function ValidComparisonContent({ ...@@ -413,18 +439,22 @@ function ValidComparisonContent({
`${baselineBaseDir}/routes.json`, `${baselineBaseDir}/routes.json`,
{ revalidateOnFocus: false, revalidateOnReconnect: false } { revalidateOnFocus: false, revalidateOnReconnect: false }
) )
const { totals: baselineRouteTotals } = useRouteTotals( const baselineRouteSummaries = useSuspenseJsonData<RouteSummary[]>(
`${baselineBaseDir}/route-summaries.json`,
{ revalidateOnFocus: false, revalidateOnReconnect: false }
)
const baselineRouteTotals = new Map(
baselineRouteSummaries.map(({ route, size, compressed_size }) => [
route,
{ size, compressedSize: compressed_size },
])
)
const routeDiff = diffRoutesWithSizes(
baselineRoutes, baselineRoutes,
baselineBaseDir model.currentRoutes,
) baselineRouteTotals,
const routeDiff = model.currentRouteTotals model.currentRouteTotals
? diffRoutesWithSizes( )
baselineRoutes,
model.currentRoutes,
baselineRouteTotals,
model.currentRouteTotals
)
: null
const layoutProps = { const layoutProps = {
baselineSnapshot, baselineSnapshot,
comparisonSnapshot: model.comparisonSnapshot, comparisonSnapshot: model.comparisonSnapshot,
...@@ -572,7 +602,11 @@ function SingleAnalyzerView({ model }: { model: AnalyzerModel }) { ...@@ -572,7 +602,11 @@ function SingleAnalyzerView({ model }: { model: AnalyzerModel }) {
const analyzeData = model.analyzeData const analyzeData = model.analyzeData
const content = analyzeData ? ( const content = analyzeData ? (
<SingleAnalyzerContent model={model} analyzeData={analyzeData} /> <SingleAnalyzerContent model={model} analyzeData={analyzeData} />
) : null ) : (
<div className="flex flex-1 items-center justify-center text-sm text-muted-foreground">
Select a route to analyze.
</div>
)
const footer = const footer =
analyzeData && model.compareView === CompareView.Treemap ? ( analyzeData && model.compareView === CompareView.Treemap ? (
......
'use client' 'use client'
import useSWR from 'swr' import useSWR from 'swr'
import Link from 'next/link'
import { import {
Check, Check,
ChevronsUpDown, ChevronsUpDown,
...@@ -16,6 +17,7 @@ import { ...@@ -16,6 +17,7 @@ import {
CommandGroup, CommandGroup,
CommandInput, CommandInput,
CommandItem, CommandItem,
CommandLinkItem,
CommandList, CommandList,
} from '@/components/ui/command' } from '@/components/ui/command'
import { import {
...@@ -32,16 +34,31 @@ import { ...@@ -32,16 +34,31 @@ import {
type SnapshotMetadata, type SnapshotMetadata,
} from '@/lib/snapshot' } from '@/lib/snapshot'
interface BaselinePickerProps { interface BaselinePickerOptions {
/** Selected historical snapshot id, or null for the control's default. */ /** Selected historical snapshot id, or null for the control's default. */
selectedSnapshotId: string | null selectedSnapshotId: string | null
onSelectionChange: (snapshot: SnapshotMetadata | null) => void
excludedSnapshotId?: string | null excludedSnapshotId?: string | null
prefix?: string prefix?: string
placeholder?: string placeholder?: string
clearLabel?: string clearLabel?: string
} }
type BaselinePickerProps = BaselinePickerOptions &
(
| {
mode: 'link'
getSnapshotHref: (snapshot: SnapshotMetadata) => string
clearHref: string
onSelectionChange?: never
}
| {
mode: 'action'
onSelectionChange: (snapshot: SnapshotMetadata | null) => void
clearHref?: string
getSnapshotHref?: never
}
)
/** /**
* Top-bar control that lets the user pick one historical analyze snapshot. * Top-bar control that lets the user pick one historical analyze snapshot.
* Two instances can select arbitrary A and B snapshots for comparison. * Two instances can select arbitrary A and B snapshots for comparison.
...@@ -49,14 +66,15 @@ interface BaselinePickerProps { ...@@ -49,14 +66,15 @@ interface BaselinePickerProps {
* Snapshots are loaded from `history/history.json` — written by * Snapshots are loaded from `history/history.json` — written by
* `writeAnalyzeSnapshot` after each `next build --analyze`. * `writeAnalyzeSnapshot` after each `next build --analyze`.
*/ */
export function BaselinePicker({ export function BaselinePicker(props: BaselinePickerProps) {
selectedSnapshotId, const {
onSelectionChange, selectedSnapshotId,
excludedSnapshotId, clearHref,
prefix = 'vs', excludedSnapshotId,
placeholder = 'Compare with…', prefix = 'vs',
clearLabel = 'Stop comparing', placeholder = 'Compare with…',
}: BaselinePickerProps) { clearLabel = 'Stop comparing',
} = props
const [open, setOpen] = useState(false) const [open, setOpen] = useState(false)
const { data: history, isLoading, error } = useHistoryIndex() const { data: history, isLoading, error } = useHistoryIndex()
...@@ -168,43 +186,66 @@ export function BaselinePicker({ ...@@ -168,43 +186,66 @@ export function BaselinePicker({
<CommandEmpty>No snapshots found.</CommandEmpty> <CommandEmpty>No snapshots found.</CommandEmpty>
)} )}
<CommandGroup> <CommandGroup>
{snapshots.map((snapshot) => ( {snapshots.map((snapshot) => {
<CommandItem const value = `${snapshot.id} ${snapshot.gitBranch ?? ''} ${snapshot.gitShortSha ?? ''} ${snapshot.gitMessage ?? ''} ${snapshot.snapshotName ?? ''}`
key={snapshot.id} const content = (
value={`${snapshot.id} ${snapshot.gitBranch ?? ''} ${snapshot.gitShortSha ?? ''} ${snapshot.gitMessage ?? ''} ${snapshot.snapshotName ?? ''}`} <>
onSelect={() => { <Check
onSelectionChange(snapshot) className={cn(
setOpen(false) 'mr-2 h-4 w-4',
}} selectedSnapshotId === snapshot.id
> ? 'opacity-100'
<Check : 'opacity-0'
className={cn( )}
'mr-2 h-4 w-4', />
selectedSnapshotId === snapshot.id <SnapshotRow snapshot={snapshot} />
? 'opacity-100' </>
: 'opacity-0' )
)} return props.mode === 'link' ? (
/> <CommandLinkItem
<SnapshotRow snapshot={snapshot} /> key={snapshot.id}
</CommandItem> value={value}
))} href={props.getSnapshotHref(snapshot)}
>
{content}
</CommandLinkItem>
) : (
<CommandItem
key={snapshot.id}
value={value}
onSelect={() => {
props.onSelectionChange(snapshot)
setOpen(false)
}}
>
{content}
</CommandItem>
)
})}
</CommandGroup> </CommandGroup>
</CommandList> </CommandList>
</Command> </Command>
</PopoverContent> </PopoverContent>
</Popover> </Popover>
{selected != null && ( {selected != null &&
<Button (clearHref ? (
variant="ghost" <Button asChild variant="ghost" size="icon" className="h-8 w-8">
size="icon" <Link href={clearHref} aria-label={clearLabel}>
aria-label={clearLabel} <X className="h-3.5 w-3.5" />
onClick={() => onSelectionChange(null)} </Link>
className="h-8 w-8" </Button>
> ) : props.mode === 'action' ? (
<X className="h-3.5 w-3.5" /> <Button
</Button> variant="ghost"
)} size="icon"
aria-label={clearLabel}
onClick={() => props.onSelectionChange(null)}
className="h-8 w-8"
>
<X className="h-3.5 w-3.5" />
</Button>
) : null)}
</div> </div>
) )
} }
......
'use client'
import { Suspense, useEffect, useRef } from 'react'
import { useSearchParams } from 'next/navigation'
import {
RouteTypeahead,
RouteTypeaheadContent,
} from '@/components/route-typeahead'
import { RouteSummarySkeleton } from '@/components/ui/skeleton'
import { useSuspenseJsonData } from '@/lib/analyzer-data'
import type { RouteSummary, RouteSizeTotals } from '@/lib/diff'
export function RouteSummaryPage() {
return (
<Suspense fallback={<RouteSummarySkeleton />}>
<RouteSummaryContent />
</Suspense>
)
}
function RouteSummaryContent() {
// Read the client URL before Suspense data: static prerendering must bail out
// before SWR attempts to fetch live analyzer files on the server.
const searchParams = useSearchParams()
const searchInputRef = useRef<HTMLInputElement>(null)
const summaries = useSuspenseJsonData<RouteSummary[]>(
'/data/route-summaries.json',
{ revalidateOnFocus: false, revalidateOnReconnect: false }
)
const clientRouteTotals = new Map<string, RouteSizeTotals>(
summaries.map(({ route, client }) => [
route,
{ size: client.size, compressedSize: client.compressed_size },
])
)
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (
event.key !== '/' ||
event.defaultPrevented ||
event.altKey ||
event.ctrlKey ||
event.metaKey
) {
return
}
const target = event.target
if (
target instanceof HTMLElement &&
(target.isContentEditable ||
['INPUT', 'TEXTAREA', 'SELECT'].includes(target.tagName))
) {
return
}
event.preventDefault()
searchInputRef.current?.focus()
}
window.addEventListener('keydown', handleKeyDown)
return () => window.removeEventListener('keydown', handleKeyDown)
}, [])
function getRouteHref(route: string) {
const params = new URLSearchParams(searchParams.toString())
params.set('route', route)
return `/analyze?${params.toString()}`
}
return (
<main className="h-screen flex flex-col bg-background">
<div className="flex-none px-4 py-2 border-b border-border flex items-center gap-3">
<div className="flex min-w-0 flex-1">
<RouteTypeahead
selectedRoute={null}
mode="link"
getRouteHref={getRouteHref}
routeTotals={clientRouteTotals}
/>
</div>
</div>
<div className="flex flex-1 justify-center overflow-auto px-6 py-12">
<section
className="w-[40rem] max-w-full h-fit"
aria-labelledby="route-heading"
>
<h1 id="route-heading" className="text-2xl font-semibold">
Analyze a route
</h1>
<p className="mt-2 mb-6 text-sm text-muted-foreground">
Choose a route to explore its bundle size and dependencies.
</p>
<div className="overflow-hidden rounded-md border bg-popover">
<RouteTypeaheadContent
selectedRoute={null}
mode="link"
getRouteHref={getRouteHref}
routeTotals={clientRouteTotals}
searchInputRef={searchInputRef}
/>
</div>
</section>
</div>
</main>
)
}
...@@ -12,7 +12,7 @@ import { ...@@ -12,7 +12,7 @@ import {
} from '@/components/ui/select' } from '@/components/ui/select'
import { MultiSelect } from '@/components/ui/multi-select' import { MultiSelect } from '@/components/ui/multi-select'
import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group' import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group'
import { diffRoutesWithSizes } from '@/lib/diff' import { diffRoutesWithSizes, type RouteSizeTotals } from '@/lib/diff'
import { type SnapshotMetadata } from '@/lib/snapshot' import { type SnapshotMetadata } from '@/lib/snapshot'
import { import {
Monitor, Monitor,
...@@ -61,6 +61,7 @@ export function ControlDivider() { ...@@ -61,6 +61,7 @@ export function ControlDivider() {
export function TopBar({ export function TopBar({
selectedRoute, selectedRoute,
setSelectedRoute, setSelectedRoute,
getRouteHref,
environmentFilter, environmentFilter,
setEnvironmentFilter, setEnvironmentFilter,
setSelectedSourceIndex, setSelectedSourceIndex,
...@@ -70,12 +71,15 @@ export function TopBar({ ...@@ -70,12 +71,15 @@ export function TopBar({
searchQuery, searchQuery,
setSearchQuery, setSearchQuery,
baselineSnapshot, baselineSnapshot,
getBaselineHref,
onBaselineChange, onBaselineChange,
stopComparisonHref,
comparisonSnapshot, comparisonSnapshot,
onComparisonChange, onComparisonChange,
compareView, compareView,
onCompareViewChange, onCompareViewChange,
routeDiff, routeDiff,
routeTotals,
hasSourceData, hasSourceData,
showViewToggle, showViewToggle,
}: { }: {
...@@ -83,6 +87,7 @@ export function TopBar({ ...@@ -83,6 +87,7 @@ export function TopBar({
showViewToggle: boolean showViewToggle: boolean
selectedRoute: string | null selectedRoute: string | null
setSelectedRoute: (route: string | null) => void setSelectedRoute: (route: string | null) => void
getRouteHref?: (route: string) => string
environmentFilter: Environment environmentFilter: Environment
setEnvironmentFilter: (env: Environment) => void setEnvironmentFilter: (env: Environment) => void
setSelectedSourceIndex: (index: number | null) => void setSelectedSourceIndex: (index: number | null) => void
...@@ -92,25 +97,48 @@ export function TopBar({ ...@@ -92,25 +97,48 @@ export function TopBar({
searchQuery: string searchQuery: string
setSearchQuery: (query: string) => void setSearchQuery: (query: string) => void
baselineSnapshot: SnapshotMetadata | null baselineSnapshot: SnapshotMetadata | null
onBaselineChange: (snapshot: SnapshotMetadata | null) => void getBaselineHref?: (snapshot: SnapshotMetadata) => string
onBaselineChange: (snapshot: SnapshotMetadata) => void
stopComparisonHref: string
comparisonSnapshot: SnapshotMetadata | null comparisonSnapshot: SnapshotMetadata | null
onComparisonChange: (snapshot: SnapshotMetadata | null) => void onComparisonChange: (snapshot: SnapshotMetadata | null) => void
compareView: CompareView compareView: CompareView
onCompareViewChange: (view: CompareView) => void onCompareViewChange: (view: CompareView) => void
routeDiff: ReturnType<typeof diffRoutesWithSizes> | null routeDiff: ReturnType<typeof diffRoutesWithSizes> | null
routeTotals?: ReadonlyMap<string, RouteSizeTotals> | null
}) { }) {
const isCompareMode = baselineSnapshot != null const isCompareMode = baselineSnapshot != null
const routeSelection = getRouteHref
? ({ mode: 'link', getRouteHref } as const)
: ({
mode: 'action',
onRouteSelected: (route: string) => {
setSelectedRoute(route)
setSelectedSourceIndex(null)
setFocusedSourceIndex(null)
},
} as const)
const baselineSelection = getBaselineHref
? ({
mode: 'link',
getSnapshotHref: getBaselineHref,
clearHref: stopComparisonHref,
} as const)
: ({
mode: 'action',
onSelectionChange: (snapshot: SnapshotMetadata | null) => {
if (snapshot) onBaselineChange(snapshot)
},
clearHref: stopComparisonHref,
} as const)
return ( return (
<div className="flex-none px-4 py-2 border-b border-border flex items-center gap-3"> <div className="flex-none px-4 py-2 border-b border-border flex items-center gap-3">
<div className="flex min-w-0 flex-1"> <div className="flex min-w-0 flex-1">
<RouteTypeahead <RouteTypeahead
selectedRoute={selectedRoute} selectedRoute={selectedRoute}
onRouteSelected={(route) => { {...routeSelection}
setSelectedRoute(route)
setSelectedSourceIndex(null)
setFocusedSourceIndex(null)
}}
routeDiff={isCompareMode ? routeDiff : null} routeDiff={isCompareMode ? routeDiff : null}
routeTotals={routeTotals}
useCompressed useCompressed
/> />
</div> </div>
...@@ -118,13 +146,14 @@ export function TopBar({ ...@@ -118,13 +146,14 @@ export function TopBar({
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<BaselinePicker <BaselinePicker
selectedSnapshotId={baselineSnapshot?.id ?? null} selectedSnapshotId={baselineSnapshot?.id ?? null}
onSelectionChange={onBaselineChange} {...baselineSelection}
excludedSnapshotId={comparisonSnapshot?.id} excludedSnapshotId={comparisonSnapshot?.id}
prefix="from" prefix="from"
placeholder="Compare from…" placeholder="Compare from…"
/> />
{isCompareMode ? ( {isCompareMode ? (
<BaselinePicker <BaselinePicker
mode="action"
selectedSnapshotId={comparisonSnapshot?.id ?? null} selectedSnapshotId={comparisonSnapshot?.id ?? null}
onSelectionChange={onComparisonChange} onSelectionChange={onComparisonChange}
excludedSnapshotId={baselineSnapshot?.id} excludedSnapshotId={baselineSnapshot?.id}
......
'use client' 'use client'
import * as React from 'react' import * as React from 'react'
import Link from 'next/link'
import { Command as CommandPrimitive } from 'cmdk' import { Command as CommandPrimitive } from 'cmdk'
import { SearchIcon } from 'lucide-react' import { SearchIcon } from 'lucide-react'
...@@ -155,6 +156,38 @@ function CommandItem({ ...@@ -155,6 +156,38 @@ function CommandItem({
) )
} }
// Keep cmdk's option semantics and keyboard selection while allowing normal
// link behavior (including modifier-click) for items that change pages.
function CommandLinkItem({
href,
className,
children,
...props
}: Omit<React.ComponentProps<typeof CommandItem>, 'onSelect'> & {
href: string
}) {
const linkRef = React.useRef<HTMLAnchorElement>(null)
return (
<CommandItem
{...props}
className={cn('p-0', className)}
onSelect={() => linkRef.current?.click()}
>
<Link
ref={linkRef}
href={href}
className="flex w-full min-w-0 items-center gap-2 px-2 py-1.5"
onClick={(event) => {
// Clicks on the link must not also select the parent command item.
event.stopPropagation()
}}
>
{children}
</Link>
</CommandItem>
)
}
function CommandShortcut({ function CommandShortcut({
className, className,
...props ...props
...@@ -179,6 +212,7 @@ export { ...@@ -179,6 +212,7 @@ export {
CommandEmpty, CommandEmpty,
CommandGroup, CommandGroup,
CommandItem, CommandItem,
CommandLinkItem,
CommandShortcut, CommandShortcut,
CommandSeparator, CommandSeparator,
} }
...@@ -60,6 +60,47 @@ export function TableSkeleton() { ...@@ -60,6 +60,47 @@ export function TableSkeleton() {
) )
} }
export function RouteSummarySkeleton() {
return (
<main className="flex h-screen flex-col overflow-hidden bg-background">
<div className="flex h-14 flex-none items-center gap-3 border-b border-border px-4">
<Skeleton className="h-9 min-w-64 flex-1" />
</div>
<div className="flex flex-1 justify-center overflow-hidden px-6 py-12">
<div className="w-full max-w-3xl" aria-hidden="true">
<div className="mb-6 flex items-start justify-between gap-6">
<div className="flex flex-col gap-3">
<Skeleton className="h-4 w-28" />
<Skeleton className="h-7 w-64" />
<Skeleton className="h-4 w-96 max-w-full" />
</div>
<div className="flex items-center gap-3">
<Skeleton className="h-4 w-16" />
<Skeleton className="h-8 w-32" />
</div>
</div>
<div className="overflow-hidden rounded-md border bg-card">
{Array.from({ length: 15 }, (_, index) => (
<div
key={index}
className="flex h-12 items-center gap-4 border-b px-4 last:border-b-0"
>
<Skeleton className="h-3 w-5" />
<Skeleton
className="h-3"
style={{ width: `${35 + (index % 4) * 8}%` }}
/>
<Skeleton className="ml-auto h-3 w-16" />
<Skeleton className="h-4 w-4" />
</div>
))}
</div>
</div>
</div>
</main>
)
}
export function AnalyzerChromeSkeleton({ export function AnalyzerChromeSkeleton({
view, view,
}: { }: {
......
...@@ -107,38 +107,6 @@ function pushRow<Row extends DiffRow>( ...@@ -107,38 +107,6 @@ function pushRow<Row extends DiffRow>(
summary.totalCompressedB += row.compressedB summary.totalCompressedB += row.compressedB
} }
/**
* Diffs two route lists. Routes are compared by their page path. Without
* per-route data loaded (which would be N round-trips), the size columns are
* left at zero; the table view fills them in lazily as the user expands rows.
*/
export function diffRouteLists(
routesA: string[] | null,
routesB: string[]
): DiffSummary {
const summary = emptySummary()
const setA = new Set(routesA ?? [])
const setB = new Set(routesB)
const all = new Set<string>([...setA, ...setB])
const sorted = Array.from(all).sort()
for (const route of sorted) {
const inA = setA.has(route)
const inB = setB.has(route)
pushRow(summary, {
key: route,
name: route,
status: statusFor(0, 0, inA, inB),
sizeA: 0,
sizeB: 0,
compressedA: 0,
compressedB: 0,
})
}
return summary
}
/** /**
* Per-route size aggregates contributed by a single side (A or B). When a * Per-route size aggregates contributed by a single side (A or B). When a
* route is missing from the corresponding map entirely, it's treated as * route is missing from the corresponding map entirely, it's treated as
...@@ -149,6 +117,16 @@ export interface RouteSizeTotals { ...@@ -149,6 +117,16 @@ export interface RouteSizeTotals {
compressedSize: number compressedSize: number
} }
export interface RouteSummary {
route: string
size: number
compressed_size: number
client: {
size: number
compressed_size: number
}
}
/** /**
* Sums the `getOwnSizes` of every leaf source in an {@link AnalyzeData}, * Sums the `getOwnSizes` of every leaf source in an {@link AnalyzeData},
* giving a single route's total contribution. This matches what the existing * giving a single route's total contribution. This matches what the existing
...@@ -171,25 +149,15 @@ export function totalsFromAnalyzeData( ...@@ -171,25 +149,15 @@ export function totalsFromAnalyzeData(
return { size, compressedSize } return { size, compressedSize }
} }
/** /** Diffs route sizes across two builds. */
* Size-aware variant of {@link diffRouteLists}. When `sizesA`/`sizesB` are
* provided, routes get real `changed`/`identical` classification based on
* their summed source sizes — not just name-presence.
*
* A route is `added` if it is missing from `sizesA`, `removed` if missing
* from `sizesB`. `sizesA == null` means the baseline is still loading and we
* fall back to the name-only diff (`diffRouteLists`).
*/
export function diffRoutesWithSizes( export function diffRoutesWithSizes(
routesA: string[] | null, routesA: string[],
routesB: string[], routesB: string[],
sizesA: ReadonlyMap<string, RouteSizeTotals> | null, sizesA: ReadonlyMap<string, RouteSizeTotals>,
sizesB: ReadonlyMap<string, RouteSizeTotals> sizesB: ReadonlyMap<string, RouteSizeTotals>
): DiffSummary { ): DiffSummary {
if (!sizesA) return diffRouteLists(routesA, routesB)
const summary = emptySummary() const summary = emptySummary()
const setA = new Set(routesA ?? []) const setA = new Set(routesA)
const setB = new Set(routesB) const setB = new Set(routesB)
const all = new Set<string>([...setA, ...setB]) const all = new Set<string>([...setA, ...setB])
const sorted = Array.from(all).sort() const sorted = Array.from(all).sort()
......
...@@ -34,10 +34,9 @@ export function useAnalyzerRoute( ...@@ -34,10 +34,9 @@ export function useAnalyzerRoute(
const searchQuery = searchParams.get('query') ?? '' const searchQuery = searchParams.get('query') ?? ''
const typeFilter = parseTypeFilter(searchParams.get('types')) const typeFilter = parseTypeFilter(searchParams.get('types'))
function navigate( function buildHref(
nextPathname: string, nextPathname: string,
updates: Record<string, string | null>, updates: Record<string, string | null>
method: 'push' | 'replace'
) { ) {
const nextSearchParams = new URLSearchParams(searchParams.toString()) const nextSearchParams = new URLSearchParams(searchParams.toString())
for (const [key, value] of Object.entries(updates)) { for (const [key, value] of Object.entries(updates)) {
...@@ -45,7 +44,15 @@ export function useAnalyzerRoute( ...@@ -45,7 +44,15 @@ export function useAnalyzerRoute(
else nextSearchParams.set(key, value) else nextSearchParams.set(key, value)
} }
const query = nextSearchParams.toString() const query = nextSearchParams.toString()
router[method](`${nextPathname}${query ? `?${query}` : ''}`) return `${nextPathname}${query ? `?${query}` : ''}`
}
function navigate(
nextPathname: string,
updates: Record<string, string | null>,
method: 'push' | 'replace'
) {
router[method](buildHref(nextPathname, updates))
} }
function replaceSearchParams(updates: Record<string, string | null>) { function replaceSearchParams(updates: Record<string, string | null>) {
...@@ -73,10 +80,22 @@ export function useAnalyzerRoute( ...@@ -73,10 +80,22 @@ export function useAnalyzerRoute(
setView: (view: CompareView) => navigate(pathname, { view }, 'replace'), setView: (view: CompareView) => navigate(pathname, { view }, 'replace'),
setRoute: (route: string | null) => setRoute: (route: string | null) =>
navigate(pathname, { route }, 'replace'), navigate(pathname, { route }, 'replace'),
startComparison: (snapshot: SnapshotMetadata) => getRouteHref:
pathname === '/'
? (route: string) => buildHref('/analyze', { route })
: undefined,
getBaselineHref:
pathname === '/compare'
? undefined
: (snapshot: SnapshotMetadata) =>
buildHref('/compare', { from: snapshot.id, to: null, view: null }),
setBaselineSnapshot: (snapshot: SnapshotMetadata) =>
navigate('/compare', { from: snapshot.id, to: null, view: null }, 'push'), navigate('/compare', { from: snapshot.id, to: null, view: null }, 'push'),
stopComparison: () => stopComparisonHref: buildHref('/analyze', {
navigate('/', { from: null, to: null, view: null }, 'push'), from: null,
to: null,
view: null,
}),
setComparisonSnapshot: (snapshot: SnapshotMetadata | null) => setComparisonSnapshot: (snapshot: SnapshotMetadata | null) =>
navigate(pathname, { to: snapshot?.id ?? null }, 'replace'), navigate(pathname, { to: snapshot?.id ?? null }, 'replace'),
setEnvironmentFilter: (environment: Environment) => setEnvironmentFilter: (environment: Environment) =>
......
import { useEffect, useState } from 'react'
import { AnalyzeData } from './analyze-data'
import { analyzeDataUrl } from './analyzer-data'
import { totalsFromAnalyzeData, type RouteSizeTotals } from './diff'
import { fetchStrict } from './utils'
/**
* Result of loading route totals for one side. `totals` is keyed by route.
* Routes whose `analyze.data` failed to load (e.g. deleted between builds)
* are simply absent from the map; callers should treat absence as
* "not present in this build".
*/
export interface RouteTotalsState {
totals: ReadonlyMap<string, RouteSizeTotals> | null
isLoading: boolean
}
/**
* Loads `analyze.data` for every route in `routes` from `baseDir` and
* reduces each to its size totals. Used to power the route-level diff so
* that routes can be classified `changed`/`identical` by real bundle size,
* not just name presence.
*
* The fetch fan-out is parallel; for projects with many routes this can be
* heavy, but typical analyze runs have a small number of routes. Results
* are stored in module-scoped state keyed by `baseDir` to avoid refetching
* across re-renders.
*/
export function useRouteTotals(
routes: string[] | null | undefined,
baseDir: string | null
): RouteTotalsState {
const [state, setState] = useState<RouteTotalsState>({
totals: null,
isLoading: false,
})
useEffect(() => {
if (!routes || !baseDir) {
setState({ totals: null, isLoading: false })
return
}
let cancelled = false
setState((prev) => ({ totals: prev.totals, isLoading: true }))
Promise.all(
routes.map(async (route) => {
try {
const resp = await fetchStrict(analyzeDataUrl(baseDir, route))
const data = new AnalyzeData(await resp.arrayBuffer())
return [route, totalsFromAnalyzeData(data)] as const
} catch {
return null
}
})
).then((results) => {
if (cancelled) return
const totals = new Map<string, RouteSizeTotals>()
for (const result of results) {
if (result) totals.set(result[0], result[1])
}
setState({ totals, isLoading: false })
})
return () => {
cancelled = true
}
}, [routes, baseDir])
return state
}
...@@ -25,6 +25,21 @@ use turbopack_core::{ ...@@ -25,6 +25,21 @@ use turbopack_core::{
reference::all_assets_from_entries, reference::all_assets_from_entries,
}; };
#[turbo_tasks::value]
#[derive(Clone, Copy, Serialize)]
pub struct BundleTotals {
pub size: u64,
pub compressed_size: u64,
}
#[turbo_tasks::value]
#[derive(Clone, Copy, Serialize)]
pub struct RouteBundleSummary {
pub size: u64,
pub compressed_size: u64,
pub client: BundleTotals,
}
pub struct EdgesData { pub struct EdgesData {
pub offsets: Vec<u32>, pub offsets: Vec<u32>,
pub data: Vec<u32>, pub data: Vec<u32>,
...@@ -401,12 +416,34 @@ pub async fn combine_traced_files( ...@@ -401,12 +416,34 @@ pub async fn combine_traced_files(
Ok(Vc::cell(combined)) Ok(Vc::cell(combined))
} }
#[turbo_tasks::value]
pub struct AnalyzedRoute {
pub content: ResolvedVc<FileContent>,
pub summary: RouteBundleSummary,
}
#[turbo_tasks::value_impl]
impl AnalyzedRoute {
#[turbo_tasks::function]
fn file_content(&self) -> Vc<FileContent> {
*self.content
}
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn analyze_output_assets( pub async fn analyze_output_assets(
output_assets: Vc<OutputAssets>, output_assets: Vc<OutputAssets>,
traced_files: Vc<FileSystemPathVec>, traced_files: Vc<FileSystemPathVec>,
) -> Result<Vc<FileContent>> { ) -> Result<Vc<AnalyzedRoute>> {
let output_assets = all_assets_from_entries(output_assets); let output_assets = all_assets_from_entries(output_assets);
let mut summary = RouteBundleSummary {
size: 0,
compressed_size: 0,
client: BundleTotals {
size: 0,
compressed_size: 0,
},
};
let mut builder = AnalyzeDataBuilder::new(); let mut builder = AnalyzeDataBuilder::new();
...@@ -439,6 +476,7 @@ pub async fn analyze_output_assets( ...@@ -439,6 +476,7 @@ pub async fn analyze_output_assets(
Either::Right(path) => path.to_string_ref().await?, Either::Right(path) => path.to_string_ref().await?,
}; };
let is_client = filename.starts_with("[client-fs]/");
let output_file_index = builder.add_output_file(AnalyzeOutputFile { let output_file_index = builder.add_output_file(AnalyzeOutputFile {
filename: filename.clone(), filename: filename.clone(),
}); });
...@@ -460,11 +498,18 @@ pub async fn analyze_output_assets( ...@@ -460,11 +498,18 @@ pub async fn analyze_output_assets(
}; };
let source_index = builder.ensure_source(&source).1; let source_index = builder.ensure_source(&source).1;
let size = chunk_part.real_size + chunk_part.unaccounted_size; let size = chunk_part.real_size + chunk_part.unaccounted_size;
let compressed_size = chunk_part.get_compressed_size().await?.unwrap_or(size);
summary.size += u64::from(size);
summary.compressed_size += u64::from(compressed_size);
if is_client {
summary.client.size += u64::from(size);
summary.client.compressed_size += u64::from(compressed_size);
}
let chunk_part_index = builder.add_chunk_part(AnalyzeChunkPart { let chunk_part_index = builder.add_chunk_part(AnalyzeChunkPart {
source_index, source_index,
output_file_index, output_file_index,
size, size,
compressed_size: chunk_part.get_compressed_size().await?.unwrap_or(size), compressed_size,
}); });
builder.add_chunk_part_to_output_file(output_file_index, chunk_part_index); builder.add_chunk_part_to_output_file(output_file_index, chunk_part_index);
builder.add_chunk_part_to_source(source_index, chunk_part_index); builder.add_chunk_part_to_source(source_index, chunk_part_index);
...@@ -493,7 +538,14 @@ pub async fn analyze_output_assets( ...@@ -493,7 +538,14 @@ pub async fn analyze_output_assets(
} }
let rope = builder.build(); let rope = builder.build();
Ok(FileContent::Content(File::from(rope)).cell()) Ok(AnalyzedRoute {
content: FileContent::Content(File::from(rope))
.cell()
.to_resolved()
.await?,
summary,
}
.cell())
} }
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -660,7 +712,8 @@ impl AnalyzeDataOutputAsset { ...@@ -660,7 +712,8 @@ impl AnalyzeDataOutputAsset {
impl Asset for AnalyzeDataOutputAsset { impl Asset for AnalyzeDataOutputAsset {
#[turbo_tasks::function] #[turbo_tasks::function]
fn content(&self) -> Vc<AssetContent> { fn content(&self) -> Vc<AssetContent> {
let file_content = analyze_output_assets(*self.output_assets, *self.traced_files); let file_content =
analyze_output_assets(*self.output_assets, *self.traced_files).file_content();
AssetContent::file(file_content) AssetContent::file(file_content)
} }
} }
......
use std::{iter::once, sync::Arc}; use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use next_api::{ use next_api::{
analyze::{ analyze::{
AnalyzeDataOutputAsset, ModulesDataOutputAsset, combine_output_assets, combine_traced_files, AnalyzeDataOutputAsset, ModulesDataOutputAsset, RouteBundleSummary, analyze_output_assets,
combine_output_assets, combine_traced_files,
}, },
project::ProjectContainer, project::ProjectContainer,
route::EndpointGroupKey, route::EndpointGroupKey,
}; };
use serde::Serialize;
use turbo_tasks::{Effects, ReadRef, ResolvedVc, TryJoinIterExt, Vc}; use turbo_tasks::{Effects, ReadRef, ResolvedVc, TryJoinIterExt, Vc};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack_core::{ use turbopack_core::{
asset::AssetContent,
issue::PlainIssue, issue::PlainIssue,
output::{OutputAsset, OutputAssets}, output::{OutputAsset, OutputAssets},
virtual_output::VirtualOutputAsset,
}; };
#[derive(Serialize)]
struct RouteSummary {
route: String,
#[serde(flatten)]
bundles: RouteBundleSummary,
}
use crate::next_api::utils::strongly_consistent_catch_collectables; use crate::next_api::utils::strongly_consistent_catch_collectables;
#[turbo_tasks::value(serialization = "skip")] #[turbo_tasks::value(serialization = "skip")]
...@@ -88,7 +99,7 @@ async fn get_analyze_data_operation( ...@@ -88,7 +99,7 @@ async fn get_analyze_data_operation(
let combined_assets_vc = Vc::cell(combined_output_assets); let combined_assets_vc = Vc::cell(combined_output_assets);
let combined_traced_vc = Vc::cell(combined_traced_files); let combined_traced_vc = Vc::cell(combined_traced_files);
let analyze_data = endpoint_groups let analyze_data_and_summaries = endpoint_groups
.iter() .iter()
.map(async |(key, endpoint_group)| { .map(async |(key, endpoint_group)| {
let output_assets = if has_combined let output_assets = if has_combined
...@@ -123,11 +134,42 @@ async fn get_analyze_data_operation( ...@@ -123,11 +134,42 @@ async fn get_analyze_data_operation(
.to_resolved() .to_resolved()
.await?; .await?;
Ok(ResolvedVc::upcast(analyze_data)) let summary = if let EndpointGroupKey::Route(route) = key {
Some(RouteSummary {
route: route.to_string(),
bundles: analyze_output_assets(output_assets, traced_files)
.await?
.summary,
})
} else {
None
};
Ok((ResolvedVc::upcast(analyze_data), summary))
}) })
.try_join() .try_join()
.await?; .await?;
let mut analyze_data = Vec::with_capacity(analyze_data_and_summaries.len() + 2);
let mut route_summaries = Vec::new();
for (asset, summary) in analyze_data_and_summaries {
analyze_data.push(asset);
if let Some(summary) = summary {
route_summaries.push(summary);
}
}
let route_summaries_asset = ResolvedVc::upcast(
VirtualOutputAsset::new(
analyze_output_root.join("route-summaries.json")?,
AssetContent::file(
FileContent::Content(File::from(serde_json::to_string(&route_summaries)?)).cell(),
),
)
.to_resolved()
.await?,
);
let modules_data = ResolvedVc::upcast( let modules_data = ResolvedVc::upcast(
ModulesDataOutputAsset::new( ModulesDataOutputAsset::new(
analyze_output_root.join("modules.data")?, analyze_output_root.join("modules.data")?,
...@@ -137,11 +179,6 @@ async fn get_analyze_data_operation( ...@@ -137,11 +179,6 @@ async fn get_analyze_data_operation(
.await?, .await?,
); );
Ok(Vc::cell( analyze_data.extend([route_summaries_asset, modules_data]);
analyze_data Ok(Vc::cell(analyze_data))
.iter()
.cloned()
.chain(once(modules_data))
.collect(),
))
} }
...@@ -109,6 +109,7 @@ describe('next analyze', () => { ...@@ -109,6 +109,7 @@ describe('next analyze', () => {
for (const file of [ for (const file of [
'index.html', 'index.html',
'data/routes.json', 'data/routes.json',
'data/route-summaries.json',
'data/modules.data', 'data/modules.data',
'data/analyze.data', 'data/analyze.data',
]) { ]) {
...@@ -121,6 +122,81 @@ describe('next analyze', () => { ...@@ -121,6 +122,81 @@ describe('next analyze', () => {
) )
const routes = JSON.parse(routesJson) const routes = JSON.parse(routesJson)
expect(routes).toEqual(['/', '/_not-found']) expect(routes).toEqual(['/', '/_not-found'])
const routeSummaries = JSON.parse(
readFileSync(
path.join(defaultOutputPath, 'data', 'route-summaries.json'),
'utf-8'
)
)
expect(
routeSummaries.map((summary: { route: string }) => summary.route)
).toEqual(expect.arrayContaining(routes))
for (const summary of routeSummaries) {
expect(Number.isFinite(summary.size)).toBe(true)
expect(summary.size).toBeGreaterThanOrEqual(0)
expect(Number.isFinite(summary.compressed_size)).toBe(true)
expect(summary.compressed_size).toBeGreaterThanOrEqual(0)
// The summary and analyze.data must account for exactly the same
// chunk parts, including shared assets and traced files.
const routeDir = summary.route.replace(/^\//, '')
const analyzeBuffer = readFileSync(
path.join(defaultOutputPath, 'data', routeDir, 'analyze.data')
)
const header = JSON.parse(
analyzeBuffer
.subarray(4, 4 + analyzeBuffer.readUInt32BE(0))
.toString('utf-8')
) as {
output_files: { filename: string }[]
chunk_parts: {
output_file_index: number
size: number
compressed_size: number
}[]
}
const totals = {
size: 0,
compressed_size: 0,
client: { size: 0, compressed_size: 0 },
}
for (const part of header.chunk_parts) {
totals.size += part.size
totals.compressed_size += part.compressed_size
if (
header.output_files[part.output_file_index].filename.startsWith(
'[client-fs]/'
)
) {
totals.client.size += part.size
totals.client.compressed_size += part.compressed_size
}
}
expect(summary).toMatchObject(totals)
expect(
header.output_files.some(({ filename }) =>
/\.(?:map|nft\.json)$/.test(filename)
)
).toBe(false)
}
const history = JSON.parse(
readFileSync(
path.join(defaultOutputPath, 'history', 'history.json'),
'utf-8'
)
)
const snapshotRouteSummaries = readFileSync(
path.join(
defaultOutputPath,
'history',
history.snapshots[0].id,
'route-summaries.json'
),
'utf-8'
)
expect(JSON.parse(snapshotRouteSummaries)).toEqual(routeSummaries)
}) })
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment