Skip to content

feat: enable tree-shaking for ESM external modules with named imports #19641

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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

3ru
Copy link
Contributor

@3ru 3ru commented Jun 29, 2025

This PR completes the implementation started in #19416, resolving the InitFragment handling issue that was blocking the original work. The main problem was that external modules were not generating proper named imports when using module concatenation, instead falling back to namespace imports.

What kind of change does this PR introduce?

Feature completion - This change enable tree-shaking optimization for external ESM modules in the ModuleConcatenationPlugin by:

1. InitFragment Collection in ModuleConcatenationPlugin

  • Collects InitFragments from external modules during code generation
  • Properly adds them to chunkInitFragments for correct import statement generation
  • Ensures the concatenation process preserves the generated imports

2. External Module Analysis in _analyseModule

  • Added export tracking for ESM external modules to optimize tree-shaking
  • Populates exportMap and rawExportMap based on getUsedExports() results
  • Only analyzes when externalType === "module" and outputting ES modules

3. ConcatenationScope Integration

  • Creates proper scope for external modules to handle name collisions

Did you add tests for your changes?

Yes, added the modern-module-tree-shaking-simple test case that:

  • Verifies named imports are generated instead of namespace imports

Does this PR introduce a breaking change?

No

What needs to be documented once your changes are merged?

Copy link

codspeed-hq bot commented Jun 30, 2025

CodSpeed Performance Report

Merging #19641 will degrade performances by 94.11%

Comparing 3ru:continue-pr-19416-external-module-render (41948a4) with main (5bcd259)

Summary

⚡ 1 improvements
❌ 4 regressions
✅ 128 untouched benchmarks

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

Benchmarks breakdown

Benchmark BASE HEAD Change
benchmark "devtool-eval-source-map", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 39.3 ms 45.2 ms -13.11%
benchmark "devtool-source-map", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 10.4 ms 42.8 ms -75.75%
benchmark "future-defaults", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 49.5 ms 11.8 ms ×4.2
benchmark "minimal", scenario '{"name":"mode-development","mode":"development"}' 24.2 ms 30.7 ms -20.94%
benchmark "three-long", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 13.2 ms 224.1 ms -94.11%

@3ru 3ru force-pushed the continue-pr-19416-external-module-render branch from c6e4dd3 to 7a37133 Compare June 30, 2025 01:08
@3ru 3ru force-pushed the continue-pr-19416-external-module-render branch 5 times, most recently from 5514a63 to 39ee0a6 Compare June 30, 2025 04:56
@3ru
Copy link
Contributor Author

3ru commented Jun 30, 2025

@alexander-akait I tried a few different yarn fix commands but couldn't get the lint CI to pass. Could you help fix this? (Might need to update snapshots too, maybe?)
Thanks!

@xiaoxiaojx
Copy link
Member

xiaoxiaojx commented Jun 30, 2025

@3ru You can run yarn fix:special to apply the necessary code fixes.
Once the PR is ready, please git rebase to squash and clean up the commits before merging.

@3ru 3ru force-pushed the continue-pr-19416-external-module-render branch 2 times, most recently from eeebd81 to 39ee0a6 Compare July 1, 2025 04:45
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.

4 participants