Skip to content

refactor: import tool modules without the browser bundle - #3512

Closed
yulunz wants to merge 2 commits into
refactor/lazy-split-third-party-bundlefrom
refactor/lazy-light-startup-imports
Closed

yulunz wants to merge 2 commits into
refactor/lazy-split-third-party-bundlefrom
refactor/lazy-light-startup-imports

Conversation

@yulunz

@yulunz yulunz commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator

Refs #3427

Part 2 of the lazy-loading series, stacked on #3510. The server still loads browser.js at startup until a follow-up PR.

What

Every module under src/tools/ can now be imported without loading third_party/browser.js. So can telemetry/flagUtils.ts, utils/text.ts and config/ConfigWatcher.ts. The server still loads browser.js at startup through index.ts, McpContext, McpResponse and others. A follow-up PR changes McpServer to load it on the first tool call.

Changes:

  • StringUtilities and Debouncer (flagUtils.ts, utils/text.ts, ConfigWatcher.ts): third_party/index.ts re-exports these two small devtools-frontend modules by file path (core/platform/StringUtilities.js, core/common/Debouncer.js), not through mcp.js. No upstream code is copied into this repository.
  • PredefinedNetworkConditions (tools/emulation.ts uses it in its schema): now re-exported from third_party/index.ts (puppeteer-core/internal/cdp/PredefinedNetworkConditions.js) instead of browser.ts. McpPage.ts and WaitForHelper.ts import it from there too.
  • New loadBrowser() in third_party/index.ts: a cached import('./browser.js'), like loadLighthouse(). ./browser.js is marked external in the index bundle.
    • tools/input.ts and tools/snapshot.ts get TimeoutError from it inside the error path, so it is still the class Puppeteer throws. handleActionError() in input.ts is now async and its three callers await it.
    • tools/performance.ts reads DevTools.Tracing*Categories and CrUXManager through it inside the handler.
  • tools/performance.ts: parseRawTraceBuffer and traceResultIsSuccess are loaded with await import('../processors/PerformanceTrace.js') in stopTracingAndAppendOutput(). tools/lighthouse.ts reached browser.js only through performance.ts.
  • tools/logpoints.ts: its schema description uses LOGPOINT_FORMAT, so a lazy import is not enough. The browser-free parts of LogpointManager.ts move to a new src/utils/logpoint.ts: LOGPOINT_FORMAT, parseLogpoint(), LogpointError and the LogpointOptions/Logpoint/GeneratedLocation interfaces. LogpointManager.ts, McpPage.ts, LogpointFormatter.ts, ToolDefinition.ts and the tests import them from there.
  • tools/memory.ts: its schema description uses MAX_NAME_LENGTH. The constant moves from HeapSnapshotFormatter.ts, which uses DevTools.I18n at load time, to a new src/formatters/constants.ts.
  • Test: new tests/browser-imports.test.ts. It walks the static imports of the built JS from each of these entry points and fails with the import chain if one reaches third_party/browser.js: tools/tools.js, telemetry/flagUtils.js, utils/text.js, config/ConfigWatcher.js. It also checks that loadBrowser() returns the browser.js module. The graph walk from tests/cli-imports.test.ts moves to a shared tests/importGraph.ts helper with an includeDynamicImports option. The new test passes false, the CLI test keeps true.

No behavior change. Tool names, schemas and descriptions are unchanged, and so is the error handling. The only difference is that a few values are read from a module that is already loaded, after an extra await.

Modules in both bundles

These modules are now in both third_party/index.js and third_party/browser.js:

Module Size (debug) Why it's safe
puppeteer-core/…/util/disposable.js 8.5 KiB Unchanged from #3510: both copies resolve to the same global symbols and classes.
devtools-frontend/front_end/core/platform/StringUtilities.ts 19.2 KiB Pure functions, no module state, and its only import is a type.
devtools-frontend/front_end/core/common/Debouncer.ts 0.6 KiB One closure-based function. Its only module state is the testDebounceOverride flag, and no code in this repository calls enableTestOverride().
puppeteer-core/…/cdp/PredefinedNetworkConditions.js 1.1 KiB A frozen plain-data object. Its values are passed to page.emulateNetworkConditions(), and nothing compares it by identity.

TimeoutError, Deferred and Mutex remain only in browser.js.

Bundle sizes

After #3510 (debug) After this PR (debug) After #3510 (release) After this PR (release)
index.js 1,281,621 B 1,303,663 B (+22 KiB, the modules listed above) 598,567 B 607,867 B (+9 KiB)
browser.js 7,174,671 B 7,174,640 B 4,011,121 B 4,011,086 B

Testing

Move puppeteer-core, @puppeteer/browsers and the DevTools frontend code
from third_party/index.js into a new third_party/browser.js bundle.
index.js keeps the MCP SDK, zod, ajv and the lazy loaders. Both bundles
are still loaded at startup, so behavior does not change. This is a
prerequisite for loading browser.js on the first tool call (#3427).
Every module under src/tools/, plus flagUtils.ts, utils/text.ts and
ConfigWatcher.ts, can now be imported without loading
third_party/browser.js:

- Re-export StringUtilities, Debouncer and PredefinedNetworkConditions
  from the light third_party/index.js.
- Add loadBrowser() and use it for TimeoutError and the DevTools tracing
  categories inside tool handlers.
- Load PerformanceTrace.js lazily in the performance tools.
- Move the browser-free logpoint parsing to utils/logpoint.ts and
  MAX_NAME_LENGTH to formatters/constants.ts, since tool schemas use
  them.
- Add a test that walks the static imports of these modules.

The server still loads browser.js at startup (#3427).
@yulunz
yulunz added this pull request to stack #3517 November 17, 2026 20:17
@yulunz
yulunz force-pushed the refactor/lazy-split-third-party-bundle branch from ab0c0fe to 946dc96 Compare November 17, 2026 21:41
@yulunz

yulunz commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Split into #3518 – #3526 so each PR migrates one module.

@yulunz yulunz closed this Nov 17, 2026
@yulunz
yulunz deleted the refactor/lazy-light-startup-imports branch November 17, 2026 21:43
@yulunz
yulunz removed this pull request from stack #3517 November 17, 2026 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant