Skip to content

Comments

refactor: inline __toDynamicImportESM #7747

Merged
graphite-app[bot] merged 1 commit intomainfrom
01-04-refactor_inline___todynamicimportesm
Jan 5, 2026
Merged

refactor: inline __toDynamicImportESM #7747
graphite-app[bot] merged 1 commit intomainfrom
01-04-refactor_inline___todynamicimportesm

Conversation

@IWANABETHATGUY
Copy link
Member

@IWANABETHATGUY IWANABETHATGUY commented Jan 4, 2026

I found an issue while trying to solve #4905. Here is an example https://github.com/rolldown/rolldown/pull/7742/changes#diff-627d2ba1517f46e8cfe17e7ce9efe06e8b51f83a3c3ff87586c415981e3e5bc3. When a module dynamically imports a CommonJS module, we always generate code like:

import("./foo.js").then(__toDynamicImportESM()).then(({ default: { bar: b } }) => {})

But after implementing the #4905 optimization, when the importer and the importee are in the same chunk, we generate code like:

Promise.resolve().then(() => __toESM(require_foo())).then(mod) => {
	console.log(mod);
});

Since the chunk optimization happens after linking state, we should reference both __toDynamicImportESM and __toESM runtime helpers. If the runtime module is included in a different chunk,
one of __toESM and __toDynamicImportESM may not be used but is still included with the import statement, which can't be eliminated by the minifier.

Inlining __toDynamicImportESM could solve this issue.

Pros

  1. Resolves the issue mentioned above.
  2. Improves run-time performance a little.
  3. Reduces the overall size of the runtime modules.

Cons

  1. Increases the generated output size a little. m.default can't be compressed by the minifier.

I am open to both solution.

@IWANABETHATGUY IWANABETHATGUY changed the title refactor: inline __toDynamicImportESM refactor: inline __toDynamicImportESM helper with arrow function Jan 4, 2026
Copy link
Member Author


How to use the Graphite Merge Queue

Add the label graphite: merge-when-ready to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@netlify
Copy link

netlify bot commented Jan 4, 2026

Deploy Preview for rolldown-rs canceled.

Name Link
🔨 Latest commit 6c69f39
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/695b489b8ffa9700088a4e5d

@IWANABETHATGUY IWANABETHATGUY changed the title refactor: inline __toDynamicImportESM helper with arrow function refactor: inline __toDynamicImportESM Jan 4, 2026
@IWANABETHATGUY IWANABETHATGUY force-pushed the 01-04-refactor_inline___todynamicimportesm branch from 842410e to 04c0010 Compare January 4, 2026 15:47
@IWANABETHATGUY IWANABETHATGUY marked this pull request as ready for review January 4, 2026 15:57
Copilot AI review requested due to automatic review settings January 4, 2026 15:57
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 refactors the __toDynamicImportESM runtime helper by inlining its implementation directly at call sites. This optimization solves an issue where unused runtime helpers may be included in chunks when CommonJS modules are dynamically imported, particularly when chunk optimization happens after the linking stage.

Key Changes:

  • Removed __toDynamicImportESM from the base runtime helpers and inlined its logic (__toESM(m.default, isNodeMode)) at call sites
  • Reduced runtime helper count from 20 to 19 and adjusted bit flags accordingly
  • Updated code generation to emit arrow functions for dynamic imports: .then((m) => __toESM(m.default))

Reviewed changes

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

Show a summary per file
File Description
crates/rolldown/src/runtime/runtime-base.js Removed __toDynamicImportESM export and associated formatting cleanup
crates/rolldown_common/src/generated/runtime_helper.rs Removed ToDynamicImportEsm flag, adjusted bit positions, and updated helper count from 20 to 19
crates/rolldown/src/stages/link_stage/reference_needed_symbols.rs Updated to reference ToEsm helper instead of ToDynamicImportEsm for CommonJS dynamic imports
crates/rolldown/src/module_finalizers/mod.rs Inlined __toDynamicImportESM logic in two places: ESM format (arrow function) and CJS format (wrapped in Promise)
crates/rolldown_plugin_hmr/src/runtime/runtime-extra-dev-common.js Kept __toDynamicImportESM as inline method for HMR runtime, calling __toESM(mod.default, isNodeMode)
Various test snapshots Updated expected outputs to reflect inlined transformation and resulting hash changes

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

@IWANABETHATGUY IWANABETHATGUY force-pushed the 01-04-refactor_inline___todynamicimportesm branch from c8096f0 to 2d67725 Compare January 4, 2026 16:07
Copilot AI review requested due to automatic review settings January 4, 2026 16:13
@IWANABETHATGUY IWANABETHATGUY force-pushed the 01-04-refactor_inline___todynamicimportesm branch from 2d67725 to 1fca581 Compare January 4, 2026 16:13
@IWANABETHATGUY IWANABETHATGUY requested a review from hyf0 January 4, 2026 16:16
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

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


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

@github-actions
Copy link
Contributor

github-actions bot commented Jan 4, 2026

Benchmarks Rust

  • target: main(d8ea7f1)
  • pr: 01-04-refactor_inline___todynamicimportesm(6d80943)
group                                                        pr                                     target
-----                                                        --                                     ------
bundle/bundle@multi-duplicated-top-level-symbol              1.12     69.2±2.61ms        ? ?/sec    1.00     61.6±1.15ms        ? ?/sec
bundle/bundle@multi-duplicated-top-level-symbol-sourcemap    1.10     76.2±3.57ms        ? ?/sec    1.00     69.4±2.49ms        ? ?/sec
bundle/bundle@rome_ts                                        1.07    111.7±2.53ms        ? ?/sec    1.00    104.5±2.11ms        ? ?/sec
bundle/bundle@rome_ts-sourcemap                              1.09    126.4±3.03ms        ? ?/sec    1.00    116.2±1.67ms        ? ?/sec
bundle/bundle@threejs                                        1.10     40.4±2.43ms        ? ?/sec    1.00     36.8±0.50ms        ? ?/sec
bundle/bundle@threejs-sourcemap                              1.06     44.6±0.75ms        ? ?/sec    1.00     41.9±0.56ms        ? ?/sec
bundle/bundle@threejs10x                                     1.02    407.7±7.01ms        ? ?/sec    1.00    400.8±8.72ms        ? ?/sec
bundle/bundle@threejs10x-sourcemap                           1.00    465.0±5.76ms        ? ?/sec    1.01   471.1±10.57ms        ? ?/sec
scan/scan@rome_ts                                            1.01     89.0±1.96ms        ? ?/sec    1.00     88.2±2.24ms        ? ?/sec
scan/scan@threejs                                            1.00     30.2±1.73ms        ? ?/sec    1.00     30.2±1.78ms        ? ?/sec
scan/scan@threejs10x                                         1.01    301.9±5.31ms        ? ?/sec    1.00    297.8±5.66ms        ? ?/sec

@IWANABETHATGUY IWANABETHATGUY force-pushed the 01-04-refactor_inline___todynamicimportesm branch from 1fca581 to 82b4c05 Compare January 5, 2026 01:53
Copilot AI review requested due to automatic review settings January 5, 2026 02:13
@IWANABETHATGUY IWANABETHATGUY force-pushed the 01-04-refactor_inline___todynamicimportesm branch from 82b4c05 to 6d80943 Compare January 5, 2026 02:13
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

Copilot reviewed 16 out of 17 changed files in this pull request and generated no new comments.


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

Copy link
Member

@hyf0 hyf0 left a comment

Choose a reason for hiding this comment

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

LGTM

@hyf0
Copy link
Member

hyf0 commented Jan 5, 2026

@sapphi-red cc

@graphite-app
Copy link
Contributor

graphite-app bot commented Jan 5, 2026

Merge activity

I found an issue while trying to solve #4905. Here is an example https://github.com/rolldown/rolldown/pull/7742/changes#diff-627d2ba1517f46e8cfe17e7ce9efe06e8b51f83a3c3ff87586c415981e3e5bc3. When a module dynamically imports a CommonJS module, we always generate code like:

```js
import("./foo.js").then(__toDynamicImportESM()).then(({ default: { bar: b } }) => {})
```

But after implementing the #4905 optimization, when the importer and the importee are in the same chunk, we generate code like:

```js
Promise.resolve().then(() => __toESM(require_foo())).then(mod) => {
	console.log(mod);
});
```

Since the chunk optimization happens after linking state, we should reference both `__toDynamicImportESM` and `__toESM` runtime helpers. If the runtime module is included in a different chunk,
one of `__toESM` and `__toDynamicImportESM` may not be used but is still included with the `import` statement, which can't be eliminated by the minifier.

Inlining `__toDynamicImportESM` could solve this issue.

### Pros
1. Resolves the issue mentioned above.
2. Improves run-time performance a little.
3. Reduces the overall size of the runtime modules.

### Cons
1. Increases the generated output size a little. `m.default` can't be compressed by the minifier.

I am open to both solution.
@graphite-app graphite-app bot force-pushed the 01-04-refactor_inline___todynamicimportesm branch from 6d80943 to 6c69f39 Compare January 5, 2026 05:14
@graphite-app graphite-app bot merged commit 6c69f39 into main Jan 5, 2026
33 checks passed
@graphite-app graphite-app bot deleted the 01-04-refactor_inline___todynamicimportesm branch January 5, 2026 05:23
This was referenced 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.

2 participants