fix(esm-library): deconflict module external helper names#13695
Conversation
Merging this PR will degrade performance by 4.12%
Performance Changes
Comparing |
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
There was a problem hiding this comment.
Pull request overview
Fixes ESM-library rendering deconfliction when ModuleExternal init fragments introduce helper bindings that can collide with source/module top-level bindings (notably the node-commonjs createRequire helper).
Changes:
- Collect and reserve top-level declarations introduced by
ModuleExternalinit fragments prior to top-level symbol deconfliction. - Refactor module-external init-fragment collection to keep reservation behavior aligned with init-fragment render ordering and key de-duplication.
- Add unit tests for the new reservation behavior and add an ESM output test case covering helper-shaped source bindings plus
node-commonjsexternals.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
crates/rspack_plugin_esm_library/src/link.rs |
Reserves additional top-level decls from ModuleExternal init fragments and adds unit coverage for the new parsing/reservation logic. |
tests/rspack-test/esmOutputCases/externals/node-commonjs-helper-name-collision/rspack.config.js |
Adds a repro config that externalizes fs as node-commonjs. |
tests/rspack-test/esmOutputCases/externals/node-commonjs-helper-name-collision/index.js |
Adds a repro entry asserting helper-name deconfliction against source bindings and require("fs"). |
tests/rspack-test/esmOutputCases/externals/node-commonjs-helper-name-collision/__snapshots__/esm.snap.txt |
Snapshot verifying generated ESM output uses deconflicted helper names. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Summary
This PR fixes modern-module ESM rendering when a
ModuleExternalinit fragment introduces top-level bindings that collide with source bindings during deconfliction.It also adds an externals case that covers helper-shaped source code directly in
index.js, including a literalrequire("fs"), while the project external forfsis also configured asnode-commonjs.Root Cause
rspack_plugin_esm_librarypreviously reserved namespace-import locals fromModuleExternalinit fragments, but it did not reserve other top-level declarations introduced by those fragments. That allowed generated helpers like__rspack_createRequireand__rspack_createRequire_requireto collide with user/source bindings in ESM library rendering.What Changed
ModuleExternalinit fragments before top-level symbol deconflictionlink.rsrequire("fs"), whilefsis also anode-commonjsexternalValidation
cargo test -p rspack_plugin_esm_library link --libcargo fmt --all --checkcd tests/rspack-test && PATH="/tmp/codex-bin:$HOME/.nvm/versions/node/v20.19.0/bin:$PATH" pnpm testu EsmOutput.test.js -t "externals/node-commonjs-helper-name-collision"cd tests/rspack-test && PATH="/tmp/codex-bin:$HOME/.nvm/versions/node/v20.19.0/bin:$PATH" pnpm test EsmOutput.test.js -t "externals/node-commonjs-helper-name-collision"