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"
dependencies = [
"anyhow",
"bincode 2.0.1",
"flate2",
"rustc-hash 2.1.1",
"serde",
"serde_json",
......
'use client'
import type React from 'react'
import { SizeMode } from '@/lib/treemap-layout'
import { useEffect, useMemo, useState } from 'react'
import useSWR from 'swr'
import { ErrorState } from '@/components/error-state'
......@@ -17,12 +19,17 @@ import { computeActiveEntries, computeModuleDepthMap } from '@/lib/module-graph'
import { fetchStrict } from '@/lib/utils'
import { formatBytes } from '@/lib/utils'
enum Environment {
Client = 'client',
Server = 'server',
}
export default function Home() {
const [selectedRoute, setSelectedRoute] = useState<string | null>(null)
const [environmentFilter, setEnvironmentFilter] = useState<
'client' | 'server'
>('client')
const [typeFilter, setTypeFilter] = useState<string[]>(['js', 'css', 'json'])
const [environmentFilter, setEnvironmentFilter] = useState<Environment>(
Environment.Client
)
const [typeFilter, setTypeFilter] = useState(['js', 'css', 'json'])
const [selectedSourceIndex, setSelectedSourceIndex] = useState<number | null>(
null
)
......@@ -69,6 +76,7 @@ export default function Home() {
client?: boolean
} | null>(null)
const [searchQuery, setSearchQuery] = useState('')
const [sizeMode, setSizeMode] = useState(SizeMode.Compressed)
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
......@@ -100,15 +108,15 @@ export default function Home() {
}, [modulesData, analyzeData])
const filterSource = useMemo(() => {
if (!analyzeData) return undefined
if (!analyzeData) return () => true
return (sourceIndex: number) => {
const flags = analyzeData.getSourceFlags(sourceIndex)
// Check environment filter
const hasEnvironment =
(environmentFilter === 'client' && flags.client) ||
(environmentFilter === 'server' && flags.server)
(environmentFilter === Environment.Client && flags.client) ||
(environmentFilter === Environment.Server && flags.server)
// Check type filter
const hasType =
......@@ -157,25 +165,47 @@ export default function Home() {
/>
</div>
<div className="basis-2/3 flex justify-end">
<div className="basis-2/3 flex justify-end items-center space-x-4">
{analyzeData && (
<>
<ToggleGroup
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}
onValueChange={(value) => {
if (value) setEnvironmentFilter(value as 'client' | 'server')
if (value) setEnvironmentFilter(value as Environment)
}}
size="sm"
>
<ToggleGroupItem value="client">Client</ToggleGroupItem>
<ToggleGroupItem value="server">Server</ToggleGroupItem>
<ToggleGroupItem value={Environment.Client}>
Client
</ToggleGroupItem>
<ToggleGroupItem value={Environment.Server}>
Server
</ToggleGroupItem>
</ToggleGroup>
<ControlDivider />
<ToggleGroup
type="multiple"
className="mr-4"
value={typeFilter}
onValueChange={(value) => {
if (value.length > 0) setTypeFilter(value)
......@@ -188,6 +218,8 @@ export default function Home() {
<ToggleGroupItem value="asset">Asset</ToggleGroupItem>
</ToggleGroup>
<ControlDivider />
<FileSearch value={searchQuery} onChange={setSearchQuery} />
</>
)}
......@@ -235,6 +267,7 @@ export default function Home() {
onHoveredNodeChange={setHoveredNodeInfo}
searchQuery={searchQuery}
filterSource={filterSource}
sizeMode={sizeMode}
/>
</div>
......@@ -252,6 +285,7 @@ export default function Home() {
selectedSourceIndex={selectedSourceIndex}
moduleDepthMap={moduleDepthMap}
environmentFilter={environmentFilter}
filterSource={filterSource}
/>
</>
) : null}
......@@ -266,7 +300,7 @@ export default function Home() {
{hoveredNodeInfo.name}
</span>
<span className="ml-2 text-muted-foreground">
{formatBytes(hoveredNodeInfo.size)}
{`${formatBytes(hoveredNodeInfo.size)} ${sizeMode}`}
</span>
{(hoveredNodeInfo.server || hoveredNodeInfo.client) && (
<span className="ml-2 inline-flex gap-1">
......@@ -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 {
if (!analyzeData) return 0
const sourceRoots = analyzeData.sourceRoots()
......
'use client'
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 { Skeleton } from '@/components/ui/skeleton'
import { AnalyzeData, ModulesData } from '@/lib/analyze-data'
......@@ -15,6 +22,7 @@ interface SidebarProps {
selectedSourceIndex: number | null
moduleDepthMap: Map<number, number>
environmentFilter: 'client' | 'server'
filterSource?: (sourceIndex: number) => boolean
isLoading?: boolean
}
......@@ -34,9 +42,12 @@ export function Sidebar({
selectedSourceIndex,
moduleDepthMap,
environmentFilter,
filterSource,
isLoading = false,
}: SidebarProps) {
if (isLoading) {
filterSource = filterSource ?? (() => true)
if (isLoading || !analyzeData) {
return (
<div
className="flex-none bg-muted border-l border-border overflow-y-auto"
......@@ -56,10 +67,40 @@ export function Sidebar({
)
}
if (!analyzeData) {
return null
}
return (
<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(
analyzeData,
selectedSourceIndex
......@@ -76,101 +117,122 @@ export function Sidebar({
const childModuleCount =
hasChildModules && selectedSourceIndex != null
? analyzeData.getSourceRecursiveModuleCount(selectedSourceIndex)
? analyzeData.getRecursiveModuleCount(selectedSourceIndex, filterSource)
: null
return (
<div
className="flex-none bg-muted border-l border-border overflow-y-auto"
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>
const { size, compressedSize } = analyzeData.getRecursiveSizes(
selectedSourceIndex,
filterSource
)
{selectedSourceIndex != null &&
analyzeData.source(selectedSourceIndex) &&
(specialModuleType === SpecialModule.POLYFILL_MODULE ||
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>
)}
const chunks =
selectedSourceIndex != null
? analyzeData.sourceChunks(selectedSourceIndex)
: []
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 &&
analyzeData.source(selectedSourceIndex) &&
!hasChildModules && (
analyzeData.source(selectedSourceIndex) ? (
<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 && (
<ImportChain
key={selectedSourceIndex}
startFileId={selectedSourceIndex}
analyzeData={analyzeData}
modulesData={modulesData}
depthMap={moduleDepthMap}
environmentFilter={environmentFilter}
/>
)}
{(() => {
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
})()}
<div>
<span className="text-muted-foreground">About </span>
<span>{formatBytes(compressedSize)}</span>
<span className="text-muted-foreground ml-1">compressed</span>
<InlineHelpTooltip>
Estimated compressed size. Modules are compressed in isolation
which may differ from their size in the final chunk.
</InlineHelpTooltip>
</div>
</>
)}
{hasChildModules && childModuleCount != null ? (
<div>
<span>{childModuleCount} </span>
<span className="text-muted-foreground">
{childModuleCount === 1 ? 'module' : 'modules'}
</span>
</div>
) : null}
</div>
) : null}
</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>
)
}
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 {
computeTreemapLayoutFromAnalyze,
type LayoutNode,
type LayoutNodeInfo,
SizeMode,
} from '@/lib/treemap-layout'
import { SpecialModule } from '@/lib/types'
import { formatBytes } from '@/lib/utils'
......@@ -27,6 +28,7 @@ interface TreemapVisualizerProps {
filterSource?: (sourceIndex: number) => boolean
isModulePolyfillChunk?: (sourceIndex: number) => boolean
isNoModulePolyfillChunk?: (sourceIndex: number) => boolean
sizeMode?: SizeMode
}
function getFileColor(node: {
......@@ -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(
node: LayoutNode,
x: number,
......@@ -232,7 +305,7 @@ function drawTreemap(
ctx.lineTo(rect.x + rect.width, rect.y + titleBarHeight)
ctx.stroke()
const titleFontSize = Math.max(10, titleBarHeight * 0.5)
const { titleFontSize } = calculateTitleFontSizes(titleBarHeight)
ctx.fillStyle = colors.text
ctx.font = `600 ${titleFontSize}px sans-serif`
ctx.textAlign = 'left'
......@@ -342,7 +415,6 @@ function drawTreemap(
ctx.textBaseline = 'middle'
const maxWidth = rect.width - 8
let displayName = name
const sizeText = formatBytes(node.size)
const fontSize = 12
......@@ -353,17 +425,7 @@ function drawTreemap(
const hasSpaceForSize = rect.height > 50
ctx.font = `${fontSize}px sans-serif`
const nameMetrics = ctx.measureText(displayName)
if (nameMetrics.width > maxWidth) {
while (
displayName.length > 0 &&
ctx.measureText(`${displayName}...`).width > maxWidth
) {
displayName = displayName.slice(0, -1)
}
displayName += '...'
}
const displayName = truncateTextWithEllipsisIfNeeded(ctx, name, maxWidth)
if (hasSpaceForSize) {
ctx.font = `${fontSize}px sans-serif`
......@@ -426,17 +488,40 @@ function drawTreemap(
ctx.lineTo(rect.x + rect.width, rect.y + titleBarHeight)
ctx.stroke()
const titleFontSize = Math.max(10, titleBarHeight * 0.5)
ctx.fillStyle = colors.text
ctx.font = `600 ${titleFontSize}px sans-serif`
ctx.textAlign = 'left'
const { titleFontSize, sizeFontSize } =
calculateTitleFontSizes(titleBarHeight)
const sizeText = formatBytes(node.size)
const centerY = rect.y + titleBarHeight / 2
const gap = 6
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,
rect.x + 8,
rect.y + titleBarHeight / 2,
rect.width - 16
availableNameWidth
)
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
......@@ -473,17 +558,39 @@ function drawTreemap(
ctx.lineTo(rect.x + rect.width, rect.y + titleBarHeight)
ctx.stroke()
const titleFontSize = Math.max(10, titleBarHeight * 0.5)
ctx.fillStyle = colors.text
ctx.font = `600 ${titleFontSize}px sans-serif`
ctx.textAlign = 'left'
const { titleFontSize, sizeFontSize } =
calculateTitleFontSizes(titleBarHeight)
const sizeText = formatBytes(node.size)
const centerY = rect.y + titleBarHeight / 2
const gap = 6
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,
rect.x + 8,
rect.y + titleBarHeight / 2,
rect.width - 16
availableNameWidth
)
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
......@@ -604,6 +711,7 @@ export function TreemapVisualizer({
onHoveredNodeChangeDelayed,
searchQuery = '',
filterSource,
sizeMode = SizeMode.Compressed,
}: TreemapVisualizerProps) {
const canvasRef = useRef<HTMLCanvasElement>(null)
const containerRef = useRef<HTMLDivElement>(null)
......@@ -708,7 +816,8 @@ export function TreemapVisualizer({
width: cssDimensions.width,
height: cssDimensions.height,
},
filterSource
filterSource,
sizeMode
)
// If we're not at the root, wrap with ancestor title bars
......@@ -731,6 +840,7 @@ export function TreemapVisualizer({
cssDimensions.width,
cssDimensions.height,
filterSource,
sizeMode,
])
useEffect(() => {
......@@ -917,16 +1027,16 @@ function getThemeColors() {
return {
text: dark ? '#ffffff' : '#000000',
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)',
dirBg: dark ? 'rgba(255, 255, 255, 0.05)' : 'rgba(128, 128, 128, 0.1)',
dirBorder: dark ? 'rgba(255, 255, 255, 0.2)' : 'rgba(128, 128, 128, 0.3)',
dirTitleBg: dark ? 'rgba(255, 255, 255, 0.08)' : 'rgba(128, 128, 128, 0.1)',
border: dark ? 'rgba(255, 255, 255, 0.4)' : 'rgba(180, 180, 180, 0.5)',
dirBg: dark ? 'rgba(255, 255, 255, 0.05)' : 'rgba(230, 230, 230, 0.1)',
dirBorder: dark ? 'rgba(255, 255, 255, 0.5)' : 'rgba(180, 180, 180, 0.6)',
dirTitleBg: dark ? 'rgba(255, 255, 255, 0.08)' : 'rgba(230, 230, 230, 0.1)',
dirTitleBorder: dark
? 'rgba(255, 255, 255, 0.15)'
: 'rgba(128, 128, 128, 0.2)',
? 'rgba(255, 255, 255, 0.4)'
: 'rgba(180, 180, 180, 0.5)',
collapsedBg: dark
? 'rgba(128, 128, 128, 0.15)'
: 'rgba(128, 128, 128, 0.2)',
: 'rgba(230, 230, 230, 0.2)',
collapsedText: dark
? 'rgba(255, 255, 255, 0.5)'
: '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 {
source_index: number
output_file_index: number
size: number
compressed_size: number
}
export interface AnalyzeOutputFile {
......@@ -336,29 +337,31 @@ export class AnalyzeData {
return parentPath + source.path
}
getSourceOutputSize(index: SourceIndex): number {
getOwnSizes(index: SourceIndex): {
size: number
compressedSize: number
} {
const chunkParts = this.sourceChunkParts(index)
let totalSize = 0
let size = 0
let compressedSize = 0
for (const chunkPartIndex of chunkParts) {
const chunkPart = this.chunkPart(chunkPartIndex)
if (chunkPart) {
totalSize += chunkPart.size
size += chunkPart.size
compressedSize += chunkPart.compressed_size
}
}
return totalSize
return { size, compressedSize }
}
getSourceRecursiveSize(index: SourceIndex): number {
let totalSize = this.getSourceOutputSize(index)
const children = this.sourceChildren(index)
for (const childIndex of children) {
totalSize += this.getSourceRecursiveSize(childIndex)
}
return totalSize
}
getRecursiveModuleCount(
index: SourceIndex,
filterSource: (sourceIndex: SourceIndex) => boolean
): number {
const selfVisible = filterSource(index)
const selfCount =
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)
if (children.length === 0) {
return selfCount
......@@ -366,7 +369,7 @@ export class AnalyzeData {
let totalCount = selfCount
for (const childIndex of children) {
totalCount += this.getSourceRecursiveModuleCount(childIndex)
totalCount += this.getRecursiveModuleCount(childIndex, filterSource)
}
return totalCount
}
......@@ -388,6 +391,35 @@ export class AnalyzeData {
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): {
client: boolean
server: boolean
......
......@@ -35,7 +35,13 @@ export interface LayoutNode extends LayoutNodeInfo {
interface SourceMetadata {
filtered: boolean
totalSize: number
size: number
compressedSize: number
}
export enum SizeMode {
Compressed = 'compressed',
Uncompressed = 'uncompressed',
}
function precomputeSourceMetadata(
......@@ -45,23 +51,24 @@ function precomputeSourceMetadata(
const sourceCount = analyzeData.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--) {
const children = analyzeData.sourceChildren(i)
const ownSize = analyzeData.getSourceOutputSize(i)
const ownSize = analyzeData.getOwnSizes(i)
if (children.length === 0) {
// Leaf node (file)
metadata[i].totalSize = ownSize
metadata[i].filtered = filterSource ? !filterSource(i) : false
// file
metadata[i] = {
size: ownSize.size,
compressedSize: ownSize.compressedSize,
filtered: filterSource ? !filterSource(i) : false,
}
} else {
// Directory - initialize with own size
metadata[i].totalSize = ownSize
// directory
metadata[i] = {
filtered: true,
size: ownSize.size,
compressedSize: ownSize.compressedSize,
}
}
}
......@@ -70,19 +77,22 @@ function precomputeSourceMetadata(
const children = analyzeData.sourceChildren(idx)
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
for (const childIdx of children) {
processDirectory(childIdx) // Process child first
if (!metadata[childIdx].filtered) {
// Only add size of visible (non-filtered) children
totalSize += metadata[childIdx].totalSize
totalUncompressedSize += metadata[childIdx].size
totalCompressedSize += metadata[childIdx].compressedSize
hasVisibleChild = true
}
}
metadata[idx].totalSize = totalSize
metadata[idx].size = totalUncompressedSize
metadata[idx].compressedSize = totalCompressedSize
metadata[idx].filtered = !hasVisibleChild // Directory filtered if no visible children
}
......@@ -102,7 +112,8 @@ function computeTreemapLayoutFromAnalyzeInternal(
foldedPath: string,
rect: LayoutRect,
metadata: SourceMetadata[],
filterSource?: (sourceIndex: SourceIndex) => boolean
filterSource: ((sourceIndex: SourceIndex) => boolean) | undefined,
sizeMode: SizeMode
): LayoutNode {
const source = analyzeData.source(sourceIndex)
if (!source) {
......@@ -111,7 +122,6 @@ function computeTreemapLayoutFromAnalyzeInternal(
const isDirectory = source.path.endsWith('/') || !source.path
// Get children sources
const childrenIndices = analyzeData.sourceChildren(sourceIndex)
// Fold single-child directories
......@@ -129,13 +139,16 @@ function computeTreemapLayoutFromAnalyzeInternal(
foldedPath + source.path,
rect,
metadata,
filterSource
filterSource,
sizeMode
)
}
}
// Use precomputed size
const totalSize = metadata[sourceIndex].totalSize
const totalSize =
sizeMode === SizeMode.Compressed
? metadata[sourceIndex].compressedSize
: metadata[sourceIndex].size
// If this is a file (no children), create a file node
if (!isDirectory || childrenIndices.length === 0) {
......@@ -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(
Math.max(12, Math.min(24, rect.height * 0.1))
)
......@@ -175,7 +190,7 @@ function computeTreemapLayoutFromAnalyzeInternal(
}
return {
name: foldedPath + source.path,
name: directoryName,
size: totalSize,
type: 'collapsed-directory',
rect,
......@@ -199,8 +214,11 @@ function computeTreemapLayoutFromAnalyzeInternal(
continue
}
// Use precomputed size
const childSize = metadata[childIndex].totalSize
// Use precomputed size based on mode
const childSize =
sizeMode === SizeMode.Compressed
? metadata[childIndex].compressedSize
: metadata[childIndex].size
childrenData.push({
index: childIndex,
......@@ -210,7 +228,7 @@ function computeTreemapLayoutFromAnalyzeInternal(
if (childrenData.length === 0) {
return {
name: foldedPath + source.path,
name: directoryName,
size: totalSize,
type: 'directory',
rect,
......@@ -235,12 +253,13 @@ function computeTreemapLayoutFromAnalyzeInternal(
'',
childRects[i],
metadata,
filterSource
filterSource,
sizeMode
)
)
return {
name: foldedPath + source.path,
name: directoryName,
size: totalSize,
type: 'directory',
rect,
......@@ -256,7 +275,8 @@ export function computeTreemapLayoutFromAnalyze(
analyzeData: AnalyzeData,
sourceIndex: SourceIndex,
rect: LayoutRect,
filterSource?: (sourceIndex: SourceIndex) => boolean
filterSource?: (sourceIndex: SourceIndex) => boolean,
sizeMode: SizeMode = SizeMode.Compressed
): LayoutNode {
// Precompute metadata once for entire tree
const metadata = precomputeSourceMetadata(analyzeData, filterSource)
......@@ -268,6 +288,7 @@ export function computeTreemapLayoutFromAnalyze(
'',
rect,
metadata,
filterSource
filterSource,
sizeMode
)
}
......@@ -11,6 +11,7 @@
"dependencies": {
"@radix-ui/react-dialog": "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-toggle-group": "1.1.1",
"autoprefixer": "^10.4.20",
......
......@@ -75,6 +75,7 @@ pub struct AnalyzeChunkPart {
pub source_index: u32,
pub output_file_index: u32,
pub size: u32,
pub compressed_size: u32,
}
#[derive(Serialize)]
......@@ -384,6 +385,7 @@ pub async fn analyze_output_assets(output_assets: Vc<OutputAssets>) -> Result<Vc
source_index,
output_file_index,
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_source(source_index, chunk_part_index);
......
This diff is collapsed.
......@@ -11,6 +11,7 @@ bench = false
[dependencies]
anyhow = { workspace = true }
bincode = { workspace = true }
flate2 = { workspace = true }
rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] }
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)]
pub mod compressed_size;
pub mod split_chunk;
......@@ -10,7 +10,7 @@ use turbo_tasks_fs::{
File, FileContent, FileSystem, FileSystemPath, VirtualFileSystem, rope::Rope,
};
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::{
asset::{Asset, AssetContent},
code_builder::{Code, CodeBuilder},
......@@ -59,39 +59,42 @@ async fn split_chunk() {
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!(
&*parts,
&vec![
ChunkPart {
source: rcstr!("source1.js"),
real_size: 15,
unaccounted_size: 51,
ranges: vec![
ChunkPartRange {
line: 2,
start_column: 0,
end_column: 12,
},
ChunkPartRange {
line: 3,
start_column: 0,
end_column: 3,
},
],
parts[0].ranges,
vec![
ChunkPartRange {
line: 2,
start_column: 0,
end_column: 12
},
ChunkPart {
source: rcstr!("source2.js"),
real_size: 31,
unaccounted_size: 46,
ranges: vec![ChunkPartRange {
line: 4,
start_column: 0,
end_column: 31,
}],
ChunkPartRange {
line: 3,
start_column: 0,
end_column: 34
},
]
);
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);
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