Files
vercel__next.js/apps/bundle-analyzer/lib/layout-treemap.ts
Will Binns-Smith 617ead5753 Turbopack: Add bundle analyzer UI to next build --experimental-analyze (#85788)
This adds an interactive Treemap-style bundle analyzer app to the Next.js codebase and writes a copy to disk when `next build --experimental-analyze` is run.

* It's a separate app in `apps/bundle-analyzer` as `@next/bundle-analyzer-ui` that uses `output: 'export'` to export a purely static Next.js app
* These static resources are copied into Next.js's published `dist` directory in the `next` package itself. Users never depend on `@next/bundle-analyzer-ui`
* When users run `next build --experimental-analyze`, these static resources are copied into the user's `.next/diagnostics/analyze` along with the data payloads needed to visualize the user's app

In an upcoming PR, we'll add `next experimental-analyze` that will create analysis files without build artifacts, as well as optionally start a static file server.

Test Plan: Ran `next build --experimental-analyze` in a test app and ran a static file server to run it.
2025-11-14 15:50:42 -08:00

122 lines
3.4 KiB
TypeScript

import type { LayoutRect } from './treemap-layout'
export function layoutTreemap(sizes: number[], rect: LayoutRect): LayoutRect[] {
if (sizes.length === 0) return []
if (sizes.length === 1) return [rect]
const totalSize = sizes.reduce((a, b) => a + b, 0)
const normalizedSizes = sizes.map(
(s) => (s / totalSize) * rect.width * rect.height
)
const result: LayoutRect[] = []
let remaining = [...normalizedSizes]
let currentRect = { ...rect }
let totalRemaining = remaining.reduce((a, b) => a + b, 0)
while (remaining.length > 1) {
// Decide orientation: vertical if wider, horizontal if taller
const vertical = currentRect.width >= currentRect.height
// Pick items until sum > total / count
const picked: number[] = []
let sum = 0
for (const size of remaining) {
picked.push(size)
sum += size
if (vertical) {
const width = (currentRect.width * sum) / totalRemaining
if (width > (currentRect.height / picked.length) * 0.9) {
break
}
} else {
const height = (currentRect.height * sum) / totalRemaining
if (height > (currentRect.width / picked.length) * 0.9) {
break
}
}
}
// Ensure at least one item is picked
if (picked.length === 0) {
picked.push(remaining[0])
sum = remaining[0]
}
// Calculate the space used by this row/column
const spaceRatio = sum / totalRemaining
totalRemaining -= sum
if (vertical) {
// Items stacked vertically, filling full width
const rowWidth = Math.round(spaceRatio * currentRect.width)
let offsetY = 0
for (let i = 0; i < picked.length; i++) {
const size = picked[i]
const itemHeight =
i === picked.length - 1
? Math.round(currentRect.height - offsetY)
: Math.round((size / sum) * currentRect.height)
result.push({
x: Math.round(currentRect.x),
y: Math.round(currentRect.y + offsetY),
width: rowWidth,
height: itemHeight,
})
offsetY += itemHeight
}
// Update remaining rectangle
currentRect = {
x: Math.round(currentRect.x + rowWidth),
y: Math.round(currentRect.y),
width: Math.round(currentRect.width - rowWidth),
height: Math.round(currentRect.height),
}
} else {
// Items placed horizontally, filling full height
const rowHeight = Math.round(spaceRatio * currentRect.height)
let offsetX = 0
for (let i = 0; i < picked.length; i++) {
const size = picked[i]
const itemWidth =
i === picked.length - 1
? Math.round(currentRect.width - offsetX)
: Math.round((size / sum) * currentRect.width)
result.push({
x: Math.round(currentRect.x + offsetX),
y: Math.round(currentRect.y),
width: itemWidth,
height: rowHeight,
})
offsetX += itemWidth
}
// Update remaining rectangle
currentRect = {
x: Math.round(currentRect.x),
y: Math.round(currentRect.y + rowHeight),
width: Math.round(currentRect.width),
height: Math.round(currentRect.height - rowHeight),
}
}
// Remove picked items from remaining
remaining = remaining.slice(picked.length)
}
// Last item fills remaining space
if (remaining.length === 1) {
result.push(currentRect)
}
return result
}