Repository navigation
fix(compiler): annotate octane factory calls with @__PURE__ so esbuild tree-shakes them - #1269
Merged
Merged
Conversation
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
binding-aria-esbuildshipped the whole client runtime: 197,180 bytes raw vs 8,016 forbinding-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
interactions/useFocusVisible.tsis on aria'ssideEffectslist. It callsaddWindowFocusTracking()when the module loads, and its handlers readopenLink.isOpening, which pulls inutils/openLink.ts.openLink.tshas an unusedconst RouterContext = createContext({...}).createContextis 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 keepscreateContext→ provider body → all ofruntime.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/lazycalls across the bindings, and user apps have the same exposure. So the compiler adds the call-site annotation instead of each binding.What changed
compiler/pure-factories.jsholds the list of factories to annotate. For client DOM builds that'screateContext,memo,lazyandcreatePortal. For server and universal targets it'slazyonly, which was the previous behavior (the servercreateContextcallsregisterContext).compile.js:annotatePureLazyCallsbecomesannotatePureFactoryCallsand now covers the whole list, for both.tsrxand.tsx.slot-hooks.js/plain-hook-memo.js: plain.ts/.jsmodules 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.@__PURE__don't get a second one.run-minimal.mjskeeps the TS loader for compiled.ts/.tsxoutput, so the lane builds (TSenum). This is the same one-line change as the uncommittedfix/context-provider-dev-error-bench-laneswork, so whichever lands second should drop the duplicate.minimal-budgets.json:binding-aria-esbuildre-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:
Validation
node benchmarks/bench.mjs --quick --ratios bundle-size bundle-reachability: exit 0, 156 ratio guards checked, none breachedpnpm format:files:checkon changed filespnpm typecheck:files(tsrx-tsc) on changed filespnpm test: not run in full.--project octane --project octane-prodran 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.tests/compiler/pure-factory-annotation.test.ts, replacingpure-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); serverlazy()drops its import; a shadowed local keeps its side effect.lazycases fail when their guards are removed.external-hook-slot.test.tsandbundler-compiler.test.tsfor the new output. The negative_$__s(remember(Host))check now accepts the comment, so it can't pass trivially.Risk / follow-ups
codegen-sizecan't run locally on main: the rspack CSS-modules fixture emits an extra338.main.cjschunk, 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.createContext/memo/createPortalstay side-effect-free. Their runtime declarations already promise that, and Rollup already relies on it.Provenance
agent-authored)🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith 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/createPortalcalls 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 fromoctanefor those factories in compiled.tsrx/.tsxoutput and in plain.ts/.jsviaslot-hooks(including hook-free modules). Client DOM builds annotate all four; server output still annotates onlylazy()because servercreateContextregisters contexts. Shadowed locals and calls that already have@__PURE__are skipped.Adds
pure-factories.js, rebaselinesbinding-aria-esbuildminimal-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 inpure-factory-annotation.test.ts.Reviewed by Cursor Bugbot for commit e05721b. Bugbot is set up for automated code reviews on this repo. Configure here.