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() {
return <SingleAnalyzer />
return <RouteSummaryPage />
}
......@@ -34,8 +34,7 @@ import {
useHistoryIndex,
useSuspenseJsonData,
} from '@/lib/analyzer-data'
import { diffRoutesWithSizes, diffSources } from '@/lib/diff'
import { useRouteTotals } from '@/lib/use-route-totals'
import { diffRoutesWithSizes, diffSources, type RouteSummary } from '@/lib/diff'
import { useSidebarResize } from '@/lib/use-sidebar-resize'
import { useAnalyzerRoute } from '@/lib/use-analyzer-route'
import { computeActiveEntries, computeModuleDepthMap } from '@/lib/module-graph'
......@@ -72,12 +71,14 @@ function CompareAnalyzerController() {
function AnalyzerBoundary({
children,
defaultView,
fallback,
}: {
children: ReactNode
defaultView: CompareView
fallback?: ReactNode
}) {
return (
<Suspense fallback={<AnalyzerFallback view={defaultView} />}>
<Suspense fallback={fallback ?? <AnalyzerFallback view={defaultView} />}>
{children}
</Suspense>
)
......@@ -260,12 +261,30 @@ function useAnalyzerModel(compare: boolean) {
})
}, [analyzeData, baselineSnapshot, filterSource])
// Per-route totals for both sides, used to size the route-level diff so
// that routes whose modules changed can be reported as `changed` rather
// than `identical`. Only fetched in compare mode.
const { totals: currentRouteTotals } = useRouteTotals(
baselineSnapshot ? currentRoutes : null,
baselineSnapshot ? comparisonBaseDir : null
const routeSummaries = useSuspenseJsonData<RouteSummary[]>(
`${comparisonBaseDir}/route-summaries.json`,
{ revalidateOnFocus: false, revalidateOnReconnect: false }
)
const clientRouteTotals = new Map(
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 {
analyzeData,
......@@ -284,6 +303,8 @@ function useAnalyzerModel(compare: boolean) {
moduleDepthMap,
modulesData,
currentRouteTotals,
clientRouteTotals,
serverRouteTotals,
searchQuery: searchInput,
selectedRoute,
selectedSourceIndex,
......@@ -341,6 +362,7 @@ function AnalyzerTopBar({
compareView={model.compareView}
onCompareViewChange={model.setCompareView}
selectedRoute={model.selectedRoute}
getRouteHref={model.routeState.getRouteHref}
setSelectedRoute={model.routeState.setRoute}
environmentFilter={model.environmentFilter}
setEnvironmentFilter={model.setEnvironmentFilter}
......@@ -351,13 +373,17 @@ function AnalyzerTopBar({
searchQuery={model.searchQuery}
setSearchQuery={model.setSearchQuery}
baselineSnapshot={model.baselineSnapshot}
onBaselineChange={(snapshot) => {
if (snapshot) model.routeState.startComparison(snapshot)
else model.routeState.stopComparison()
}}
getBaselineHref={model.routeState.getBaselineHref}
onBaselineChange={model.routeState.setBaselineSnapshot}
stopComparisonHref={model.routeState.stopComparisonHref}
comparisonSnapshot={model.comparisonSnapshot}
onComparisonChange={model.routeState.setComparisonSnapshot}
routeDiff={routeDiff}
routeTotals={
model.environmentFilter === Environment.Client
? model.clientRouteTotals
: model.serverRouteTotals
}
/>
)
}
......@@ -413,18 +439,22 @@ function ValidComparisonContent({
`${baselineBaseDir}/routes.json`,
{ 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,
baselineBaseDir
)
const routeDiff = model.currentRouteTotals
? diffRoutesWithSizes(
baselineRoutes,
model.currentRoutes,
baselineRouteTotals,
model.currentRouteTotals
)
: null
model.currentRoutes,
baselineRouteTotals,
model.currentRouteTotals
)
const layoutProps = {
baselineSnapshot,
comparisonSnapshot: model.comparisonSnapshot,
......@@ -572,7 +602,11 @@ function SingleAnalyzerView({ model }: { model: AnalyzerModel }) {
const analyzeData = model.analyzeData
const content = 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 =
analyzeData && model.compareView === CompareView.Treemap ? (
......
'use client'
import useSWR from 'swr'
import Link from 'next/link'
import {
Check,
ChevronsUpDown,
......@@ -16,6 +17,7 @@ import {
CommandGroup,
CommandInput,
CommandItem,
CommandLinkItem,
CommandList,
} from '@/components/ui/command'
import {
......@@ -32,16 +34,31 @@ import {
type SnapshotMetadata,
} from '@/lib/snapshot'
interface BaselinePickerProps {
interface BaselinePickerOptions {
/** Selected historical snapshot id, or null for the control's default. */
selectedSnapshotId: string | null
onSelectionChange: (snapshot: SnapshotMetadata | null) => void
excludedSnapshotId?: string | null
prefix?: string
placeholder?: 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.
* Two instances can select arbitrary A and B snapshots for comparison.
......@@ -49,14 +66,15 @@ interface BaselinePickerProps {
* Snapshots are loaded from `history/history.json` — written by
* `writeAnalyzeSnapshot` after each `next build --analyze`.
*/
export function BaselinePicker({
selectedSnapshotId,
onSelectionChange,
excludedSnapshotId,
prefix = 'vs',
placeholder = 'Compare with…',
clearLabel = 'Stop comparing',
}: BaselinePickerProps) {
export function BaselinePicker(props: BaselinePickerProps) {
const {
selectedSnapshotId,
clearHref,
excludedSnapshotId,
prefix = 'vs',
placeholder = 'Compare with…',
clearLabel = 'Stop comparing',
} = props
const [open, setOpen] = useState(false)
const { data: history, isLoading, error } = useHistoryIndex()
......@@ -168,43 +186,66 @@ export function BaselinePicker({
<CommandEmpty>No snapshots found.</CommandEmpty>
)}
<CommandGroup>
{snapshots.map((snapshot) => (
<CommandItem
key={snapshot.id}
value={`${snapshot.id} ${snapshot.gitBranch ?? ''} ${snapshot.gitShortSha ?? ''} ${snapshot.gitMessage ?? ''} ${snapshot.snapshotName ?? ''}`}
onSelect={() => {
onSelectionChange(snapshot)
setOpen(false)
}}
>
<Check
className={cn(
'mr-2 h-4 w-4',
selectedSnapshotId === snapshot.id
? 'opacity-100'
: 'opacity-0'
)}
/>
<SnapshotRow snapshot={snapshot} />
</CommandItem>
))}
{snapshots.map((snapshot) => {
const value = `${snapshot.id} ${snapshot.gitBranch ?? ''} ${snapshot.gitShortSha ?? ''} ${snapshot.gitMessage ?? ''} ${snapshot.snapshotName ?? ''}`
const content = (
<>
<Check
className={cn(
'mr-2 h-4 w-4',
selectedSnapshotId === snapshot.id
? 'opacity-100'
: 'opacity-0'
)}
/>
<SnapshotRow snapshot={snapshot} />
</>
)
return props.mode === 'link' ? (
<CommandLinkItem
key={snapshot.id}
value={value}
href={props.getSnapshotHref(snapshot)}
>
{content}
</CommandLinkItem>
) : (
<CommandItem
key={snapshot.id}
value={value}
onSelect={() => {
props.onSelectionChange(snapshot)
setOpen(false)
}}
>
{content}
</CommandItem>
)
})}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
{selected != null && (
<Button
variant="ghost"
size="icon"
aria-label={clearLabel}
onClick={() => onSelectionChange(null)}
className="h-8 w-8"
>
<X className="h-3.5 w-3.5" />
</Button>
)}
{selected != null &&
(clearHref ? (
<Button asChild variant="ghost" size="icon" className="h-8 w-8">
<Link href={clearHref} aria-label={clearLabel}>
<X className="h-3.5 w-3.5" />
</Link>
</Button>
) : props.mode === 'action' ? (
<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>
)
}
......
'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 {
} from '@/components/ui/select'
import { MultiSelect } from '@/components/ui/multi-select'
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 {
Monitor,
......@@ -61,6 +61,7 @@ export function ControlDivider() {
export function TopBar({
selectedRoute,
setSelectedRoute,
getRouteHref,
environmentFilter,
setEnvironmentFilter,
setSelectedSourceIndex,
......@@ -70,12 +71,15 @@ export function TopBar({
searchQuery,
setSearchQuery,
baselineSnapshot,
getBaselineHref,
onBaselineChange,
stopComparisonHref,
comparisonSnapshot,
onComparisonChange,
compareView,
onCompareViewChange,
routeDiff,
routeTotals,
hasSourceData,
showViewToggle,
}: {
......@@ -83,6 +87,7 @@ export function TopBar({
showViewToggle: boolean
selectedRoute: string | null
setSelectedRoute: (route: string | null) => void
getRouteHref?: (route: string) => string
environmentFilter: Environment
setEnvironmentFilter: (env: Environment) => void
setSelectedSourceIndex: (index: number | null) => void
......@@ -92,25 +97,48 @@ export function TopBar({
searchQuery: string
setSearchQuery: (query: string) => void
baselineSnapshot: SnapshotMetadata | null
onBaselineChange: (snapshot: SnapshotMetadata | null) => void
getBaselineHref?: (snapshot: SnapshotMetadata) => string
onBaselineChange: (snapshot: SnapshotMetadata) => void
stopComparisonHref: string
comparisonSnapshot: SnapshotMetadata | null
onComparisonChange: (snapshot: SnapshotMetadata | null) => void
compareView: CompareView
onCompareViewChange: (view: CompareView) => void
routeDiff: ReturnType<typeof diffRoutesWithSizes> | null
routeTotals?: ReadonlyMap<string, RouteSizeTotals> | 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 (
<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={selectedRoute}
onRouteSelected={(route) => {
setSelectedRoute(route)
setSelectedSourceIndex(null)
setFocusedSourceIndex(null)
}}
{...routeSelection}
routeDiff={isCompareMode ? routeDiff : null}
routeTotals={routeTotals}
useCompressed
/>
</div>
......@@ -118,13 +146,14 @@ export function TopBar({
<div className="flex items-center gap-2">
<BaselinePicker
selectedSnapshotId={baselineSnapshot?.id ?? null}
onSelectionChange={onBaselineChange}
{...baselineSelection}
excludedSnapshotId={comparisonSnapshot?.id}
prefix="from"
placeholder="Compare from…"
/>
{isCompareMode ? (
<BaselinePicker
mode="action"
selectedSnapshotId={comparisonSnapshot?.id ?? null}
onSelectionChange={onComparisonChange}
excludedSnapshotId={baselineSnapshot?.id}
......
'use client'
import * as React from 'react'
import Link from 'next/link'
import { Command as CommandPrimitive } from 'cmdk'
import { SearchIcon } from 'lucide-react'
......@@ -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({
className,
...props
......@@ -179,6 +212,7 @@ export {
CommandEmpty,
CommandGroup,
CommandItem,
CommandLinkItem,
CommandShortcut,
CommandSeparator,
}
......@@ -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({
view,
}: {
......
......@@ -107,38 +107,6 @@ function pushRow<Row extends DiffRow>(
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
* route is missing from the corresponding map entirely, it's treated as
......@@ -149,6 +117,16 @@ export interface RouteSizeTotals {
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},
* giving a single route's total contribution. This matches what the existing
......@@ -171,25 +149,15 @@ export function totalsFromAnalyzeData(
return { size, compressedSize }
}
/**
* 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`).
*/
/** Diffs route sizes across two builds. */
export function diffRoutesWithSizes(
routesA: string[] | null,
routesA: string[],
routesB: string[],
sizesA: ReadonlyMap<string, RouteSizeTotals> | null,
sizesA: ReadonlyMap<string, RouteSizeTotals>,
sizesB: ReadonlyMap<string, RouteSizeTotals>
): DiffSummary {
if (!sizesA) return diffRouteLists(routesA, routesB)
const summary = emptySummary()
const setA = new Set(routesA ?? [])
const setA = new Set(routesA)
const setB = new Set(routesB)
const all = new Set<string>([...setA, ...setB])
const sorted = Array.from(all).sort()
......
......@@ -34,10 +34,9 @@ export function useAnalyzerRoute(
const searchQuery = searchParams.get('query') ?? ''
const typeFilter = parseTypeFilter(searchParams.get('types'))
function navigate(
function buildHref(
nextPathname: string,
updates: Record<string, string | null>,
method: 'push' | 'replace'
updates: Record<string, string | null>
) {
const nextSearchParams = new URLSearchParams(searchParams.toString())
for (const [key, value] of Object.entries(updates)) {
......@@ -45,7 +44,15 @@ export function useAnalyzerRoute(
else nextSearchParams.set(key, value)
}
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>) {
......@@ -73,10 +80,22 @@ export function useAnalyzerRoute(
setView: (view: CompareView) => navigate(pathname, { view }, 'replace'),
setRoute: (route: string | null) =>
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'),
stopComparison: () =>
navigate('/', { from: null, to: null, view: null }, 'push'),
stopComparisonHref: buildHref('/analyze', {
from: null,
to: null,
view: null,
}),
setComparisonSnapshot: (snapshot: SnapshotMetadata | null) =>
navigate(pathname, { to: snapshot?.id ?? null }, 'replace'),
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::{
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 offsets: Vec<u32>,
pub data: Vec<u32>,
......@@ -401,12 +416,34 @@ pub async fn combine_traced_files(
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]
pub async fn analyze_output_assets(
output_assets: Vc<OutputAssets>,
traced_files: Vc<FileSystemPathVec>,
) -> Result<Vc<FileContent>> {
) -> Result<Vc<AnalyzedRoute>> {
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();
......@@ -439,6 +476,7 @@ pub async fn analyze_output_assets(
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 {
filename: filename.clone(),
});
......@@ -460,11 +498,18 @@ pub async fn analyze_output_assets(
};
let source_index = builder.ensure_source(&source).1;
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 {
source_index,
output_file_index,
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_source(source_index, chunk_part_index);
......@@ -493,7 +538,14 @@ pub async fn analyze_output_assets(
}
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]
......@@ -660,7 +712,8 @@ impl AnalyzeDataOutputAsset {
impl Asset for AnalyzeDataOutputAsset {
#[turbo_tasks::function]
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)
}
}
......
use std::{iter::once, sync::Arc};
use std::sync::Arc;
use anyhow::Result;
use next_api::{
analyze::{
AnalyzeDataOutputAsset, ModulesDataOutputAsset, combine_output_assets, combine_traced_files,
AnalyzeDataOutputAsset, ModulesDataOutputAsset, RouteBundleSummary, analyze_output_assets,
combine_output_assets, combine_traced_files,
},
project::ProjectContainer,
route::EndpointGroupKey,
};
use serde::Serialize;
use turbo_tasks::{Effects, ReadRef, ResolvedVc, TryJoinIterExt, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack_core::{
asset::AssetContent,
issue::PlainIssue,
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;
#[turbo_tasks::value(serialization = "skip")]
......@@ -88,7 +99,7 @@ async fn get_analyze_data_operation(
let combined_assets_vc = Vc::cell(combined_output_assets);
let combined_traced_vc = Vc::cell(combined_traced_files);
let analyze_data = endpoint_groups
let analyze_data_and_summaries = endpoint_groups
.iter()
.map(async |(key, endpoint_group)| {
let output_assets = if has_combined
......@@ -123,11 +134,42 @@ async fn get_analyze_data_operation(
.to_resolved()
.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()
.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(
ModulesDataOutputAsset::new(
analyze_output_root.join("modules.data")?,
......@@ -137,11 +179,6 @@ async fn get_analyze_data_operation(
.await?,
);
Ok(Vc::cell(
analyze_data
.iter()
.cloned()
.chain(once(modules_data))
.collect(),
))
analyze_data.extend([route_summaries_asset, modules_data]);
Ok(Vc::cell(analyze_data))
}
......@@ -109,6 +109,7 @@ describe('next analyze', () => {
for (const file of [
'index.html',
'data/routes.json',
'data/route-summaries.json',
'data/modules.data',
'data/analyze.data',
]) {
......@@ -121,6 +122,81 @@ describe('next analyze', () => {
)
const routes = JSON.parse(routesJson)
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