Skip to content

Comments

feat: improve treeshaking logic to handle empty parameter list in dynamic import .then() callbacks#7781

Merged
sapphi-red merged 8 commits intomainfrom
copilot/fix-tree-shaking-behavior
Jan 7, 2026
Merged

feat: improve treeshaking logic to handle empty parameter list in dynamic import .then() callbacks#7781
sapphi-red merged 8 commits intomainfrom
copilot/fix-tree-shaking-behavior

Conversation

Copy link
Contributor

Copilot AI commented Jan 7, 2026

Fix tree shaking for dynamic imports with empty .then() callbacks

Issue

import('./dynamic.js').then(() => {}) should be treated the same as import('./dynamic.js').then(({}) => {}) when tree shaking, but currently the former bails out to complete usage while the latter correctly tree-shakes unused exports.

Root Cause

In crates/rolldown/src/ast_scanner/dynamic_import.rs, the init_dynamic_import_usage_with_member_expr function handles .then() calls. When the callback has no parameters (like () => {}), func.params.items.first() returns None, causing the function to bail out and mark the import as having complete usage. However, when there's an empty destructured object parameter (like ({}) => {}), it correctly identifies this as an empty set of exports being used.

Solution

Modified the init_dynamic_import_usage_with_member_expr function to:

  • Change func.params.items.first()? to func.params.items.first() (removing the ? operator)
  • Handle the None case explicitly by returning Some(FxHashSet::default()) which indicates an empty set of used exports
  • Use idiomatic let-else pattern for cleaner code structure
  • This matches the behavior of empty destructured object parameters

Changes

  • Understand the issue and locate root cause in ast_scanner/dynamic_import.rs
  • Create a test case to reproduce the issue (tree_shaking/dynamic_import_then_empty_param)
  • Fix the logic to handle empty parameter lists same as empty destructured patterns
  • Run targeted tests - new test passes with correct behavior
  • Run full integration test suite - all 682 tests pass
  • Address code review feedback - simplified test case and used let-else pattern
  • Update filename hash snapshot after test simplification
  • Security scan attempted (timed out, but changes are minimal and safe)

Testing

  • Created focused test case tree_shaking/dynamic_import_then_empty_param that demonstrates the fix
  • All 682 integration tests pass
  • All 33 dynamic import tests pass
  • Test shows .then(() => {}) produces optimized output with complete tree-shaking
  • Updated snapshot files to reflect test simplification
Original prompt

import('./dynamic.js').then(() => {}) should be treated the same with import('./dynamic.js').then(({}) => {}) when tree shaking, but it doesn't seem to be. Fix that.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Jan 7, 2026
@netlify
Copy link

netlify bot commented Jan 7, 2026

Deploy Preview for rolldown-rs canceled.

Name Link
🔨 Latest commit 7ca847f
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/695dfb53eb79fa000895dd10

Copilot AI changed the title [WIP] Fix tree shaking handling for import syntax variations fix(tree-shaking): handle empty parameter list in dynamic import .then() callbacks Jan 7, 2026
Copilot AI requested a review from sapphi-red January 7, 2026 05:29
Signed-off-by: 翠 <[email protected]>
@sapphi-red sapphi-red changed the title fix(tree-shaking): handle empty parameter list in dynamic import .then() callbacks feat: improve treeshaking logic to handle empty parameter list in dynamic import .then() callbacks Jan 7, 2026
@sapphi-red sapphi-red requested review from IWANABETHATGUY and removed request for sapphi-red January 7, 2026 05:58
@sapphi-red sapphi-red marked this pull request as ready for review January 7, 2026 05:58
Copilot AI review requested due to automatic review settings January 7, 2026 05:58
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 a tree-shaking inconsistency where dynamic imports with empty parameter lists in .then() callbacks were not being tree-shaken properly. The change ensures that import('./dynamic.js').then(() => {}) is treated the same as import('./dynamic.js').then(({}) => {}) for tree-shaking purposes.

Key changes:

  • Modified the parameter extraction logic to handle empty parameter lists explicitly
  • Changed from early-return-on-None (? operator) to explicit None handling with let-else pattern
  • Added test case demonstrating the fix

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
crates/rolldown/src/ast_scanner/dynamic_import.rs Core fix: Removes ? operator from parameter extraction and adds explicit handling for empty parameter lists to return empty usage set instead of bailing out
crates/rolldown/tests/rolldown/tree_shaking/dynamic_import_then_empty_param/main.js Test input demonstrating the issue: dynamic import with empty callback parameters
crates/rolldown/tests/rolldown/tree_shaking/dynamic_import_then_empty_param/lib.js Test library with exports that should be tree-shaken
crates/rolldown/tests/rolldown/tree_shaking/dynamic_import_then_empty_param/artifacts.snap Expected output snapshot showing proper tree-shaking with empty frozen object
crates/rolldown/tests/rolldown/tree_shaking/dynamic_import_then_empty_param/_config.json Empty test configuration file
crates/rolldown/tests/snapshots/integration_rolldown__filename_with_hash.snap Updated hash snapshot including new test case

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

@github-actions
Copy link
Contributor

github-actions bot commented Jan 7, 2026

Benchmarks Rust

  • target: main(e5a047d)
  • pr: copilot/fix-tree-shaking-behavior(7ca847f)
group                                                        pr                                     target
-----                                                        --                                     ------
bundle/bundle@multi-duplicated-top-level-symbol              1.00     63.1±1.37ms        ? ?/sec    1.01     64.0±1.40ms        ? ?/sec
bundle/bundle@multi-duplicated-top-level-symbol-sourcemap    1.00     69.4±1.86ms        ? ?/sec    1.01     70.4±1.90ms        ? ?/sec
bundle/bundle@rome_ts                                        1.00    104.7±2.74ms        ? ?/sec    1.00    104.5±1.75ms        ? ?/sec
bundle/bundle@rome_ts-sourcemap                              1.01    116.7±2.12ms        ? ?/sec    1.00    116.1±1.62ms        ? ?/sec
bundle/bundle@threejs                                        1.01     37.7±2.19ms        ? ?/sec    1.00     37.2±2.17ms        ? ?/sec
bundle/bundle@threejs-sourcemap                              1.00     41.6±0.50ms        ? ?/sec    1.00     41.7±1.02ms        ? ?/sec
bundle/bundle@threejs10x                                     1.00    379.4±2.91ms        ? ?/sec    1.00    381.2±5.52ms        ? ?/sec
bundle/bundle@threejs10x-sourcemap                           1.00    440.0±3.79ms        ? ?/sec    1.01    442.5±3.79ms        ? ?/sec
scan/scan@rome_ts                                            1.00     82.3±1.55ms        ? ?/sec    1.00     82.0±1.48ms        ? ?/sec
scan/scan@threejs                                            1.00     27.4±0.33ms        ? ?/sec    1.01     27.7±1.65ms        ? ?/sec
scan/scan@threejs10x                                         1.00    281.1±3.42ms        ? ?/sec    1.00    281.6±4.31ms        ? ?/sec

@sapphi-red
Copy link
Member

@copilot update the snapshot that happens with just test

Copy link
Contributor Author

Copilot AI commented Jan 7, 2026

@copilot update the snapshot that happens with just test

Done in 7ca847f. Updated the filename hash snapshot that changed due to test simplification.

Copilot AI requested a review from sapphi-red January 7, 2026 06:21
@sapphi-red sapphi-red merged commit bf9cc26 into main Jan 7, 2026
36 checks passed
@sapphi-red sapphi-red deleted the copilot/fix-tree-shaking-behavior branch January 7, 2026 06:55
@github-actions github-actions bot mentioned this pull request Jan 7, 2026
shulaoda added a commit that referenced this pull request Jan 7, 2026
## [1.0.0-beta.59] - 2026-01-07

⚡ Inline Dynamic Imports for Statically Imported Modules

- When a module is already statically imported, dynamic imports to that same module are now inlined instead of creating a separate chunk

### 🚀 Features

- plugin_timings: add 3s threshold and doc link to warning message (#7741) by @shulaoda
- improve treeshaking logic to handle empty parameter list in dynamic import .then() callbacks (#7781) by @Copilot
- dev/lazy: don't include already executed modules (#7745) by @hyf0
- dev/lazy: support dynamic `import(..)` (#7726) by @hyf0
- inline dynamic imports that imports statically imported modules (#7742) by @IWANABETHATGUY
- option: add experimental option to control chunk optimization (#7738) by @IWANABETHATGUY

### 🐛 Bug Fixes

- inline dynamic entry to user defined entry with esm wrap kind (#7783) by @IWANABETHATGUY
- use canonical namespace reference for property access (#7777) by @IWANABETHATGUY
- dynamic entry merged into common chunk with cjs and esm wrap kind (#7771) by @IWANABETHATGUY
- tla: should not await non-tla-related modules (#7768) by @hyf0
- dynamic entry captured by common chunk with CJS format (#7757) by @IWANABETHATGUY
- module_loader: mark emitted chunks as user-defined entry when already loaded (#7765) by @shulaoda
- normalize preserveModulesRoot path (#7737) by @IWANABETHATGUY
- linker: resolve race condition in side effects computation for export-star (#7728) by @camc314

### 🚜 Refactor

- plugin_timings: filter out plugins with duration < 1s from timing warnings (#7785) by @shulaoda
- module_loader: remove unnecessary collect before extend (#7769) by @shulaoda
- rename _id suffixes to _idx for oxc_index types (#7767) by @IWANABETHATGUY
- remove duplicate `preserve_entry_signatures` from `AddEntryModuleMsg` (#7762) by @shulaoda
- module_loader: pass `user_defined_entries` by reference (#7756) by @shulaoda
- dev/lazy: get proxy entry's `ResolvedId` correctly (#7746) by @hyf0
- simplify try_rewrite_import_expression control flow (#7753) by @IWANABETHATGUY
- module_loader: remove unnecessary dynamic import handling for runtime module (#7754) by @shulaoda
- inline __toDynamicImportESM  (#7747) by @IWANABETHATGUY
- use From impl for ModuleLoaderOutput conversion (#7732) by @shulaoda
- remove duplicate fields from `ModuleLoader` (#7731) by @shulaoda
- tweak `resolve_user_defined_entries` (#7727) by @shulaoda

### 📚 Documentation

- add rolldown-string reference to native MagicString compatibility section (#7778) by @Copilot
- improve comments for export star side effects handling (#7730) by @IWANABETHATGUY

### 🧪 Testing

- use assertion instead of console.log for some testcase (#7744) by @IWANABETHATGUY

### ⚙️ Miscellaneous Tasks

- tweak some `output.dynamicImportInCjs` related rollup test results (#7776) by @sapphi-red
- mark esbuild/dce/dce_of_symbol_ctor_call as passed (#7775) by @sapphi-red
- deps: update oxc apps (#7772) by @renovate[bot]
- vite-tests: allow running on PRs with `test: vite-tests` label (#7770) by @shulaoda
- deps: update oxc apps (#7760) by @renovate[bot]
- deps: update rollup submodule for tests to v4.55.1 (#7763) by @sapphi-red
- deps: update test262 submodule for tests (#7764) by @sapphi-red
- deps: update oxc to v0.107.0 (#7758) by @camc314
- deps: update taiki-e/install-action action to v2.65.13 (#7751) by @renovate[bot]
- deps: update rust crates (#7750) by @renovate[bot]
- deps: update npm packages (#7749) by @renovate[bot]
- deps: update github-actions (#7748) by @renovate[bot]
- deps: update dependency oxlint-tsgolint to v0.10.1 (#7729) by @renovate[bot]
- deps: update crate-ci/typos action to v1.41.0 (#7725) by @renovate[bot]

Co-authored-by: shulaoda <[email protected]>
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