perf(build): improve tree shaking with purity annotations - #3167
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
Performance benchmarksCompared 1 improved · 0 regressed · 5 within ±1.5%
View detailed results and traces 🟢 improvement · 🔴 regression · ⚫ change below 1.5% · paired base/head |
There was a problem hiding this comment.
🟡 Changes recommended
The current @__PURE__ eligibility allows ImportExpression loaders, which can cause bundlers to drop eager import(...) evaluation when the call is removed, changing runtime semantics.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR improves build-time dead-code elimination by adding “purity”/“no side effects” annotations so Rolldown can more aggressively tree-shake unused next/dynamic component factories and unused script nonce hook setup, reducing retained code and lazy chunks in consumer bundles.
Changes:
- Annotate eligible transformed
next/dynamiccall sites with/* @__PURE__ */during the preload-metadata transform. - Mark the script nonce hook factory as side-effect-free to allow removing unused hook setup when only
withScriptNonceis imported. - Add build-optimization regression tests covering both tree-shaking scenarios and guardrails (no duplicate pure annotations; avoid accessor-based option objects).
File summaries
| File | Description |
|---|---|
| tests/build-optimization.test.ts | Adds regression tests ensuring unused dynamic calls and unused script nonce hook setup are tree-shaken. |
| packages/vinext/src/shims/script-nonce-context.tsx | Adds a @__NO_SIDE_EFFECTS__ annotation to enable DCE of unused hook setup. |
| packages/vinext/src/plugins/dynamic-preload-metadata.ts | Extends the next/dynamic transform to optionally annotate transformed calls as @__PURE__, with eligibility checks. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
/bigbonk review for issues |
|
LGTM! |
Summary
next/dynamiccalls as pure so Rolldown can remove unused component factories and lazy chunkswithScriptNoncedo not retain unused hook setupDetails
The dynamic transform limits the hint to function/import loaders with accessor-free object arguments, and preserves existing user or compiler annotations without duplication.
Build-size probe
Isolated minified Rolldown production bundles with React externalized, totaling all emitted JavaScript chunks:
next/dynamiccomponentwithScriptNonce-only importThese focused probes isolate the tree-shaking opportunity; whole-application savings depend on which exports and dynamic components are unused.