Skip to content

fix(es/minifier): Inline before merge if#11526

Open
Austaras wants to merge 1 commit intoswc-project:mainfrom
Austaras:main
Open

fix(es/minifier): Inline before merge if#11526
Austaras wants to merge 1 commit intoswc-project:mainfrom
Austaras:main

Conversation

@Austaras
Copy link
Member

@Austaras Austaras commented Feb 3, 2026

Description:
The original issue is more subtle than claude found. It happens because minifier take var to ready for inline, but not actually doing the inline, so leave the to be merged statement into a bad middle state. Besides we should merge two if ignore span because variable defined in two if cons would have different syntax context, but if their value and usage is the same, we can still merge those two if.

BREAKING CHANGE:

Related issue (if exists):
Fixes #11517

@Austaras Austaras requested a review from a team as a code owner February 3, 2026 16:52
Copilot AI review requested due to automatic review settings February 3, 2026 16:52
@changeset-bot
Copy link

changeset-bot bot commented Feb 3, 2026

⚠️ No Changeset found

Latest commit: 3299427

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Austaras
Copy link
Member Author

Austaras commented Feb 3, 2026

Let's see how performance changes.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes issue #11517 where the SWC minifier incorrectly merged conditional statements, causing semantic changes in the output code. The issue occurred when two if statements had identical consequent blocks but different conditions, and the consequent blocks contained local variables with different values (e.g., different string literals).

Changes:

  • Added inline pass before merging if statements to ensure variables marked for inlining are actually inlined
  • Modified comparison logic to use SyntaxContext::within_ignored_ctxt() when comparing if statement consequents, allowing safe merging after inlining
  • Updated test outputs to reflect improved minification (multiple test files show better optimization where consecutive if statements with identical bodies are now correctly merged)

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
crates/swc_ecma_minifier/src/compress/optimize/conditionals.rs Adds inline pass before merge and wraps comparison in SyntaxContext::within_ignored_ctxt() to fix the bug
crates/swc_ecma_minifier/tests/libs-size.snapshot.md Updates size benchmarks showing minor improvements in echarts and typescript
crates/swc_ecma_minifier/tests/fixture/next/wrap-contracts/output.js Shows improved minification where two if statements with same body are merged
crates/swc_ecma_minifier/tests/fixture/next/react-pdf-renderer/output.js Shows improved minification with merged if statements
crates/swc_ecma_minifier/tests/fixture/next/feedback-util-promisify/chunks/pages/_app-72ad41192608e93a/output.js Shows improved minification with merged if statements
crates/swc_ecma_minifier/tests/fixture/next/33265/static/chunks/pages/index-cb36c1bf7f830e3c/output.js Shows improved minification with merged if statements
crates/swc_ecma_minifier/tests/fixture/next/33265/static/chunks/d6e1aeb5-38a8d7ae57119c23/output.js Shows improved minification with multiple merged if statements
crates/swc_ecma_minifier/tests/benches-full/echarts.js Shows improved minification with merged if statements and nested conditions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2026

Binary Sizes

File Size
swc.linux-x64-gnu.node 28M (28691400 bytes)

Commit: 17bd804

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 3, 2026

CodSpeed Performance Report

Merging this PR will degrade performance by 2.04%

Comparing Austaras:main (3299427) with main (3e63627)

Summary

❌ 1 regressed benchmark
✅ 183 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
es/full/minify/libraries/terser 507.3 ms 517.8 ms -2.04%

@Austaras
Copy link
Member Author

Austaras commented Feb 4, 2026

Seems fine.

Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

@claude Add enormous amount of execution tests to the minifier to verify changes in this PR

Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

@claude Add enormous amount of execution tests to the minifier to verify changes in this PR

@kdy1 kdy1 added this to the Planned milestone Feb 4, 2026
@Austaras
Copy link
Member Author

Austaras commented Feb 4, 2026

Nothing happened. claude stops working?

@kdy1
Copy link
Member

kdy1 commented Feb 4, 2026

Ah. The problem is that this is a PR from a fork. Hmm... I'll invoke claude locally

@kdy1
Copy link
Member

kdy1 commented Feb 4, 2026

We can rebase this PR once #11530 is merged, which adds lots of execution tests.

@kdy1
Copy link
Member

kdy1 commented Feb 4, 2026

I've added 113 execution tests to verify this fix in #11530. The tests cover the original issue reproduction and many variations including different variable types, statement types, data structures, function types, and real-world patterns like Redux reducers, i18n, HTTP status handling, etc.

@Austaras
Copy link
Member Author

Austaras commented Feb 4, 2026

That's excessively a lot of tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

SWC minifier incorrectly merges conditions (semantic change)

2 participants