Skip to content

fix: port webpack fix for __webpack_module__ name collision#12956

Merged
SyMind merged 7 commits intomainfrom
copilot/port-webpack-fix-to-rspark
Feb 9, 2026
Merged

fix: port webpack fix for __webpack_module__ name collision#12956
SyMind merged 7 commits intomainfrom
copilot/port-webpack-fix-to-rspark

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 5, 2026

Ports webpack fix from PR #20265 to resolve name collision when user code declares module at top level while using __webpack_module__.id or __webpack_module__.

Problem:

console.log(__webpack_module__.id);
const module = 'layout';  // Collision: SyntaxError in generated code

Generated code creates illegal redeclaration:

((module) => {
  console.log(module.id);
  const module = 'layout';  // SyntaxError: Identifier 'module' has already been declared
})

Changes:

  • Detection: Added pre_declarator and pre_statement hooks in APIPlugin to detect top-level declarations of module (variable, function, or class)
  • Switching: Set module_argument to RspackModule when collision detected, changing parameter name from module to __webpack_module__
  • Template update: Modified ModuleArgumentDependency to emit init fragments with internal variables (__webpack_internal_module_id__, __webpack_internal_module__) when collision occurs
  • Core: Added PartialEq + Eq traits to ModuleArgument enum

Fixed output:

((__webpack_module__) => {
  var __webpack_internal_module_id__ = __webpack_module__.id;
  console.log(__webpack_internal_module_id__);
  const module = 'layout';  // No collision
})

Upstream reference: webpack/webpack#20265

Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: fix: port webpack fix for issue #20264 (PR #20265)</issue_title>
<issue_description>### Description

We need to port a bug fix from Webpack to Rspack.

Upstream Context:

Task:
Please analyze the logic changes in the Webpack JS implementation and apply the equivalent logic to Rspack's Rust implementation.

Goal:
Ensure the Rspack implementation handles the path resolution logic correctly as per the upstream fix.

Context for Copilot

  • Source language: JavaScript (Webpack)
  • Target language: Rust (Rspack)
  • Please be careful with Rust types and Path handling.
    </issue_description>

<agent_instructions>I need to port logic from an external source. The reference logic is in this Webpack PR: webpack/webpack#20265. Please replicate this logic in the corresponding Rust files in this workspace.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits February 5, 2026 09:39
- 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>
Copilot AI changed the title [WIP] Port Webpack bug fix for path resolution to Rspack fix: port webpack fix for __webpack_module__ name collision Feb 5, 2026
Copilot AI requested a review from SyMind February 5, 2026 09:56
@SyMind SyMind marked this pull request as ready for review February 6, 2026 03:52
Copilot AI review requested due to automatic review settings February 6, 2026 03:52
@github-actions github-actions bot added the release: bug fix release: bug related release(mr only) label Feb 6, 2026
Copy link
Copy Markdown
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 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 module declarations using parser plugin hooks
  • Modified code generation to use internal variables when collisions are detected
  • Added PartialEq and Eq traits to ModuleArgument enum 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.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 6, 2026

📦 Binary Size-limit

Comparing 4585d68 to refactor: rename derefOption to stealCell and migrate atomicRefCell to Stealcell (#12974) by hardfist

❌ Size increased by 256bytes from 48.59MB to 48.59MB (⬆️0.00%)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 6, 2026

Rsdoctor Bundle Diff Analysis

Found 5 projects in monorepo, 5 projects with changes.

📊 Quick Summary
Project Total Size Change
react-10k 5.7 MB -
react-1k 825.4 KB -
react-5k 2.7 MB -
ui-components 2.2 MB -
rome 984.2 KB -
📋 Detailed Reports (Click to expand)

📁 react-10k

Path: ../build-tools-performance/cases/react-10k/dist/rsdoctor-data.json

⚠️ No baseline data found - Unable to perform comparison analysis

Metric Current Baseline Change
📊 Total Size 5.7 MB - -
📄 JavaScript 5.7 MB - -
🎨 CSS 21.0 B - -
🌐 HTML 0 B - -
📁 Other Assets 0 B - -

📁 react-1k

Path: ../build-tools-performance/cases/react-1k/dist/rsdoctor-data.json

⚠️ No baseline data found - Unable to perform comparison analysis

Metric Current Baseline Change
📊 Total Size 825.4 KB - -
📄 JavaScript 825.4 KB - -
🎨 CSS 0 B - -
🌐 HTML 0 B - -
📁 Other Assets 0 B - -

📁 react-5k

Path: ../build-tools-performance/cases/react-5k/dist/rsdoctor-data.json

⚠️ No baseline data found - Unable to perform comparison analysis

Metric Current Baseline Change
📊 Total Size 2.7 MB - -
📄 JavaScript 2.7 MB - -
🎨 CSS 21.0 B - -
🌐 HTML 0 B - -
📁 Other Assets 0 B - -

📁 ui-components

Path: ../build-tools-performance/cases/ui-components/dist/rsdoctor-data.json

⚠️ No baseline data found - Unable to perform comparison analysis

Metric Current Baseline Change
📊 Total Size 2.2 MB - -
📄 JavaScript 2.0 MB - -
🎨 CSS 267.8 KB - -
🌐 HTML 0 B - -
📁 Other Assets 0 B - -

📁 rome

Path: ../build-tools-performance/cases/rome/dist/rsdoctor-data.json

⚠️ No baseline data found - Unable to perform comparison analysis

Metric Current Baseline Change
📊 Total Size 984.2 KB - -
📄 JavaScript 984.2 KB - -
🎨 CSS 0 B - -
🌐 HTML 0 B - -
📁 Other Assets 0 B - -

Generated by Rsdoctor GitHub Action

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Feb 6, 2026

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing copilot/port-webpack-fix-to-rspark (4585d68) with main (aed1ab4)1

Summary

✅ 16 untouched benchmarks
⏩ 1 skipped benchmark2

Footnotes

  1. No successful run was found on main (3576494) during the generation of this report, so aed1ab4 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

  2. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Feb 6, 2026

Deploying rspack with  Cloudflare Pages  Cloudflare Pages

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

View logs

@SyMind SyMind force-pushed the copilot/port-webpack-fix-to-rspark branch from f20f6c1 to 4585d68 Compare February 9, 2026 04:01
@SyMind SyMind merged commit 5e3f3b2 into main Feb 9, 2026
49 checks passed
@SyMind SyMind deleted the copilot/port-webpack-fix-to-rspark branch February 9, 2026 05:07
SyMind added a commit that referenced this pull request Feb 9, 2026
* 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>
SyMind added a commit that referenced this pull request Feb 9, 2026
* 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>
SyMind added a commit that referenced this pull request Feb 9, 2026
…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>
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]: fix: port webpack fix for issue #20264 (PR #20265)

4 participants