Skip to content

fix: align module external remapping with webpack#13802

Merged
SyMind merged 15 commits into
web-infra-dev:mainfrom
JSerFeng:fy/issue-13733-webpack-module-external-remap
May 6, 2026
Merged

fix: align module external remapping with webpack#13802
SyMind merged 15 commits into
web-infra-dev:mainfrom
JSerFeng:fy/issue-13733-webpack-module-external-remap

Conversation

@JSerFeng

Copy link
Copy Markdown
Contributor

Summary

  • align module external remapping with webpack by generating a remapped namespace object instead of using mangled bundle-local names as external property access
  • add focused Rust unit coverage for external remapping behavior in rspack_core
  • add two regression fixtures: the original output-module/issue-13733 reproducer and a migrated webpack node/output-module-external case

Root cause

When a module external fell back to namespace access in module output, Rspack mixed two different name domains:

  • the bundle-internal used_name produced by export mangling
  • the real export name exposed by the external module

That made namespace property access use mangled names like ns.a instead of the real external export name such as ns.EventEmitter.

Impact

This keeps module externals compatible with optimization.mangleExports, concatenation, and namespace fallback behavior, while matching webpack's remapping strategy more closely.

Fixes #13733.

Validation

  • cargo test -p rspack_core module_external_remapping --lib -- --nocapture
  • cargo test -p rspack_plugin_javascript --lib -- --nocapture
  • cargo test -p rspack_plugin_esm_library --lib -- --nocapture
  • cargo fmt --all --check
  • git diff --check
  • node --check tests/rspack-test/configCases/node/output-module-external/import.js
  • node --check tests/rspack-test/configCases/node/output-module-external/require.js
  • node --check tests/rspack-test/configCases/node/output-module-external/rspack.config.js
  • node --check tests/rspack-test/configCases/node/output-module-external/test.config.js
  • node --check tests/rspack-test/configCases/node/output-module-external/test.filter.js
  • node --check tests/rspack-test/configCases/node/output-module-external/stream.js
  • node -e "console.log(require('./tests/rspack-test/configCases/node/output-module-external/test.filter.js')())"

Notes

  • I could not run the new rspack-test config cases locally because this environment does not have pnpm or corepack. The migrated JS fixture was syntax-checked instead.

@codspeed-hq

codspeed-hq Bot commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 34 untouched benchmarks


Comparing JSerFeng:fy/issue-13733-webpack-module-external-remap (4f959ed) with main (c44e6a5)

Open in CodSpeed

@JSerFeng

Copy link
Copy Markdown
Contributor Author

@codex review

@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: d3eddc5975

ℹ️ 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 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 address that feedback".

Comment thread crates/rspack_core/src/external_module.rs Outdated
@JSerFeng JSerFeng marked this pull request as ready for review April 23, 2026 06:16
Copilot AI review requested due to automatic review settings April 23, 2026 06:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Aligns Rspack’s module externals behavior with webpack when optimization.mangleExports is enabled by generating a remapped namespace object for external property access, preventing mangled bundle-local names from being used against real external exports. Adds targeted Rust unit coverage and new rspack-test fixtures to prevent regressions (incl. #13733).

Changes:

  • Implement module-external namespace remapping logic in rspack_core external module codegen.
  • Add Rust unit tests validating remapping behavior (including nested exports).
  • Add regression fixtures for output-module/issue-13733 and migrated node/output-module-external cases.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
crates/rspack_core/src/external_module.rs Implements module-external remapping logic and adds focused Rust unit tests.
tests/rspack-test/configCases/output-module/issue-13733/test.config.js Adds bundle selection for the new regression case.
tests/rspack-test/configCases/output-module/issue-13733/rspack.config.js Reproducer config for #13733 (module output + mangleExports + chunk merge).
tests/rspack-test/configCases/output-module/issue-13733/index.js Asserts emitted bundle output and runtime behavior stay correct.
tests/rspack-test/configCases/output-module/issue-13733/a.js Minimal ESM import that exercises named module externals.
tests/rspack-test/configCases/output-module/issue-13733/b.js Async import side module to force chunk merge scenario.
tests/rspack-test/configCases/node/output-module-external/test.filter.js Filters test execution based on Node support for createRequire.
tests/rspack-test/configCases/node/output-module-external/test.config.js Adds bundle selection for migrated node builtin externals case.
tests/rspack-test/configCases/node/output-module-external/stream.js Helper module for stream assertion in the require-based test.
tests/rspack-test/configCases/node/output-module-external/rspack.config.js Configures outputModule build and injects NODE_VERSION.
tests/rspack-test/configCases/node/output-module-external/require.js Migrated require-based node builtin external coverage.
tests/rspack-test/configCases/node/output-module-external/import.js Migrated dynamic-import-based node builtin external coverage.

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

Comment thread crates/rspack_core/src/external_module.rs
Comment thread crates/rspack_core/src/external_module.rs Outdated
Comment thread tests/rspack-test/configCases/node/output-module-external/import.js Outdated
@JSerFeng JSerFeng changed the title [codex] fix: align module external remapping with webpack fix: align module external remapping with webpack Apr 23, 2026
@github-actions github-actions Bot added the release: bug fix release: bug related release(mr only) label Apr 23, 2026
@JSerFeng

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

ℹ️ 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 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 address that feedback".

@JSerFeng

Copy link
Copy Markdown
Contributor Author

@codex review

@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: 52e71a8947

ℹ️ 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 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 address that feedback".

Comment thread crates/rspack_core/src/external_module.rs
@JSerFeng

JSerFeng commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

ℹ️ 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 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 address that feedback".

@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

📝 Benchmark detail: Open

Name Base (3c0eafe) Current Change
10000_big_production-mode_disable-minimize + exec 14.8 s ± 203 ms 15 s ± 140 ms +1.30 %
10000_development-mode + exec 904 ms ± 16 ms 917 ms ± 24 ms +1.39 %
10000_development-mode_hmr + stats 165 ms ± 8.9 ms 165 ms ± 5 ms +0.34 %
10000_development-mode_noop-loader + exec 1.95 s ± 80 ms 1.97 s ± 79 ms +1.09 %
10000_production-mode + exec 1.02 s ± 38 ms 1.04 s ± 44 ms +2.18 %
10000_production-mode_persistent-cold + exec 1.18 s ± 38 ms 1.18 s ± 26 ms -0.07 %
10000_production-mode_persistent-hot + exec 598 ms ± 13 ms 607 ms ± 18 ms +1.54 %
10000_production-mode_source-map + exec 1.2 s ± 20 ms 1.2 s ± 38 ms +0.15 %
arco-pro_development-mode + exec 1.32 s ± 103 ms 1.33 s ± 81 ms +1.24 %
arco-pro_development-mode_hmr + stats 34 ms ± 0.8 ms 34 ms ± 0.56 ms -0.05 %
arco-pro_production-mode + exec 2.41 s ± 116 ms 2.4 s ± 111 ms -0.41 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 2.49 s ± 41 ms 2.45 s ± 109 ms -1.79 %
arco-pro_production-mode_persistent-cold + exec 2.49 s ± 119 ms 2.47 s ± 95 ms -1.15 %
arco-pro_production-mode_persistent-hot + exec 340 ms ± 2.5 ms 344 ms ± 12 ms +1.41 %
arco-pro_production-mode_source-map + exec 2.86 s ± 110 ms 2.87 s ± 132 ms +0.17 %
arco-pro_production-mode_traverse-chunk-modules + exec 2.44 s ± 119 ms 2.46 s ± 100 ms +1.08 %
bundled-threejs_development-mode + exec 179 ms ± 3.5 ms 186 ms ± 5.7 ms +3.91 %
bundled-threejs_production-mode + exec 209 ms ± 3.4 ms 216 ms ± 4.2 ms +3.55 %
large-dyn-imports_development-mode + exec 1.14 s ± 36 ms 1.17 s ± 20 ms +1.83 %
large-dyn-imports_production-mode + exec 1.27 s ± 67 ms 1.25 s ± 7.8 ms -1.27 %
threejs_development-mode_10x + exec 816 ms ± 42 ms 811 ms ± 30 ms -0.56 %
threejs_development-mode_10x_hmr + stats 112 ms ± 1 ms 113 ms ± 2.6 ms +0.89 %
threejs_production-mode_10x + exec 2.89 s ± 31 ms 2.89 s ± 24 ms -0.03 %
threejs_production-mode_10x_persistent-cold + exec 3.01 s ± 20 ms 3.02 s ± 45 ms +0.63 %
threejs_production-mode_10x_persistent-hot + exec 379 ms ± 10 ms 384 ms ± 9.1 ms +1.53 %
threejs_production-mode_10x_source-map + exec 3.58 s ± 18 ms 3.59 s ± 33 ms +0.36 %
10000_big_production-mode_disable-minimize + rss memory 2181 MiB ± 55.8 MiB 2100 MiB ± 78.8 MiB -3.72 %
10000_development-mode + rss memory 669 MiB ± 38.2 MiB 668 MiB ± 19.5 MiB -0.03 %
10000_development-mode_hmr + rss memory 954 MiB ± 25.7 MiB 959 MiB ± 19.2 MiB +0.53 %
10000_development-mode_noop-loader + rss memory 981 MiB ± 13.3 MiB 990 MiB ± 15.7 MiB +0.96 %
10000_production-mode + rss memory 648 MiB ± 18.4 MiB 641 MiB ± 18 MiB -0.99 %
10000_production-mode_persistent-cold + rss memory 796 MiB ± 12.8 MiB 800 MiB ± 38.3 MiB +0.38 %
10000_production-mode_persistent-hot + rss memory 699 MiB ± 9.68 MiB 705 MiB ± 15.8 MiB +0.89 %
10000_production-mode_source-map + rss memory 670 MiB ± 13.9 MiB 677 MiB ± 25.4 MiB +1.06 %
arco-pro_development-mode + rss memory 597 MiB ± 30.7 MiB 615 MiB ± 29.2 MiB +2.91 %
arco-pro_development-mode_hmr + rss memory 676 MiB ± 24.7 MiB 668 MiB ± 50.4 MiB -1.22 %
arco-pro_production-mode + rss memory 793 MiB ± 50.6 MiB 792 MiB ± 14 MiB -0.04 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 810 MiB ± 18.3 MiB 812 MiB ± 28.1 MiB +0.16 %
arco-pro_production-mode_persistent-cold + rss memory 905 MiB ± 26.9 MiB 918 MiB ± 34.1 MiB +1.51 %
arco-pro_production-mode_persistent-hot + rss memory 502 MiB ± 30.5 MiB 494 MiB ± 22.3 MiB -1.55 %
arco-pro_production-mode_source-map + rss memory 978 MiB ± 38.3 MiB 967 MiB ± 23.3 MiB -1.14 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 793 MiB ± 29.8 MiB 800 MiB ± 38.9 MiB +0.95 %
bundled-threejs_development-mode + rss memory 166 MiB ± 10.8 MiB 163 MiB ± 11.4 MiB -2.23 %
bundled-threejs_production-mode + rss memory 206 MiB ± 21 MiB 211 MiB ± 38.6 MiB +2.34 %
large-dyn-imports_development-mode + rss memory 660 MiB ± 14.2 MiB 654 MiB ± 15 MiB -0.89 %
large-dyn-imports_production-mode + rss memory 506 MiB ± 15.3 MiB 507 MiB ± 10.1 MiB +0.22 %
threejs_development-mode_10x + rss memory 586 MiB ± 8.56 MiB 587 MiB ± 25.5 MiB +0.04 %
threejs_development-mode_10x_hmr + rss memory 771 MiB ± 19.6 MiB 778 MiB ± 37.7 MiB +0.82 %
threejs_production-mode_10x + rss memory 856 MiB ± 25 MiB 850 MiB ± 11.3 MiB -0.66 %
threejs_production-mode_10x_persistent-cold + rss memory 1025 MiB ± 25.7 MiB 1024 MiB ± 28.1 MiB -0.07 %
threejs_production-mode_10x_persistent-hot + rss memory 574 MiB ± 15.9 MiB 583 MiB ± 17.1 MiB +1.57 %
threejs_production-mode_10x_source-map + rss memory 1033 MiB ± 33.5 MiB 1034 MiB ± 45.8 MiB +0.04 %

Base persistent cache hit rate:

  • 10000_production-mode_persistent-cold: 0.00%
  • arco-pro_production-mode_persistent-cold: 0.00%
  • threejs_production-mode_10x_persistent-cold: 0.00%
    Current persistent cache hit rate:
  • 10000_production-mode_persistent-cold: 0.00%
  • arco-pro_production-mode_persistent-cold: 0.00%
  • threejs_production-mode_10x_persistent-cold: 0.00%

@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

📝 Rspack Ecosystem CI: Open

suite result
modernjs ✅ success
rsbuild ✅ success
rstest ✅ success
rspress ✅ success
rslib ✅ success
lynx-stack ❌ failure
devserver ✅ success
examples ❌ failure
rsdoctor ✅ success
plugin ✅ success
rsbuild-rsc-plugin ✅ success

@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

📝 Rspack Ecosystem CI: Open

suite result
modernjs ✅ success
rsdoctor ✅ success
rsbuild ✅ success
rstest ✅ success
devserver ❌ failure
rspress ✅ success
examples ✅ success
plugin ❌ failure
lynx-stack ❌ failure
rsbuild-rsc-plugin ✅ success
rslib ✅ success

ahabhgk
ahabhgk previously approved these changes May 6, 2026
@SyMind SyMind merged commit 4cddada into web-infra-dev:main May 6, 2026
53 of 55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release: bug fix release: bug related release(mr only)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: rspack produces broken code when optimization.mangleExports = "size" and LimitChunkCountPlugin.maxChunks = 1

4 participants