Skip to content

fix(compiler): annotate octane factory calls with @__PURE__ so esbuild tree-shakes them - #1269

Merged
domgan-oai merged 2 commits into
mainfrom
claude/wonderful-mirzakhani-667c43
Sep 23, 2026
Merged

domgan-oai merged 2 commits into
mainfrom
claude/wonderful-mirzakhani-667c43

Conversation

@trueadm

@trueadm trueadm commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

binding-aria-esbuild shipped the whole client runtime: 197,180 bytes raw vs 8,016 for binding-aria-vite. Its committed budget (113 KB, from #637) had been hiding the problem. The root cause is in the compiler, so this fixes it there instead of raising the budget or patching aria.

Why esbuild kept the runtime

  1. interactions/useFocusVisible.ts is on aria's sideEffects list. It calls addWindowFocusTracking() when the module loads, and its handlers read openLink.isOpening, which pulls in utils/openLink.ts.
  2. openLink.ts has an unused const RouterContext = createContext({...}).
  3. createContext is declared /* @__NO_SIDE_EFFECTS__ */, but esbuild only applies that annotation inside the declaring file (CHANGELOG 0.18.1: tree-shaking decisions "are currently local to the file they appear in"; use /* @__PURE__ */ on the call). So the unused call counts as a side effect and keeps createContext → provider body → all of runtime.ts. Rollup applies the annotation across modules, which is why only the esbuild lane was affected.

There are about 366 unannotated module-scope createContext/memo/lazy calls across the bindings, and user apps have the same exposure. So the compiler adds the call-site annotation instead of each binding.

What changed

  • New compiler/pure-factories.js holds the list of factories to annotate. For client DOM builds that's createContext, memo, lazy and createPortal. For server and universal targets it's lazy only, which was the previous behavior (the server createContext calls registerContext).
  • compile.js: annotatePureLazyCalls becomes annotatePureFactoryCalls and now covers the whole list, for both .tsrx and .tsx.
  • slot-hooks.js / plain-hook-memo.js: plain .ts/.js modules get the same annotation. This covers the surgical edit path (text edits, line-preserving) and the whole-module memo-inlining path, including hook-free modules that previously passed through unchanged.
  • Calls through a local that shadows the import are left alone, and calls that already carry @__PURE__ don't get a second one.
  • run-minimal.mjs keeps the TS loader for compiled .ts/.tsx output, so the lane builds (TS enum). This is the same one-line change as the uncommitted fix/context-provider-dev-error-bench-lanes work, so whichever lands second should drop the duplicate.
  • minimal-budgets.json: binding-aria-esbuild re-baselined to 8,960 / 3,472 / 3,104 bytes (raw / gzip / brotli), the same ~3.4% headroom its Vite twin has.

Measured, all minimal-import lanes, same machine, before vs after:

lane raw gzip
binding-aria-esbuild 197,180 → 8,667 63,197 → 3,349
the other 30 lanes identical identical

Validation

  • node benchmarks/bench.mjs --quick --ratios bundle-size bundle-reachability: exit 0, 156 ratio guards checked, none breached
  • pnpm format:files:check on changed files
  • pnpm typecheck:files (tsrx-tsc) on changed files
  • pnpm test: not run in full. --project octane --project octane-prod ran under load average ~42 from other sessions. Of its failures, only two were caused by this change: codegen tests pinning the old output, now updated. The rest were load-bound timeouts. Split-context and independent-parent hydration failures reproduce with the change reverted and pass or fail from run to run.
  • targeted tests:
    • New tests/compiler/pure-factory-annotation.test.ts, replacing pure-lazy-annotation.test.ts. It bundles compiler output with esbuild, asserts module reachability through the metafile, and runs the bundle. Cases: hook-free .ts, hook-slotted .ts, memo-inlined .ts, .tsrx; a control case (a used context keeps the runtime); server lazy() drops its import; a shadowed local keeps its side effect.
    • The four main cases fail with the fix reverted. The shadow and server-lazy cases fail when their guards are removed.
    • Updated external-hook-slot.test.ts and bundler-compiler.test.ts for the new output. The negative _$__s(remember(Host)) check now accepts the comment, so it can't pass trivially.

Risk / follow-ups

  • codegen-size can't run locally on main: the rspack CSS-modules fixture emits an extra 338.main.cjs chunk, with or without this change. Its compiled-byte numbers will grow about 16 bytes per factory call, because esbuild's transform keeps @__PURE__ comments until the final bundle step.
  • The annotation assumes the client DOM createContext/memo/createPortal stay side-effect-free. Their runtime declarations already promise that, and Rollup already relies on it.

Provenance

  • An agent produced this diff (agent-authored)

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Medium Risk
Compiler output changes affect all client bundles under esbuild; correctness depends on client DOM factories staying side-effect-free at call sites, which the runtime already documents.

Overview
Fixes esbuild pulling the full Octane client runtime when bindings leave unused module-scope createContext / memo / lazy / createPortal calls in the graph. Rollup/Vite honor declaration-level /* @__NO_SIDE_EFFECTS__ */ across modules; esbuild does not, so those calls were treated as side effects.

The compiler now emits portable /* @__PURE__ */ on direct imports from octane for those factories in compiled .tsrx/.tsx output and in plain .ts/.js via slot-hooks (including hook-free modules). Client DOM builds annotate all four; server output still annotates only lazy() because server createContext registers contexts. Shadowed locals and calls that already have @__PURE__ are skipped.

Adds pure-factories.js, rebaselines binding-aria-esbuild minimal-import budgets (~113 KB → ~9 KB raw), aligns the esbuild benchmark lane’s TS loader with Vite for compiled .ts/.tsx, and replaces lazy-only tests with esbuild reachability coverage in pure-factory-annotation.test.ts.

Reviewed by Cursor Bugbot for commit e05721b. Bugbot is set up for automated code reviews on this repo. Configure here.

esbuild parses `/* @__NO_SIDE_EFFECTS__ */` but never applies it across
files (CHANGELOG 0.18.1), so an unused module-scope `createContext(…)`
counted as a side effect and retained the whole client runtime. In the
aria minimal-import lane, useFocusVisible (sideEffects) reaches
openLink.ts, whose unused RouterContext kept 174 KB of runtime.ts.

The compiler now marks direct calls to the octane imports
createContext/memo/lazy/createPortal with `/* @__PURE__ */` in compiled
modules and in plain .ts/.js modules, including hook-free ones. Server
and universal targets keep the lazy-only set (the server createContext
registers). Shadowed locals and authored annotations are left alone.

binding-aria-esbuild: 197,180 -> 8,667 raw, 63,197 -> 3,349 gzip. All
other minimal lanes are byte-identical. Its budget is re-baselined with
the headroom of its Vite twin. The harness now keeps the TS loader for
compiled .ts/.tsx output so the lane builds.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix labels Sep 23, 2026
@github-actions
github-actions Bot marked this pull request as draft September 23, 2026 08:22
@domgan-oai
domgan-oai marked this pull request as ready for review September 23, 2026 09:44
@domgan-oai
domgan-oai merged commit e4bb4e0 into main Sep 23, 2026
21 of 39 checks passed
@domgan-oai
domgan-oai deleted the claude/wonderful-mirzakhani-667c43 branch September 23, 2026 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agent-authored Diff or issue body substantially produced by an AI agent fix PR: bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants