Skip to content
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

feat(es/minifier): Invoke IIFE into block #10220

Merged
merged 10 commits into from
Mar 19, 2025
Merged

feat(es/minifier): Invoke IIFE into block #10220

merged 10 commits into from
Mar 19, 2025

Conversation

Austaras
Copy link
Member

@Austaras Austaras commented Mar 17, 2025

Description:

This is maybe the biggest missing feature of terser as compared to gcc because its lack of proper scope support. But we have.

Further development:

  1. invoke IIFE with return into labelled break
  2. invoke IIFE in place where value is needed like if (() => {} ())
  3. invoke IIFE in seq and other side effect free expressions

Related issue:

@Austaras Austaras requested a review from a team as a code owner March 17, 2025 15:46
Copy link

changeset-bot bot commented Mar 17, 2025

🦋 Changeset detected

Latest commit: 39e6480

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

Copy link

codspeed-hq bot commented Mar 17, 2025

CodSpeed Performance Report

Merging #10220 will degrade performances by 2.08%

Comparing Austaras:main (39e6480) with main (482b63a)

Summary

⚡ 2 improvements
❌ 1 regressions
✅ 149 untouched benchmarks

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

Benchmarks breakdown

Benchmark BASE HEAD Change
serialization of serde 2.8 µs 2.8 µs -2.08%
es/lints/libs/d3 28.2 ms 27.8 ms +1.24%
es/lints/libs/terser 29.7 ms 29 ms +2.49%

@kdy1 kdy1 self-assigned this Mar 17, 2025
@kdy1 kdy1 added this to the Planned milestone Mar 17, 2025
@Austaras Austaras requested a review from kdy1 March 18, 2025 05:53
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.

I'll review this very carefully with some time. Sorry for the delay, but this is a quite advanced optimization.

@kdy1 kdy1 requested a review from Copilot March 18, 2025 13:30
Copy link

@Copilot 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 implements changes to invoke IIFE blocks into a new block structure for various module outputs and async/await helper functions. The key changes include:

  • Updating snapshot test outputs for library sizes.
  • Rewriting module export patterns (AMD, CommonJS, UMD) to use a variable assignment with an "all" object and a for‑in loop.
  • Modifying async/await helper output by removing IIFE wrappers.

Reviewed Changes

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

Show a summary per file
File Description
crates/swc/tests/libs-size.snapshot.md Updated size snapshot values for various libraries.
crates/swc/tests/tsc-references/contextuallyTypedStringLiteralsInJsxAttributes02.2.minified.js Reordered export definitions with new function declarations.
crates/swc/tests/tsc-references/exportAsNamespace3(module=amd).2.minified.js Changed export pattern to use an "all" object and for‑in loop.
crates/swc/tests/tsc-references/exportAsNamespace2(module=amd).2.minified.js Changed export pattern to use an "all" object and for‑in loop.
crates/swc/tests/tsc-references/asyncAwaitIsolatedModules_es6.2.minified.js Removed IIFE wrapper in async/await helper function assignment.
crates/swc/tests/tsc-references/asyncAwaitIsolatedModules_es5.2.minified.js Removed IIFE wrapper in async/await helper function assignment.
crates/swc/tests/tsc-references/asyncAwait_es6.2.minified.js Removed IIFE wrapper in async/await helper function assignment.
crates/swc/tests/tsc-references/asyncAwait_es5.2.minified.js Removed IIFE wrapper in async/await helper function assignment.
crates/swc/tests/tsc-references/exportAsNamespace2(module=umd).2.minified.js Changed export pattern to use an "all" object and for‑in loop.
crates/swc/tests/tsc-references/exportAsNamespace2(module=commonjs).2.minified.js Changed export pattern to use an "all" object and for‑in loop.
crates/swc/tests/tsc-references/exportAsNamespace3(module=commonjs).2.minified.js Changed export pattern to use an "all" object and for‑in loop.
crates/swc/tests/tsc-references/exportAsNamespace3(module=umd).2.minified.js Changed export pattern to use an "all" object and for‑in loop.
crates/swc/tests/tsc-references/amdImportNotAsPrimaryExpression.2.minified.js Changed export pattern; removal of a redundant variable declaration.
crates/swc/tests/tsc-references/commonJSImportNotAsPrimaryExpression.2.minified.js Changed export pattern; removal of redundant declaration ordering.
crates/swc/tests/tsc-references/declarationEmitThisPredicates01.2.minified.js Changed export pattern to use variable assignment and "all" object.
crates/swc/tests/tsc-references/assignmentCompatWithDiscriminatedUnion.2.minified.js Updated call ordering within expression chains.
crates/swc/tests/tsc-references/emptyVariableDeclarationBindingPatterns01_ES6.2.minified.js Modified scope and declaration patterns in for‑of loops.
crates/swc/tests/tsc-references/exportAsNamespace1(module=amd).2.minified.js Changed export pattern to use an "all" object and for‑in loop.
crates/swc/tests/tsc-references/exportAsNamespace1(module=commonjs).2.minified.js Changed export pattern to use an "all" object and for‑in loop.
crates/swc/tests/tsc-references/exportAsNamespace1(module=umd).2.minified.js Changed export pattern to use an "all" object and for‑in loop.
Comments suppressed due to low confidence (2)

crates/swc/tests/tsc-references/amdImportNotAsPrimaryExpression.2.minified.js:14

  • The function for property 'E1' returns 'E11', which is not defined. This may cause a runtime error.
E1: function() { return E11; }

crates/swc/tests/tsc-references/commonJSImportNotAsPrimaryExpression.2.minified.js:10

  • The function for property 'E1' returns 'E11', which is not defined. This may cause a runtime error.
E1: function() { return E11; }

@@ -1,8 +1,6 @@
//// [assignmentCompatWithDiscriminatedUnion.ts]
Copy link
Member

Choose a reason for hiding this comment

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

Note from copilot:

The initialization order of the variables (Example1, Example2, Example3, Example4, Example5, GH14865, GH30170, GH12052, GH18421, GH15907, GH20889, and GH39357) remains the same before and after the changes. The primary difference is the movement of the getAxisType function call and the removal of a few lines, which does not affect the order of variable initialization.


has_default && has_non_empty_terminates
}
Stmt::Try(t) => {
Copy link
Member

Choose a reason for hiding this comment

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

What about the finally block?

Copy link
Member Author

Choose a reason for hiding this comment

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

finally block, despite run after every throw, would not change control flow, following code would not be executed if there's no catch

@kdy1 kdy1 requested a review from Copilot March 19, 2025 14:30
Copy link

@Copilot 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 implements a new feature in the ES minifier to invoke IIFEs into blocks, addressing missing scope support compared to gcc. The changes include refactoring how IIFEs and export objects are wrapped across various module formats (AMD, CommonJS, UMD) and updating snapshot files to reflect the new minified output.

  • Export object definitions are refactored to use a local “all” variable with an iteration to define properties.
  • The IIFE invocation for functions (e.g. for f1) is updated to use block scoping via immediate object assignment.
  • Snapshot files are updated to reflect minor size changes.

Reviewed Changes

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

Show a summary per file
File Description
crates/swc/tests/libs-size.snapshot.md Updated snapshot output values for compressed and gzipped sizes.
crates/swc/tests/tsc-references/contextuallyTypedStringLiteralsInJsxAttributes02.2.minified.js Reordered export definitions and function declarations for clarity.
crates/swc/tests/fixture/issues-4xxx/4953/output/index.js Wrapped the export assignment of “vr” in an IIFE to enforce strict mode.
Multiple files under crates/swc/tests/tsc-references/ Standardized export object creation across AMD, CommonJS, and UMD modules; updated async/await patterns accordingly.
crates/swc/tests/tsc-references/emptyVariableDeclarationBindingPatterns01_ES6.2.minified.js Adjusted loop binding declarations (using const over let) in destructuring loops.
Comments suppressed due to low confidence (1)

crates/swc/tests/tsc-references/amdImportNotAsPrimaryExpression.2.minified.js:15

  • The getter for E1 returns E11, which appears to be undefined; please verify whether it should return E1 instead.
return E11;

kdy1
kdy1 previously approved these changes Mar 19, 2025
@kdy1 kdy1 requested a review from a team as a code owner March 19, 2025 14:46
@kdy1 kdy1 merged commit c9a6c23 into swc-project:main Mar 19, 2025
18 checks passed
kdy1 added a commit that referenced this pull request Mar 20, 2025
This reverts commit c9a6c23.
kdy1 added a commit that referenced this pull request Mar 20, 2025
@kdy1 kdy1 modified the milestones: Planned, v1.11.12 Mar 23, 2025
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: deep inline functions with statements
2 participants