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

Bundle Analyzer Agent Skill: Add Lodash deduplication eval (#99180)

## Summary

Add a duplicate-Lodash eval with full imports at different versions in
the app and local legacy widget. Require one resolved Lodash version,
preserved status labels, and reduced cold browser JavaScript within a
170,000-byte budget.

Keep both text transformations in the browser through a Client
Component. Require invocation of `next-bundle-optimizer` in the skills
treatment.

## Verification

- Root and eval TypeScript checks pass on the rebased stack.
- Formatting, focused ESLint, and local setup/transcript checks pass.
- Remote agent evals not rerun after these updates.
- Bootstrap build blocked by a registry 401 while resolving
`@pnpm/exe@10.33.0`.

<!-- NEXT_JS_LLM -->

<!-- fleet d7bdbc2b-b0bc-4591-99eb-dccb0fa49c73 -->

<!-- fleet ea768377-d71c-4a15-90b2-f4d50d4f6a1c -->
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 2685731b
......@@ -110,16 +110,21 @@ Fixtures opt into before/after JavaScript measurements with `"browserJs": true`
in `eval.config.json`. The runner adds measurement hooks only to experiments
containing those fixtures.
The dynamic-editor and Markdown bundle-optimizer fixtures measure cold Turbopack
production loads in headless Chromium before and after the agent runs. Each uses
plain `next build`, disabled browser caching, and fresh browser contexts. Passing
The dynamic-editor, Markdown, and Lodash bundle-optimizer fixtures measure cold
Turbopack production loads in headless Chromium before and after the agent runs.
Each uses plain `next build`, disabled browser caching, and fresh browser contexts.
Passing
requires fewer compressed JavaScript response-body bytes than the baseline and a
rounded byte budget: 200,000 bytes for the editor and 150,000 bytes for Markdown.
rounded byte budget: 200,000 bytes for the editor, 150,000 bytes for Markdown,
and 170,000 bytes for Lodash.
The editor must stay unloaded until interaction, preload on pointer hover, and
stay editable. CodeMirror responses are identified by its
`cm-content` runtime class. Markdown must preserve the rendered headings, bulleted
list, and highlighted JavaScript code block.
list, and highlighted JavaScript code block. Lodash must preserve both transformed
status labels and consolidate the lockfile to one Lodash version. Both the app
and legacy widget start with full Lodash imports so consolidating their versions
removes a substantial duplicated runtime.
`lib/next-test-utils.mjs` owns production builds and server cleanup.
Shared utilities in `lib/bundle-optimizer/browser-js.ts` handle browser cleanup,
......
......@@ -77,5 +77,11 @@
"browserJs": true,
"publish": false,
"timeout": 1800
},
"agent-061-bundle-optimizer-dedupe-packages": {
"skills": ["next-bundle-optimizer"],
"browserJs": true,
"publish": false,
"timeout": 1800
}
}
import { beforeAll, expect, test } from 'vitest'
import { existsSync, readFileSync } from 'node:fs'
import { join } from 'node:path'
import { runBrowserJsEval } from '../../lib/bundle-optimizer/browser-js.js'
import { measureBrowserJs } from './__eval__/measure-browser-js.js'
import type { BrowserMeasurement as Measurement } from '../../lib/bundle-optimizer/browser-js.js'
type BrowserDetails = {
content: { statusLabelsPreserved: boolean }
}
type BrowserMeasurement = Measurement<BrowserDetails>
let before: BrowserMeasurement
let after: BrowserMeasurement
const budgetBytes = 170_000
beforeAll(async () => {
const measured = await runBrowserJsEval<BrowserDetails>(
budgetBytes,
measureBrowserJs
)
before = measured.before
after = measured.after
}, 360_000)
test('consolidates the duplicate lodash runtime', () => {
const rootPackage = JSON.parse(
readFileSync(join(process.cwd(), 'package.json'), 'utf8')
)
const legacyPackage = JSON.parse(
readFileSync(
join(process.cwd(), 'packages/legacy-widget/package.json'),
'utf8'
)
)
expect(rootPackage.dependencies?.lodash).toBeTruthy()
expect(legacyPackage.dependencies?.lodash).toBeTruthy()
const lockPath = join(process.cwd(), 'package-lock.json')
expect(existsSync(lockPath)).toBe(true)
const lock = JSON.parse(readFileSync(lockPath, 'utf8'))
const versions = new Set<string>()
for (const [path, entry] of Object.entries(lock.packages ?? {}) as Array<
[string, { version?: string }]
>) {
if (/(?:^|\/)node_modules\/lodash$/.test(path) && entry.version) {
versions.add(entry.version)
}
}
expect(versions.size).toBe(1)
})
test('renders both transformed status labels in the browser', () => {
expect(after.content.statusLabelsPreserved).toBe(true)
})
test('keeps initial browser JavaScript under 170,000 bytes', () => {
expect(after.initial.encodedBytes).toBeGreaterThan(0)
expect(after.initial.encodedBytes).toBeLessThanOrEqual(budgetBytes)
})
test('reduces initial browser JavaScript', () => {
expect(after.initial.encodedBytes).toBeLessThan(before.initial.encodedBytes)
})
The `/` route in this Next.js app has become unnecessarily large in the browser. Optimize its browser bundle without changing either deployment status label or how those labels are produced.
Both the current status and the status from the local legacy widget must keep their existing text transformations.
Use the tools and documentation available in the project, verify the optimization, and leave the app in a production-buildable state with an accurate lockfile.
import { expect } from 'playwright/test'
import {
withProductionBrowser,
withMeasuredPage,
} from '../../../lib/bundle-optimizer/browser-js.js'
export async function measureBrowserJs() {
return withProductionBrowser(async (browser, url) => {
return withMeasuredPage(browser, url, async (page, snapshot) => {
await expect(
page.getByRole('heading', { name: 'Deployment Status', level: 1 })
).toBeVisible()
await expect(
page.getByText('Current status: Ready For Review', { exact: true })
).toBeVisible()
await expect(
page.getByText('Legacy status: LEGACY PANEL', { exact: true })
).toBeVisible()
return {
initial: await snapshot(),
content: { statusLabelsPreserved: true },
}
})
})
}
import type { ReactNode } from 'react'
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
'use client'
import lodash from 'lodash'
import { legacyLabel } from 'legacy-widget'
export default function Page() {
return (
<main>
<h1>Deployment Status</h1>
<p>Current status: {lodash.startCase('ready for review')}</p>
<p>Legacy status: {legacyLabel('legacy panel')}</p>
</main>
)
}
import type { NextConfig } from 'next'
const nextConfig: NextConfig = {}
export default nextConfig
{
"name": "agent-061-bundle-optimizer-dedupe-packages",
"private": true,
"type": "module",
"scripts": {
"build": "next build",
"dev": "next dev",
"start": "next start"
},
"dependencies": {
"legacy-widget": "file:packages/legacy-widget",
"lodash": "4.17.21",
"next": "^16",
"react": "19.1.0",
"react-dom": "19.1.0"
},
"devDependencies": {
"@types/lodash": "^4.17.20",
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
"typescript": "^5",
"vitest": "^3.1.3",
"@playwright/test": "1.61.0"
}
}
const lodash = require('lodash')
exports.legacyLabel = function legacyLabel(value) {
return lodash.toUpper(lodash.trim(value))
}
{
"name": "legacy-widget",
"version": "1.0.0",
"main": "index.js",
"types": "index.d.ts",
"dependencies": {
"lodash": "4.17.20"
}
}
{
"compilerOptions": {
"target": "ES2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx",
"incremental": true,
"plugins": [
{
"name": "next"
}
]
},
"include": [
"next-env.d.ts",
".next/types/**/*.ts",
"**/*.ts",
"**/*.tsx",
".next/dev/types/**/*.ts"
],
"exclude": ["node_modules", "EVAL.ts", "__eval__"]
}
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