Skip to content

feat: optimize in checks on namespaces to keep them treeshake-able #6029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 27, 2025

Conversation

cyyynthia
Copy link
Contributor

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

Description

Currently, using the in operator on a namespace completely disables treeshaking, even if the left hand-side of the expression can be statically resolved. This makes checking for the existence of an export annoying, as demonstrated here.

This PR optimizes these checks to collapse the expression to a boolean, that is also used for DCE which allows getting rid of eventual compat code and what not in libraries.

There is still a slight annoyance with how warnings are emitted, as shown by the added test. Despite the gate Rollup still emits a warning about the unresolved export. This is minor and I felt like it'd require more invasive changes, so I didn't address this here.

Copy link

vercel bot commented Jul 23, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rollup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 27, 2025 6:03am

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice! I also like how this easily leaves room for extension, e.g. to simplify more binary expressions. The use of deoptimization looks correct to me.

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, apparently there is something off in the test, can you check it out?

@lukastaegert
Copy link
Member

Ah, the signature of traceExport had changed on master. I pushed a simple fix.

Copy link

codecov bot commented Jul 27, 2025

Codecov Report

❌ Patch coverage is 85.29412% with 5 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (master@fdd48a9). Learn more about missing BASE report.
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/ast/nodes/BinaryExpression.ts 70.58% 4 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff            @@
##             master    #6029   +/-   ##
=========================================
  Coverage          ?   98.75%           
=========================================
  Files             ?      271           
  Lines             ?    10597           
  Branches          ?     2828           
=========================================
  Hits              ?    10465           
  Misses            ?       91           
  Partials          ?       41           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@lukastaegert lukastaegert added this pull request to the merge queue Jul 27, 2025
Merged via the queue into rollup:master with commit 9a8614f Jul 27, 2025
41 checks passed
Copy link

This PR has been released as part of rollup@4.46.0. You can test it via npm install rollup.

@cyyynthia
Copy link
Contributor Author

I also like how this easily leaves room for extension, e.g. to simplify more binary expressions.

FWIW, the first iteration of my patch inlined all binary expressions that were statically resolved by Rollup; but it quickly turned out to be a potential footgun where the pre-computed result was longer than the binary operation itself. As it's not trivial to determine whether inlining is a win or not, I figured letting a code minifier tool deal with those was the best option for now and focus on making good use of Rollup's additional knowledge to do things Terser (and other minifiers) can't do.

// This should not be inlined
var a = 'this is a rather long string that we really do not want to repeat multiple times'
var b = a + '.'
var c = a + '!'

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.

3 participants