fix: port webpack fix for __webpack_module__ name collision#12956
fix: port webpack fix for __webpack_module__ name collision#12956
Conversation
- Add PartialEq, Eq to ModuleArgument enum - Implement pre_declarator hook to detect 'module' variable declarations - Implement pre_statement hook to detect 'module' function/class declarations - Update ModuleArgumentDependency template to use internal variables when collision detected - Add test cases for collision scenarios Co-authored-by: SyMind <19852293+SyMind@users.noreply.github.com>
Use fixed names for internal variables to match webpack's approach: - __webpack_internal_module_id__ for .id property - __webpack_internal_module_loaded__ for .loaded property - __webpack_internal_module__ for module itself Co-authored-by: SyMind <19852293+SyMind@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR ports a bug fix from webpack (PR #20265) to resolve name collision issues when user code declares a variable, function, or class named module at the top level while also using __webpack_module__.id or __webpack_module__. The fix prevents SyntaxError from illegal redeclarations in the generated code.
Changes:
- Added collision detection for top-level
moduledeclarations using parser plugin hooks - Modified code generation to use internal variables when collisions are detected
- Added
PartialEqandEqtraits toModuleArgumentenum for comparison
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
crates/rspack_core/src/module.rs |
Added PartialEq and Eq traits to ModuleArgument enum to enable comparison in collision detection logic |
crates/rspack_plugin_javascript/src/parser_plugin/api_plugin.rs |
Implemented pre_declarator and pre_statement hooks to detect top-level declarations of module and set module_argument to RspackModule when collision detected |
crates/rspack_plugin_javascript/src/dependency/module_argument_dependency.rs |
Modified template to emit init fragments with internal variables (__webpack_internal_module_id__, __webpack_internal_module__) when collision occurs, avoiding redeclaration errors |
tests/rspack-test/normalCases/parsing/webpack-module-collision/with-var-collision.js |
Test case for variable declaration collision with __webpack_module__.id |
tests/rspack-test/normalCases/parsing/webpack-module-collision/with-function-collision.js |
Test case for function declaration collision with __webpack_module__.id |
tests/rspack-test/normalCases/parsing/webpack-module-collision/with-class-collision.js |
Test case for class declaration collision with __webpack_module__.id |
tests/rspack-test/normalCases/parsing/webpack-module-collision/with-module-var-collision.js |
Test case for variable declaration collision with __webpack_module__ |
tests/rspack-test/normalCases/parsing/webpack-module-collision/index.js |
Test suite with assertions verifying correct handling of all collision scenarios |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
📦 Binary Size-limit
❌ Size increased by 256bytes from 48.59MB to 48.59MB (⬆️0.00%) |
Rsdoctor Bundle Diff AnalysisFound 5 projects in monorepo, 5 projects with changes. 📊 Quick Summary
📋 Detailed Reports (Click to expand)📁 react-10kPath:
📁 react-1kPath:
📁 react-5kPath:
📁 ui-componentsPath:
📁 romePath:
Generated by Rsdoctor GitHub Action |
CodSpeed Performance ReportMerging this PR will not alter performanceComparing Summary
Footnotes
|
Deploying rspack with
|
| Latest commit: |
5f60191
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://e7b7a832.rspack-v2.pages.dev |
| Branch Preview URL: | https://copilot-port-webpack-fix-to.rspack-v2.pages.dev |
f20f6c1 to
4585d68
Compare
* Initial plan * feat: port webpack fix for __webpack_module__ name collision - Implement pre_declarator hook to detect 'module' variable declarations - Implement pre_statement hook to detect 'module' function/class declarations - Add test cases for collision scenarios Co-authored-by: SyMind <19852293+SyMind@users.noreply.github.com> * fix: use fixed internal variable names for consistency Co-authored-by: SyMind <19852293+SyMind@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: SyMind <19852293+SyMind@users.noreply.github.com> Co-authored-by: Cong-Cong <dacongsama@live.com>
* Initial plan * feat: port webpack fix for __webpack_module__ name collision - Implement pre_declarator hook to detect 'module' variable declarations - Implement pre_statement hook to detect 'module' function/class declarations - Add test cases for collision scenarios Co-authored-by: SyMind <19852293+SyMind@users.noreply.github.com> * fix: use fixed internal variable names for consistency Co-authored-by: SyMind <19852293+SyMind@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: SyMind <19852293+SyMind@users.noreply.github.com> Co-authored-by: Cong-Cong <dacongsama@live.com>
…12988) fix: port webpack fix for __webpack_module__ name collision (#12956) * Initial plan * feat: port webpack fix for __webpack_module__ name collision - Implement pre_declarator hook to detect 'module' variable declarations - Implement pre_statement hook to detect 'module' function/class declarations - Add test cases for collision scenarios * fix: use fixed internal variable names for consistency --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: SyMind <19852293+SyMind@users.noreply.github.com>
Ports webpack fix from PR #20265 to resolve name collision when user code declares
moduleat top level while using__webpack_module__.idor__webpack_module__.Problem:
Generated code creates illegal redeclaration:
Changes:
pre_declaratorandpre_statementhooks inAPIPluginto detect top-level declarations ofmodule(variable, function, or class)module_argumenttoRspackModulewhen collision detected, changing parameter name frommoduleto__webpack_module__ModuleArgumentDependencyto emit init fragments with internal variables (__webpack_internal_module_id__,__webpack_internal_module__) when collision occursPartialEq+Eqtraits toModuleArgumentenumFixed output:
Upstream reference: webpack/webpack#20265
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.