Skip to content

fix(es/minifier): Preserve pure IIFE annotations - #12167

Open
mrlonely (mameikagou) wants to merge 3 commits into
swc-project:mainfrom
mameikagou:fix/preserve-pure-iife-annotations
Open

fix(es/minifier): Preserve pure IIFE annotations#12167
mrlonely (mameikagou) wants to merge 3 commits into
swc-project:mainfrom
mameikagou:fix/preserve-pure-iife-annotations

Conversation

@mameikagou

@mameikagou mrlonely (mameikagou) commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Description:

Pure annotations on unused IIFEs could be lost when the IIFE was inlined before unused-variable cleanup, leaving assignments that dead-code elimination could not remove.

This preserves annotated IIFEs while optimizing initializers whose bindings are already known to be removable. Later cleanup can then remove the call while retaining argument and sequence side effects. Live bindings remain eligible for the existing optimizations.

Related issue (if exists):

Closes #12165

Tests:

  • cargo test -p swc_ecma_minifier
  • ./scripts/exec.sh terser_reduce_vars_iife
  • cargo clippy -p swc_ecma_minifier --features concurrent --tests -- -D warnings
  • cargo fmt --all -- --check

@mameikagou
mrlonely (mameikagou) requested review from a team as code owners August 30, 2026 10:41
@changeset-bot

changeset-bot Bot commented Aug 30, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d7b1652

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

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

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Aug 30, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-08-30T14:08:18.480694Z d7b1652 New commits
ℹ️ About Codex in GitHub

Your 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 85c6f73867

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

Comment thread crates/swc_ecma_minifier/tests/fixture/issues/12165/output.js Outdated
Comment thread crates/swc_ecma_minifier/src/compress/optimize/mod.rs Outdated
@codspeed-hq

codspeed-hq Bot commented Aug 30, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 200 untouched benchmarks
⏩ 61 skipped benchmarks1


Comparing mameikagou:fix/preserve-pure-iife-annotations (d7b1652) with main (e876e80)

Open in CodSpeed

Footnotes

  1. 61 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3f068ed17b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

Comment on lines +3276 to +3277
let dont_invoke_pure_iife =
!self.mode.preserve_vars() && self.can_drop_unused_pat(&var.name);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Limit IIFE protection to removable patterns

For an unused binding such as let {...rest} = /*#__PURE__*/ function (v) { /* large body */ return v }({}), can_drop_unused_pat returns true because the rest identifier has no usages, so this disables IIFE invocation and adds the persistent no-inline mark. However, take_pat_if_unused immediately preserves every object pattern containing a rest property, meaning the declaration cannot be removed; the condition is recomputed on every pass, so the previously eligible IIFE remains uninlined and retains its full body in minified output. Only enable this protection when unused-pattern cleanup can actually remove the pattern.

AGENTS.md reference: AGENTS.md:L7-L7

Useful? React with 👍 / 👎.

Comment on lines +3276 to +3277
let dont_invoke_pure_iife =
!self.mode.preserve_vars() && self.can_drop_unused_pat(&var.name);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Check the initializer before scanning every binding

This computes can_drop_unused_pat for every variable declarator, including declarations without an initializer and initializers that are not annotated IIFEs. That helper calls find_pat_ids once through can_drop_unused_vars and then again itself, allocating and walking every binding pattern twice on each optimizer pass even though the result is only needed for the rare pure-IIFE case. Check that var.init is an annotated function/arrow call first, then perform the usage scan only for that case to avoid adding repeated hot-path work to ordinary minification.

AGENTS.md reference: AGENTS.md:L7-L7

Useful? React with 👍 / 👎.

Comment on lines +519 to +523
if self.ctx.bit_ctx.contains(BitCtx::DontInvokePureIife)
&& call.ctxt.has_mark(self.marks.pure)
{
log_abort!("iife: Has pure annotation");
return false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve throws from unused destructuring

With an unused binding such as let {x} = /*#__PURE__*/ function () { return null }(), this guard preserves the pure call until take_pat_if_unused, which treats the annotation as permission to remove the whole pattern and call. The annotation only says invoking the function has no side effects; destructuring its null result still throws a TypeError, so the optimized program incorrectly continues instead of throwing. Preserve the destructuring operation unless its returned value is known to be safely destructurable.

AGENTS.md reference: crates/swc_ecma_minifier/AGENTS.md:L34-L36

Useful? React with 👍 / 👎.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d7b16528a4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

Comment on lines +1626 to +1628
if !matches!(
expr,
Expr::Call(..) | Expr::New(..) | Expr::TaggedTpl(..) | Expr::Seq(..)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve non-PURE comments when replacing annotated IIFEs

When comments are retained and an annotation is accompanied by another leading comment, such as let dead = /*! license */ /*#__PURE__*/ (() => {})(console.log("effect")), this branch keeps the surviving call's argument span instead of the removed IIFE span. The code generator only takes leading comments at emitted node spans, so both comments remain attached to the now-unreferenced IIFE position and the license/preserve comment disappears. Move or selectively remove the PURE annotation while transferring other comments to the replacement expression.

Useful? React with 👍 / 👎.

@Austaras

Copy link
Copy Markdown
Member

It seems fine if we don't invoke any IIFE with pure annotation at all. It's very rare to have pure annotations and Terser behaves the same.

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.

es/minifier: IIFE inlining runs before pure DCE, so the /*#__PURE__*/ annotation is lost

3 participants