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

bundle-analyzer: Show compressed sizes (#87093)

parent 180af0ce
...@@ -9505,6 +9505,7 @@ version = "0.1.0" ...@@ -9505,6 +9505,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bincode 2.0.1", "bincode 2.0.1",
"flate2",
"rustc-hash 2.1.1", "rustc-hash 2.1.1",
"serde", "serde",
"serde_json", "serde_json",
......
'use client' 'use client'
import type React from 'react' import type React from 'react'
import { SizeMode } from '@/lib/treemap-layout'
import { useEffect, useMemo, useState } from 'react' import { useEffect, useMemo, useState } from 'react'
import useSWR from 'swr' import useSWR from 'swr'
import { ErrorState } from '@/components/error-state' import { ErrorState } from '@/components/error-state'
...@@ -17,12 +19,17 @@ import { computeActiveEntries, computeModuleDepthMap } from '@/lib/module-graph' ...@@ -17,12 +19,17 @@ import { computeActiveEntries, computeModuleDepthMap } from '@/lib/module-graph'
import { fetchStrict } from '@/lib/utils' import { fetchStrict } from '@/lib/utils'
import { formatBytes } from '@/lib/utils' import { formatBytes } from '@/lib/utils'
enum Environment {
Client = 'client',
Server = 'server',
}
export default function Home() { export default function Home() {
const [selectedRoute, setSelectedRoute] = useState<string | null>(null) const [selectedRoute, setSelectedRoute] = useState<string | null>(null)
const [environmentFilter, setEnvironmentFilter] = useState< const [environmentFilter, setEnvironmentFilter] = useState<Environment>(
'client' | 'server' Environment.Client
>('client') )
const [typeFilter, setTypeFilter] = useState<string[]>(['js', 'css', 'json']) const [typeFilter, setTypeFilter] = useState(['js', 'css', 'json'])
const [selectedSourceIndex, setSelectedSourceIndex] = useState<number | null>( const [selectedSourceIndex, setSelectedSourceIndex] = useState<number | null>(
null null
) )
...@@ -69,6 +76,7 @@ export default function Home() { ...@@ -69,6 +76,7 @@ export default function Home() {
client?: boolean client?: boolean
} | null>(null) } | null>(null)
const [searchQuery, setSearchQuery] = useState('') const [searchQuery, setSearchQuery] = useState('')
const [sizeMode, setSizeMode] = useState(SizeMode.Compressed)
useEffect(() => { useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => { const handleKeyDown = (e: KeyboardEvent) => {
...@@ -100,15 +108,15 @@ export default function Home() { ...@@ -100,15 +108,15 @@ export default function Home() {
}, [modulesData, analyzeData]) }, [modulesData, analyzeData])
const filterSource = useMemo(() => { const filterSource = useMemo(() => {
if (!analyzeData) return undefined if (!analyzeData) return () => true
return (sourceIndex: number) => { return (sourceIndex: number) => {
const flags = analyzeData.getSourceFlags(sourceIndex) const flags = analyzeData.getSourceFlags(sourceIndex)
// Check environment filter // Check environment filter
const hasEnvironment = const hasEnvironment =
(environmentFilter === 'client' && flags.client) || (environmentFilter === Environment.Client && flags.client) ||
(environmentFilter === 'server' && flags.server) (environmentFilter === Environment.Server && flags.server)
// Check type filter // Check type filter
const hasType = const hasType =
...@@ -157,25 +165,47 @@ export default function Home() { ...@@ -157,25 +165,47 @@ export default function Home() {
/> />
</div> </div>
<div className="basis-2/3 flex justify-end"> <div className="basis-2/3 flex justify-end items-center space-x-4">
{analyzeData && ( {analyzeData && (
<> <>
<ToggleGroup <ToggleGroup
type="single" type="single"
className="mr-4" value={sizeMode}
onValueChange={(value) => {
if (value) setSizeMode(value as SizeMode)
}}
size="sm"
>
<ToggleGroupItem value={SizeMode.Uncompressed}>
Uncompressed
</ToggleGroupItem>
<ToggleGroupItem value={SizeMode.Compressed}>
Compressed
</ToggleGroupItem>
</ToggleGroup>
<ControlDivider />
<ToggleGroup
type="single"
value={environmentFilter} value={environmentFilter}
onValueChange={(value) => { onValueChange={(value) => {
if (value) setEnvironmentFilter(value as 'client' | 'server') if (value) setEnvironmentFilter(value as Environment)
}} }}
size="sm" size="sm"
> >
<ToggleGroupItem value="client">Client</ToggleGroupItem> <ToggleGroupItem value={Environment.Client}>
<ToggleGroupItem value="server">Server</ToggleGroupItem> Client
</ToggleGroupItem>
<ToggleGroupItem value={Environment.Server}>
Server
</ToggleGroupItem>
</ToggleGroup> </ToggleGroup>
<ControlDivider />
<ToggleGroup <ToggleGroup
type="multiple" type="multiple"
className="mr-4"
value={typeFilter} value={typeFilter}
onValueChange={(value) => { onValueChange={(value) => {
if (value.length > 0) setTypeFilter(value) if (value.length > 0) setTypeFilter(value)
...@@ -188,6 +218,8 @@ export default function Home() { ...@@ -188,6 +218,8 @@ export default function Home() {
<ToggleGroupItem value="asset">Asset</ToggleGroupItem> <ToggleGroupItem value="asset">Asset</ToggleGroupItem>
</ToggleGroup> </ToggleGroup>
<ControlDivider />
<FileSearch value={searchQuery} onChange={setSearchQuery} /> <FileSearch value={searchQuery} onChange={setSearchQuery} />
</> </>
)} )}
...@@ -235,6 +267,7 @@ export default function Home() { ...@@ -235,6 +267,7 @@ export default function Home() {
onHoveredNodeChange={setHoveredNodeInfo} onHoveredNodeChange={setHoveredNodeInfo}
searchQuery={searchQuery} searchQuery={searchQuery}
filterSource={filterSource} filterSource={filterSource}
sizeMode={sizeMode}
/> />
</div> </div>
...@@ -252,6 +285,7 @@ export default function Home() { ...@@ -252,6 +285,7 @@ export default function Home() {
selectedSourceIndex={selectedSourceIndex} selectedSourceIndex={selectedSourceIndex}
moduleDepthMap={moduleDepthMap} moduleDepthMap={moduleDepthMap}
environmentFilter={environmentFilter} environmentFilter={environmentFilter}
filterSource={filterSource}
/> />
</> </>
) : null} ) : null}
...@@ -266,7 +300,7 @@ export default function Home() { ...@@ -266,7 +300,7 @@ export default function Home() {
{hoveredNodeInfo.name} {hoveredNodeInfo.name}
</span> </span>
<span className="ml-2 text-muted-foreground"> <span className="ml-2 text-muted-foreground">
{formatBytes(hoveredNodeInfo.size)} {`${formatBytes(hoveredNodeInfo.size)} ${sizeMode}`}
</span> </span>
{(hoveredNodeInfo.server || hoveredNodeInfo.client) && ( {(hoveredNodeInfo.server || hoveredNodeInfo.client) && (
<span className="ml-2 inline-flex gap-1"> <span className="ml-2 inline-flex gap-1">
...@@ -289,6 +323,10 @@ export default function Home() { ...@@ -289,6 +323,10 @@ export default function Home() {
) )
} }
function ControlDivider() {
return <span className="h-6 w-px bg-muted-foreground/30" />
}
function getRootSourceIndex(analyzeData: AnalyzeData | undefined): number { function getRootSourceIndex(analyzeData: AnalyzeData | undefined): number {
if (!analyzeData) return 0 if (!analyzeData) return 0
const sourceRoots = analyzeData.sourceRoots() const sourceRoots = analyzeData.sourceRoots()
......
'use client' 'use client'
import type React from 'react' import type React from 'react'
import { CircleHelp } from 'lucide-react'
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from './ui/tooltip'
import { ImportChain } from '@/components/import-chain' import { ImportChain } from '@/components/import-chain'
import { Skeleton } from '@/components/ui/skeleton' import { Skeleton } from '@/components/ui/skeleton'
import { AnalyzeData, ModulesData } from '@/lib/analyze-data' import { AnalyzeData, ModulesData } from '@/lib/analyze-data'
...@@ -15,6 +22,7 @@ interface SidebarProps { ...@@ -15,6 +22,7 @@ interface SidebarProps {
selectedSourceIndex: number | null selectedSourceIndex: number | null
moduleDepthMap: Map<number, number> moduleDepthMap: Map<number, number>
environmentFilter: 'client' | 'server' environmentFilter: 'client' | 'server'
filterSource?: (sourceIndex: number) => boolean
isLoading?: boolean isLoading?: boolean
} }
...@@ -34,9 +42,12 @@ export function Sidebar({ ...@@ -34,9 +42,12 @@ export function Sidebar({
selectedSourceIndex, selectedSourceIndex,
moduleDepthMap, moduleDepthMap,
environmentFilter, environmentFilter,
filterSource,
isLoading = false, isLoading = false,
}: SidebarProps) { }: SidebarProps) {
if (isLoading) { filterSource = filterSource ?? (() => true)
if (isLoading || !analyzeData) {
return ( return (
<div <div
className="flex-none bg-muted border-l border-border overflow-y-auto" className="flex-none bg-muted border-l border-border overflow-y-auto"
...@@ -56,10 +67,40 @@ export function Sidebar({ ...@@ -56,10 +67,40 @@ export function Sidebar({
) )
} }
if (!analyzeData) { return (
return null <div
} className="flex-none bg-muted border-l border-border overflow-y-auto"
style={{ width: `${sidebarWidth}%` }}
>
{selectedSourceIndex != null ? (
<SelectionDetails
analyzeData={analyzeData}
modulesData={modulesData}
selectedSourceIndex={selectedSourceIndex}
filterSource={filterSource}
moduleDepthMap={moduleDepthMap}
environmentFilter={environmentFilter}
/>
) : null}
</div>
)
}
function SelectionDetails({
analyzeData,
modulesData,
selectedSourceIndex,
filterSource,
moduleDepthMap,
environmentFilter,
}: {
analyzeData: AnalyzeData
modulesData: ModulesData | null
selectedSourceIndex: number
moduleDepthMap: Map<number, number>
environmentFilter: 'client' | 'server'
filterSource: (sourceIndex: number) => boolean
}) {
const specialModuleType = getSpecialModuleType( const specialModuleType = getSpecialModuleType(
analyzeData, analyzeData,
selectedSourceIndex selectedSourceIndex
...@@ -76,101 +117,122 @@ export function Sidebar({ ...@@ -76,101 +117,122 @@ export function Sidebar({
const childModuleCount = const childModuleCount =
hasChildModules && selectedSourceIndex != null hasChildModules && selectedSourceIndex != null
? analyzeData.getSourceRecursiveModuleCount(selectedSourceIndex) ? analyzeData.getRecursiveModuleCount(selectedSourceIndex, filterSource)
: null : null
return ( const { size, compressedSize } = analyzeData.getRecursiveSizes(
<div selectedSourceIndex,
className="flex-none bg-muted border-l border-border overflow-y-auto" filterSource
style={{ width: `${sidebarWidth}%` }} )
>
<div className="flex-1 p-3 space-y-8 overflow-y-auto">
<div className="space-y-2">
<h2 className="text-s font-semibold mb-1 text-foreground truncate">
{selectedSource
? selectedSource.path || 'All Route Modules'
: 'Unknown Source'}
</h2>
{selectedSourceIndex != null &&
analyzeData.source(selectedSourceIndex) ? (
<div>
<div className="text-xs">
<span>
{hasChildModules
? formatBytes(
analyzeData.getSourceRecursiveSize(selectedSourceIndex)
)
: formatBytes(
analyzeData.getSourceOutputSize(selectedSourceIndex)
)}
</span>{' '}
<span className="text-muted-foreground">bundled</span>
</div>
{hasChildModules && childModuleCount != null ? (
<div className="text-xs">
<span>{childModuleCount} </span>
<span className="text-muted-foreground">
{childModuleCount === 1 ? 'module' : 'modules'}
</span>
</div>
) : null}
</div>
) : null}
</div>
{selectedSourceIndex != null && const chunks =
analyzeData.source(selectedSourceIndex) && selectedSourceIndex != null
(specialModuleType === SpecialModule.POLYFILL_MODULE || ? analyzeData.sourceChunks(selectedSourceIndex)
specialModuleType === SpecialModule.POLYFILL_NOMODULE) && ( : []
<dl>
<div className="flex items-center gap-2">
<dt className="inline-flex items-center">
<Badge variant="polyfill">Polyfill</Badge>
</dt>
<dd className="text-xs text-muted-foreground">
Next.js built-in polyfills
</dd>
</div>
</dl>
)}
return (
<div className="flex-1 p-3 space-y-8 overflow-y-auto">
<div className="space-y-2">
<h2 className="text-s font-semibold mb-1 text-foreground truncate">
{selectedSource?.path || 'All Route Modules'}
</h2>
{selectedSourceIndex != null && {selectedSourceIndex != null &&
analyzeData.source(selectedSourceIndex) && analyzeData.source(selectedSourceIndex) ? (
!hasChildModules && ( <div className="text-xs">
<div>
<span>{formatBytes(size)}</span>{' '}
<span className="text-muted-foreground">uncompressed</span>
<InlineHelpTooltip>
Uncompressed modules may still be minified, tree-shaken, and
dead-code eliminated. They just don't account for general
compression like gzip.
</InlineHelpTooltip>
</div>
<> <>
{modulesData && ( <div>
<ImportChain <span className="text-muted-foreground">About </span>
key={selectedSourceIndex} <span>{formatBytes(compressedSize)}</span>
startFileId={selectedSourceIndex} <span className="text-muted-foreground ml-1">compressed</span>
analyzeData={analyzeData} <InlineHelpTooltip>
modulesData={modulesData} Estimated compressed size. Modules are compressed in isolation
depthMap={moduleDepthMap} which may differ from their size in the final chunk.
environmentFilter={environmentFilter} </InlineHelpTooltip>
/> </div>
)}
{(() => {
const chunks = analyzeData.sourceChunks(selectedSourceIndex)
if (chunks.length > 0) {
return (
<div className="mt-2">
<p className="text-xs font-semibold text-foreground">
Output Chunks
</p>
<ul className="text-xs text-muted-foreground font-mono mt-1 space-y-1">
{chunks.map((chunk) => (
<li key={chunk} className="break-all">
{chunk}
</li>
))}
</ul>
</div>
)
}
return null
})()}
</> </>
)} {hasChildModules && childModuleCount != null ? (
<div>
<span>{childModuleCount} </span>
<span className="text-muted-foreground">
{childModuleCount === 1 ? 'module' : 'modules'}
</span>
</div>
) : null}
</div>
) : null}
</div> </div>
{selectedSourceIndex != null &&
analyzeData.source(selectedSourceIndex) &&
(specialModuleType === SpecialModule.POLYFILL_MODULE ||
specialModuleType === SpecialModule.POLYFILL_NOMODULE) && (
<dl className="flex items-center gap-2">
<dt className="inline-flex items-center">
<Badge variant="polyfill">Polyfill</Badge>
</dt>
<dd className="text-xs text-muted-foreground">
Next.js built-in polyfills
</dd>
</dl>
)}
{selectedSourceIndex != null &&
analyzeData.source(selectedSourceIndex) &&
!hasChildModules && (
<>
{modulesData && (
<ImportChain
startFileId={selectedSourceIndex}
analyzeData={analyzeData}
modulesData={modulesData}
depthMap={moduleDepthMap}
environmentFilter={environmentFilter}
/>
)}
{chunks.length > 0 ? (
<div className="mt-2">
<p className="text-xs font-semibold text-foreground">
Output Chunks
</p>
<ul className="text-xs text-muted-foreground font-mono mt-1 space-y-1">
{chunks.map((chunk) => (
<li key={chunk} className="break-all">
{chunk}
</li>
))}
</ul>
</div>
) : null}
</>
)}
</div> </div>
) )
} }
function InlineHelpTooltip({ children }: { children: React.ReactNode }) {
return (
<TooltipProvider>
<Tooltip>
<TooltipTrigger asChild>
<CircleHelp
size={14}
className="inline-block ml-1 text-muted-foreground"
aria-hidden="true"
/>
</TooltipTrigger>
<TooltipContent className="max-w-xs" side="top" align="center">
{children}
</TooltipContent>
</Tooltip>
</TooltipProvider>
)
}
...@@ -8,6 +8,7 @@ import { ...@@ -8,6 +8,7 @@ import {
computeTreemapLayoutFromAnalyze, computeTreemapLayoutFromAnalyze,
type LayoutNode, type LayoutNode,
type LayoutNodeInfo, type LayoutNodeInfo,
SizeMode,
} from '@/lib/treemap-layout' } from '@/lib/treemap-layout'
import { SpecialModule } from '@/lib/types' import { SpecialModule } from '@/lib/types'
import { formatBytes } from '@/lib/utils' import { formatBytes } from '@/lib/utils'
...@@ -27,6 +28,7 @@ interface TreemapVisualizerProps { ...@@ -27,6 +28,7 @@ interface TreemapVisualizerProps {
filterSource?: (sourceIndex: number) => boolean filterSource?: (sourceIndex: number) => boolean
isModulePolyfillChunk?: (sourceIndex: number) => boolean isModulePolyfillChunk?: (sourceIndex: number) => boolean
isNoModulePolyfillChunk?: (sourceIndex: number) => boolean isNoModulePolyfillChunk?: (sourceIndex: number) => boolean
sizeMode?: SizeMode
} }
function getFileColor(node: { function getFileColor(node: {
...@@ -70,6 +72,77 @@ function isPolyfill(specialModuleType: SpecialModule | null): boolean { ...@@ -70,6 +72,77 @@ function isPolyfill(specialModuleType: SpecialModule | null): boolean {
) )
} }
function calculateTitleFontSizes(titleBarHeight: number): {
titleFontSize: number
sizeFontSize: number
} {
const titleFontSize = Math.min(10, titleBarHeight * 0.5)
const sizeFontSize = Math.min(9, titleFontSize - 2)
return { titleFontSize, sizeFontSize }
}
const textWidthCache = new Map<string, number>()
const TEXT_WIDTH_CACHE_SIZE = 30_000 // Shouldn't be more than a few megabytes of memory
function measureTextCached(
ctx: CanvasRenderingContext2D,
text: string
): number {
const cacheKey = `${ctx.font}|${text}`
let width = textWidthCache.get(cacheKey)
if (width !== undefined) {
// Move to end -- update the insertion order for LRU
textWidthCache.delete(cacheKey)
textWidthCache.set(cacheKey, width)
return width
}
width = ctx.measureText(text).width
// LRU-style cache eviction
if (textWidthCache.size >= TEXT_WIDTH_CACHE_SIZE) {
const firstKey = textWidthCache.keys().next().value
if (firstKey !== undefined) {
textWidthCache.delete(firstKey)
}
}
textWidthCache.set(cacheKey, width)
return width
}
function truncateTextWithEllipsisIfNeeded(
ctx: CanvasRenderingContext2D,
text: string,
maxWidth: number
): string {
const ellipsisWidth = measureTextCached(ctx, '...')
if (measureTextCached(ctx, text) <= maxWidth) {
return text
}
let left = 0
let right = text.length
let bestLength = 0
while (left <= right) {
const mid = Math.floor((left + right) / 2)
const truncated = text.slice(0, mid)
// Don't use the cached version since we don't want repeated failures filling it up
const width = ctx.measureText(truncated).width
if (width + ellipsisWidth <= maxWidth) {
bestLength = mid
left = mid + 1
} else {
right = mid - 1
}
}
return bestLength > 0 ? `${text.slice(0, bestLength)}...` : '...'
}
function findNodeAtPosition( function findNodeAtPosition(
node: LayoutNode, node: LayoutNode,
x: number, x: number,
...@@ -232,7 +305,7 @@ function drawTreemap( ...@@ -232,7 +305,7 @@ function drawTreemap(
ctx.lineTo(rect.x + rect.width, rect.y + titleBarHeight) ctx.lineTo(rect.x + rect.width, rect.y + titleBarHeight)
ctx.stroke() ctx.stroke()
const titleFontSize = Math.max(10, titleBarHeight * 0.5) const { titleFontSize } = calculateTitleFontSizes(titleBarHeight)
ctx.fillStyle = colors.text ctx.fillStyle = colors.text
ctx.font = `600 ${titleFontSize}px sans-serif` ctx.font = `600 ${titleFontSize}px sans-serif`
ctx.textAlign = 'left' ctx.textAlign = 'left'
...@@ -342,7 +415,6 @@ function drawTreemap( ...@@ -342,7 +415,6 @@ function drawTreemap(
ctx.textBaseline = 'middle' ctx.textBaseline = 'middle'
const maxWidth = rect.width - 8 const maxWidth = rect.width - 8
let displayName = name
const sizeText = formatBytes(node.size) const sizeText = formatBytes(node.size)
const fontSize = 12 const fontSize = 12
...@@ -353,17 +425,7 @@ function drawTreemap( ...@@ -353,17 +425,7 @@ function drawTreemap(
const hasSpaceForSize = rect.height > 50 const hasSpaceForSize = rect.height > 50
ctx.font = `${fontSize}px sans-serif` ctx.font = `${fontSize}px sans-serif`
const nameMetrics = ctx.measureText(displayName) const displayName = truncateTextWithEllipsisIfNeeded(ctx, name, maxWidth)
if (nameMetrics.width > maxWidth) {
while (
displayName.length > 0 &&
ctx.measureText(`${displayName}...`).width > maxWidth
) {
displayName = displayName.slice(0, -1)
}
displayName += '...'
}
if (hasSpaceForSize) { if (hasSpaceForSize) {
ctx.font = `${fontSize}px sans-serif` ctx.font = `${fontSize}px sans-serif`
...@@ -426,17 +488,40 @@ function drawTreemap( ...@@ -426,17 +488,40 @@ function drawTreemap(
ctx.lineTo(rect.x + rect.width, rect.y + titleBarHeight) ctx.lineTo(rect.x + rect.width, rect.y + titleBarHeight)
ctx.stroke() ctx.stroke()
const titleFontSize = Math.max(10, titleBarHeight * 0.5) const { titleFontSize, sizeFontSize } =
ctx.fillStyle = colors.text calculateTitleFontSizes(titleBarHeight)
ctx.font = `600 ${titleFontSize}px sans-serif` const sizeText = formatBytes(node.size)
ctx.textAlign = 'left' const centerY = rect.y + titleBarHeight / 2
const gap = 6
ctx.textBaseline = 'middle' ctx.textBaseline = 'middle'
ctx.fillText(
// Measure size text first to reserve space
ctx.font = `${sizeFontSize}px sans-serif`
const sizeWidth = measureTextCached(ctx, sizeText)
const nameX = rect.x + 8
const availableNameWidth = Math.max(0, rect.width - 16 - sizeWidth - gap)
ctx.font = `600 ${titleFontSize}px sans-serif`
const displayName = truncateTextWithEllipsisIfNeeded(
ctx,
name, name,
rect.x + 8, availableNameWidth
rect.y + titleBarHeight / 2,
rect.width - 16
) )
ctx.fillStyle = colors.text
ctx.textAlign = 'left'
ctx.fillText(displayName, nameX, centerY, availableNameWidth)
const nameWidth = measureTextCached(ctx, displayName)
const sizeX = nameX + nameWidth + gap
// Only draw size text if it fits within bounds
if (sizeX + sizeWidth <= rect.x + rect.width - 8) {
ctx.font = `${sizeFontSize}px sans-serif`
ctx.fillStyle = colors.textMuted
ctx.fillText(sizeText, sizeX, centerY)
}
} }
ctx.globalAlpha = 1.0 ctx.globalAlpha = 1.0
...@@ -473,17 +558,39 @@ function drawTreemap( ...@@ -473,17 +558,39 @@ function drawTreemap(
ctx.lineTo(rect.x + rect.width, rect.y + titleBarHeight) ctx.lineTo(rect.x + rect.width, rect.y + titleBarHeight)
ctx.stroke() ctx.stroke()
const titleFontSize = Math.max(10, titleBarHeight * 0.5) const { titleFontSize, sizeFontSize } =
ctx.fillStyle = colors.text calculateTitleFontSizes(titleBarHeight)
ctx.font = `600 ${titleFontSize}px sans-serif` const sizeText = formatBytes(node.size)
ctx.textAlign = 'left' const centerY = rect.y + titleBarHeight / 2
const gap = 6
ctx.textBaseline = 'middle' ctx.textBaseline = 'middle'
ctx.fillText(
ctx.font = `${sizeFontSize}px sans-serif`
const sizeWidth = measureTextCached(ctx, sizeText)
const nameX = rect.x + 8
const availableNameWidth = Math.max(0, rect.width - 16 - sizeWidth - gap)
ctx.font = `600 ${titleFontSize}px sans-serif`
const displayName = truncateTextWithEllipsisIfNeeded(
ctx,
name, name,
rect.x + 8, availableNameWidth
rect.y + titleBarHeight / 2,
rect.width - 16
) )
ctx.fillStyle = colors.text
ctx.textAlign = 'left'
ctx.fillText(displayName, nameX, centerY, availableNameWidth)
const nameWidth = measureTextCached(ctx, displayName)
const sizeX = nameX + nameWidth + gap
// Only draw size text if it fits within bounds
if (sizeX + sizeWidth <= rect.x + rect.width - 8) {
ctx.font = `${sizeFontSize}px sans-serif`
ctx.fillStyle = colors.textMuted
ctx.fillText(sizeText, sizeX, centerY)
}
} }
ctx.globalAlpha = 1.0 ctx.globalAlpha = 1.0
...@@ -604,6 +711,7 @@ export function TreemapVisualizer({ ...@@ -604,6 +711,7 @@ export function TreemapVisualizer({
onHoveredNodeChangeDelayed, onHoveredNodeChangeDelayed,
searchQuery = '', searchQuery = '',
filterSource, filterSource,
sizeMode = SizeMode.Compressed,
}: TreemapVisualizerProps) { }: TreemapVisualizerProps) {
const canvasRef = useRef<HTMLCanvasElement>(null) const canvasRef = useRef<HTMLCanvasElement>(null)
const containerRef = useRef<HTMLDivElement>(null) const containerRef = useRef<HTMLDivElement>(null)
...@@ -708,7 +816,8 @@ export function TreemapVisualizer({ ...@@ -708,7 +816,8 @@ export function TreemapVisualizer({
width: cssDimensions.width, width: cssDimensions.width,
height: cssDimensions.height, height: cssDimensions.height,
}, },
filterSource filterSource,
sizeMode
) )
// If we're not at the root, wrap with ancestor title bars // If we're not at the root, wrap with ancestor title bars
...@@ -731,6 +840,7 @@ export function TreemapVisualizer({ ...@@ -731,6 +840,7 @@ export function TreemapVisualizer({
cssDimensions.width, cssDimensions.width,
cssDimensions.height, cssDimensions.height,
filterSource, filterSource,
sizeMode,
]) ])
useEffect(() => { useEffect(() => {
...@@ -917,16 +1027,16 @@ function getThemeColors() { ...@@ -917,16 +1027,16 @@ function getThemeColors() {
return { return {
text: dark ? '#ffffff' : '#000000', text: dark ? '#ffffff' : '#000000',
textMuted: dark ? 'rgba(255, 255, 255, 0.6)' : 'rgba(0, 0, 0, 0.6)', textMuted: dark ? 'rgba(255, 255, 255, 0.6)' : 'rgba(0, 0, 0, 0.6)',
border: dark ? 'rgba(255, 255, 255, 0.2)' : 'rgba(128, 128, 128, 0.2)', border: dark ? 'rgba(255, 255, 255, 0.4)' : 'rgba(180, 180, 180, 0.5)',
dirBg: dark ? 'rgba(255, 255, 255, 0.05)' : 'rgba(128, 128, 128, 0.1)', dirBg: dark ? 'rgba(255, 255, 255, 0.05)' : 'rgba(230, 230, 230, 0.1)',
dirBorder: dark ? 'rgba(255, 255, 255, 0.2)' : 'rgba(128, 128, 128, 0.3)', dirBorder: dark ? 'rgba(255, 255, 255, 0.5)' : 'rgba(180, 180, 180, 0.6)',
dirTitleBg: dark ? 'rgba(255, 255, 255, 0.08)' : 'rgba(128, 128, 128, 0.1)', dirTitleBg: dark ? 'rgba(255, 255, 255, 0.08)' : 'rgba(230, 230, 230, 0.1)',
dirTitleBorder: dark dirTitleBorder: dark
? 'rgba(255, 255, 255, 0.15)' ? 'rgba(255, 255, 255, 0.4)'
: 'rgba(128, 128, 128, 0.2)', : 'rgba(180, 180, 180, 0.5)',
collapsedBg: dark collapsedBg: dark
? 'rgba(128, 128, 128, 0.15)' ? 'rgba(128, 128, 128, 0.15)'
: 'rgba(128, 128, 128, 0.2)', : 'rgba(230, 230, 230, 0.2)',
collapsedText: dark collapsedText: dark
? 'rgba(255, 255, 255, 0.5)' ? 'rgba(255, 255, 255, 0.5)'
: 'rgba(128, 128, 128, 0.6)', : 'rgba(128, 128, 128, 0.6)',
......
'use client'
import * as TooltipPrimitive from '@radix-ui/react-tooltip'
import * as React from 'react'
import { cn } from '@/lib/utils'
const TooltipProvider = TooltipPrimitive.Provider
const Tooltip = TooltipPrimitive.Root
const TooltipTrigger = TooltipPrimitive.Trigger
const TooltipContent = React.forwardRef<
React.ElementRef<typeof TooltipPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof TooltipPrimitive.Content>
>(({ className, sideOffset = 4, ...props }, ref) => (
<TooltipPrimitive.Portal>
<TooltipPrimitive.Content
ref={ref}
sideOffset={sideOffset}
className={cn(
'z-50 overflow-hidden rounded-md border bg-popover px-3 py-1.5 text-xs text-popover-foreground shadow-md outline-none data-[state=closed]:animate-out data-[state=open]:animate-in data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2',
className
)}
{...props}
/>
</TooltipPrimitive.Portal>
))
TooltipContent.displayName = TooltipPrimitive.Content.displayName
export { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider }
...@@ -18,6 +18,7 @@ export interface AnalyzeChunkPart { ...@@ -18,6 +18,7 @@ export interface AnalyzeChunkPart {
source_index: number source_index: number
output_file_index: number output_file_index: number
size: number size: number
compressed_size: number
} }
export interface AnalyzeOutputFile { export interface AnalyzeOutputFile {
...@@ -336,29 +337,31 @@ export class AnalyzeData { ...@@ -336,29 +337,31 @@ export class AnalyzeData {
return parentPath + source.path return parentPath + source.path
} }
getSourceOutputSize(index: SourceIndex): number { getOwnSizes(index: SourceIndex): {
size: number
compressedSize: number
} {
const chunkParts = this.sourceChunkParts(index) const chunkParts = this.sourceChunkParts(index)
let totalSize = 0 let size = 0
let compressedSize = 0
for (const chunkPartIndex of chunkParts) { for (const chunkPartIndex of chunkParts) {
const chunkPart = this.chunkPart(chunkPartIndex) const chunkPart = this.chunkPart(chunkPartIndex)
if (chunkPart) { if (chunkPart) {
totalSize += chunkPart.size size += chunkPart.size
compressedSize += chunkPart.compressed_size
} }
} }
return totalSize return { size, compressedSize }
} }
getSourceRecursiveSize(index: SourceIndex): number { getRecursiveModuleCount(
let totalSize = this.getSourceOutputSize(index) index: SourceIndex,
const children = this.sourceChildren(index) filterSource: (sourceIndex: SourceIndex) => boolean
for (const childIndex of children) { ): number {
totalSize += this.getSourceRecursiveSize(childIndex) const selfVisible = filterSource(index)
} const selfCount =
return totalSize selfVisible && this.sourceChunkParts(index).length > 0 ? 1 : 0
}
getSourceRecursiveModuleCount(index: SourceIndex): number {
const selfCount = this.sourceChunkParts(index).length > 0 ? 1 : 0
const children = this.sourceChildren(index) const children = this.sourceChildren(index)
if (children.length === 0) { if (children.length === 0) {
return selfCount return selfCount
...@@ -366,7 +369,7 @@ export class AnalyzeData { ...@@ -366,7 +369,7 @@ export class AnalyzeData {
let totalCount = selfCount let totalCount = selfCount
for (const childIndex of children) { for (const childIndex of children) {
totalCount += this.getSourceRecursiveModuleCount(childIndex) totalCount += this.getRecursiveModuleCount(childIndex, filterSource)
} }
return totalCount return totalCount
} }
...@@ -388,6 +391,35 @@ export class AnalyzeData { ...@@ -388,6 +391,35 @@ export class AnalyzeData {
return Array.from(uniqueChunks).sort() return Array.from(uniqueChunks).sort()
} }
getRecursiveSizes(
index: SourceIndex,
filterSource: (sourceIndex: SourceIndex) => boolean
): { size: number; compressedSize: number } {
let size = 0
let compressedSize = 0
if (filterSource(index)) {
const { size: ownUncompressedSize, compressedSize: ownCompressedSize } =
this.getOwnSizes(index)
size += ownUncompressedSize
compressedSize += ownCompressedSize
}
for (const childIndex of this.sourceChildren(index)) {
const {
size: childUncompressedSize,
compressedSize: childCompressedSize,
} = this.getRecursiveSizes(childIndex, filterSource)
size += childUncompressedSize
compressedSize += childCompressedSize
}
return {
size,
compressedSize,
}
}
getSourceFlags(index: SourceIndex): { getSourceFlags(index: SourceIndex): {
client: boolean client: boolean
server: boolean server: boolean
......
...@@ -35,7 +35,13 @@ export interface LayoutNode extends LayoutNodeInfo { ...@@ -35,7 +35,13 @@ export interface LayoutNode extends LayoutNodeInfo {
interface SourceMetadata { interface SourceMetadata {
filtered: boolean filtered: boolean
totalSize: number size: number
compressedSize: number
}
export enum SizeMode {
Compressed = 'compressed',
Uncompressed = 'uncompressed',
} }
function precomputeSourceMetadata( function precomputeSourceMetadata(
...@@ -45,23 +51,24 @@ function precomputeSourceMetadata( ...@@ -45,23 +51,24 @@ function precomputeSourceMetadata(
const sourceCount = analyzeData.sourceCount() const sourceCount = analyzeData.sourceCount()
const metadata: SourceMetadata[] = new Array(sourceCount) const metadata: SourceMetadata[] = new Array(sourceCount)
// Initialize all entries
for (let i = 0; i < sourceCount; i++) {
metadata[i] = { filtered: true, totalSize: 0 }
}
// Bottom-up pass: compute leaf node data
for (let i = sourceCount - 1; i >= 0; i--) { for (let i = sourceCount - 1; i >= 0; i--) {
const children = analyzeData.sourceChildren(i) const children = analyzeData.sourceChildren(i)
const ownSize = analyzeData.getSourceOutputSize(i) const ownSize = analyzeData.getOwnSizes(i)
if (children.length === 0) { if (children.length === 0) {
// Leaf node (file) // file
metadata[i].totalSize = ownSize metadata[i] = {
metadata[i].filtered = filterSource ? !filterSource(i) : false size: ownSize.size,
compressedSize: ownSize.compressedSize,
filtered: filterSource ? !filterSource(i) : false,
}
} else { } else {
// Directory - initialize with own size // directory
metadata[i].totalSize = ownSize metadata[i] = {
filtered: true,
size: ownSize.size,
compressedSize: ownSize.compressedSize,
}
} }
} }
...@@ -70,19 +77,22 @@ function precomputeSourceMetadata( ...@@ -70,19 +77,22 @@ function precomputeSourceMetadata(
const children = analyzeData.sourceChildren(idx) const children = analyzeData.sourceChildren(idx)
if (children.length === 0) return // Already processed as leaf if (children.length === 0) return // Already processed as leaf
let totalSize = metadata[idx].totalSize // Start with own size let totalUncompressedSize = metadata[idx].size
let totalCompressedSize = metadata[idx].compressedSize
let hasVisibleChild = false let hasVisibleChild = false
for (const childIdx of children) { for (const childIdx of children) {
processDirectory(childIdx) // Process child first processDirectory(childIdx) // Process child first
if (!metadata[childIdx].filtered) { if (!metadata[childIdx].filtered) {
// Only add size of visible (non-filtered) children // Only add size of visible (non-filtered) children
totalSize += metadata[childIdx].totalSize totalUncompressedSize += metadata[childIdx].size
totalCompressedSize += metadata[childIdx].compressedSize
hasVisibleChild = true hasVisibleChild = true
} }
} }
metadata[idx].totalSize = totalSize metadata[idx].size = totalUncompressedSize
metadata[idx].compressedSize = totalCompressedSize
metadata[idx].filtered = !hasVisibleChild // Directory filtered if no visible children metadata[idx].filtered = !hasVisibleChild // Directory filtered if no visible children
} }
...@@ -102,7 +112,8 @@ function computeTreemapLayoutFromAnalyzeInternal( ...@@ -102,7 +112,8 @@ function computeTreemapLayoutFromAnalyzeInternal(
foldedPath: string, foldedPath: string,
rect: LayoutRect, rect: LayoutRect,
metadata: SourceMetadata[], metadata: SourceMetadata[],
filterSource?: (sourceIndex: SourceIndex) => boolean filterSource: ((sourceIndex: SourceIndex) => boolean) | undefined,
sizeMode: SizeMode
): LayoutNode { ): LayoutNode {
const source = analyzeData.source(sourceIndex) const source = analyzeData.source(sourceIndex)
if (!source) { if (!source) {
...@@ -111,7 +122,6 @@ function computeTreemapLayoutFromAnalyzeInternal( ...@@ -111,7 +122,6 @@ function computeTreemapLayoutFromAnalyzeInternal(
const isDirectory = source.path.endsWith('/') || !source.path const isDirectory = source.path.endsWith('/') || !source.path
// Get children sources
const childrenIndices = analyzeData.sourceChildren(sourceIndex) const childrenIndices = analyzeData.sourceChildren(sourceIndex)
// Fold single-child directories // Fold single-child directories
...@@ -129,13 +139,16 @@ function computeTreemapLayoutFromAnalyzeInternal( ...@@ -129,13 +139,16 @@ function computeTreemapLayoutFromAnalyzeInternal(
foldedPath + source.path, foldedPath + source.path,
rect, rect,
metadata, metadata,
filterSource filterSource,
sizeMode
) )
} }
} }
// Use precomputed size const totalSize =
const totalSize = metadata[sourceIndex].totalSize sizeMode === SizeMode.Compressed
? metadata[sourceIndex].compressedSize
: metadata[sourceIndex].size
// If this is a file (no children), create a file node // If this is a file (no children), create a file node
if (!isDirectory || childrenIndices.length === 0) { if (!isDirectory || childrenIndices.length === 0) {
...@@ -150,7 +163,9 @@ function computeTreemapLayoutFromAnalyzeInternal( ...@@ -150,7 +163,9 @@ function computeTreemapLayoutFromAnalyzeInternal(
} }
} }
// This is a directory with children const directoryName = foldedPath + source.path || 'All Route Modules'
// Directory with children
const titleBarHeight = Math.round( const titleBarHeight = Math.round(
Math.max(12, Math.min(24, rect.height * 0.1)) Math.max(12, Math.min(24, rect.height * 0.1))
) )
...@@ -175,7 +190,7 @@ function computeTreemapLayoutFromAnalyzeInternal( ...@@ -175,7 +190,7 @@ function computeTreemapLayoutFromAnalyzeInternal(
} }
return { return {
name: foldedPath + source.path, name: directoryName,
size: totalSize, size: totalSize,
type: 'collapsed-directory', type: 'collapsed-directory',
rect, rect,
...@@ -199,8 +214,11 @@ function computeTreemapLayoutFromAnalyzeInternal( ...@@ -199,8 +214,11 @@ function computeTreemapLayoutFromAnalyzeInternal(
continue continue
} }
// Use precomputed size // Use precomputed size based on mode
const childSize = metadata[childIndex].totalSize const childSize =
sizeMode === SizeMode.Compressed
? metadata[childIndex].compressedSize
: metadata[childIndex].size
childrenData.push({ childrenData.push({
index: childIndex, index: childIndex,
...@@ -210,7 +228,7 @@ function computeTreemapLayoutFromAnalyzeInternal( ...@@ -210,7 +228,7 @@ function computeTreemapLayoutFromAnalyzeInternal(
if (childrenData.length === 0) { if (childrenData.length === 0) {
return { return {
name: foldedPath + source.path, name: directoryName,
size: totalSize, size: totalSize,
type: 'directory', type: 'directory',
rect, rect,
...@@ -235,12 +253,13 @@ function computeTreemapLayoutFromAnalyzeInternal( ...@@ -235,12 +253,13 @@ function computeTreemapLayoutFromAnalyzeInternal(
'', '',
childRects[i], childRects[i],
metadata, metadata,
filterSource filterSource,
sizeMode
) )
) )
return { return {
name: foldedPath + source.path, name: directoryName,
size: totalSize, size: totalSize,
type: 'directory', type: 'directory',
rect, rect,
...@@ -256,7 +275,8 @@ export function computeTreemapLayoutFromAnalyze( ...@@ -256,7 +275,8 @@ export function computeTreemapLayoutFromAnalyze(
analyzeData: AnalyzeData, analyzeData: AnalyzeData,
sourceIndex: SourceIndex, sourceIndex: SourceIndex,
rect: LayoutRect, rect: LayoutRect,
filterSource?: (sourceIndex: SourceIndex) => boolean filterSource?: (sourceIndex: SourceIndex) => boolean,
sizeMode: SizeMode = SizeMode.Compressed
): LayoutNode { ): LayoutNode {
// Precompute metadata once for entire tree // Precompute metadata once for entire tree
const metadata = precomputeSourceMetadata(analyzeData, filterSource) const metadata = precomputeSourceMetadata(analyzeData, filterSource)
...@@ -268,6 +288,7 @@ export function computeTreemapLayoutFromAnalyze( ...@@ -268,6 +288,7 @@ export function computeTreemapLayoutFromAnalyze(
'', '',
rect, rect,
metadata, metadata,
filterSource filterSource,
sizeMode
) )
} }
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
"dependencies": { "dependencies": {
"@radix-ui/react-dialog": "1.1.4", "@radix-ui/react-dialog": "1.1.4",
"@radix-ui/react-popover": "1.1.4", "@radix-ui/react-popover": "1.1.4",
"@radix-ui/react-tooltip": "1.1.4",
"@radix-ui/react-slot": "1.1.1", "@radix-ui/react-slot": "1.1.1",
"@radix-ui/react-toggle-group": "1.1.1", "@radix-ui/react-toggle-group": "1.1.1",
"autoprefixer": "^10.4.20", "autoprefixer": "^10.4.20",
......
...@@ -75,6 +75,7 @@ pub struct AnalyzeChunkPart { ...@@ -75,6 +75,7 @@ pub struct AnalyzeChunkPart {
pub source_index: u32, pub source_index: u32,
pub output_file_index: u32, pub output_file_index: u32,
pub size: u32, pub size: u32,
pub compressed_size: u32,
} }
#[derive(Serialize)] #[derive(Serialize)]
...@@ -384,6 +385,7 @@ pub async fn analyze_output_assets(output_assets: Vc<OutputAssets>) -> Result<Vc ...@@ -384,6 +385,7 @@ pub async fn analyze_output_assets(output_assets: Vc<OutputAssets>) -> Result<Vc
source_index, source_index,
output_file_index, output_file_index,
size: chunk_part.real_size + chunk_part.unaccounted_size, size: chunk_part.real_size + chunk_part.unaccounted_size,
compressed_size: chunk_part.get_compressed_size().await?,
}); });
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);
......
This diff is collapsed.
...@@ -11,6 +11,7 @@ bench = false ...@@ -11,6 +11,7 @@ bench = false
[dependencies] [dependencies]
anyhow = { workspace = true } anyhow = { workspace = true }
bincode = { workspace = true } bincode = { workspace = true }
flate2 = { workspace = true }
rustc-hash = { workspace = true } rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] } serde = { workspace = true, features = ["derive"] }
turbo-rcstr = { workspace = true } turbo-rcstr = { workspace = true }
......
use std::io::Write;
use anyhow::Result;
use flate2::{Compression, write::DeflateEncoder};
use turbo_rcstr::RcStr;
/// Compresses an asset's content with default-level (level 6) deflate/gzip.
/// Returns the size in bytes in the compressed output
pub fn compressed_size_bytes(content: RcStr) -> Result<u32> {
// Use deflate over gzip to prevent individual file headers/footers from
// skewing the size results.
let mut encoder = DeflateEncoder::new(Vec::new(), Compression::default());
encoder.write_all(content.as_ref())?;
let compressed = encoder.finish()?;
Ok(compressed.len() as u32)
}
#![feature(arbitrary_self_types_pointers)] #![feature(arbitrary_self_types_pointers)]
pub mod compressed_size;
pub mod split_chunk; pub mod split_chunk;
...@@ -10,7 +10,7 @@ use turbo_tasks_fs::{ ...@@ -10,7 +10,7 @@ use turbo_tasks_fs::{
File, FileContent, FileSystem, FileSystemPath, VirtualFileSystem, rope::Rope, File, FileContent, FileSystem, FileSystemPath, VirtualFileSystem, rope::Rope,
}; };
use turbo_tasks_testing::{Registration, register, run_once}; use turbo_tasks_testing::{Registration, register, run_once};
use turbopack_analyze::split_chunk::{ChunkPart, ChunkPartRange, split_output_asset_into_parts}; use turbopack_analyze::split_chunk::{ChunkPartRange, split_output_asset_into_parts};
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
code_builder::{Code, CodeBuilder}, code_builder::{Code, CodeBuilder},
...@@ -59,39 +59,42 @@ async fn split_chunk() { ...@@ -59,39 +59,42 @@ async fn split_chunk() {
let parts = split_output_asset_into_parts(asset).await.unwrap(); let parts = split_output_asset_into_parts(asset).await.unwrap();
assert_eq!(parts.len(), 2);
assert_eq!(parts[0].source, rcstr!("source1.js"));
assert_eq!(parts[0].real_size, 46);
assert_eq!(parts[0].unaccounted_size, 34);
assert_eq!( assert_eq!(
&*parts, parts[0].ranges,
&vec![ vec![
ChunkPart { ChunkPartRange {
source: rcstr!("source1.js"), line: 2,
real_size: 15, start_column: 0,
unaccounted_size: 51, end_column: 12
ranges: vec![
ChunkPartRange {
line: 2,
start_column: 0,
end_column: 12,
},
ChunkPartRange {
line: 3,
start_column: 0,
end_column: 3,
},
],
}, },
ChunkPart { ChunkPartRange {
source: rcstr!("source2.js"), line: 3,
real_size: 31, start_column: 0,
unaccounted_size: 46, end_column: 34
ranges: vec![ChunkPartRange {
line: 4,
start_column: 0,
end_column: 31,
}],
}, },
] ]
); );
assert_eq!(parts[1].source, rcstr!("source2.js"));
assert_eq!(parts[1].real_size, 31);
assert_eq!(parts[1].unaccounted_size, 30);
assert_eq!(
parts[1].ranges,
vec![ChunkPartRange {
line: 4,
start_column: 0,
end_column: 31
}]
);
assert_eq!(parts[0].get_compressed_size().await.unwrap(), 43);
assert_eq!(parts[1].get_compressed_size().await.unwrap(), 28);
println!("{:#?}", parts); println!("{:#?}", parts);
anyhow::Ok(()) anyhow::Ok(())
}) })
......
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