Skip to content

Conversation

@xiaoxiaojx
Copy link
Member

@xiaoxiaojx xiaoxiaojx commented Nov 26, 2025

Summary
Fixes #20139

What kind of change does this PR introduce?
feat: provide a stable runtime function variable __webpack_global__

Did you add tests for your changes?
Yes

Does this PR introduce a breaking change?
No

If relevant, what needs to be documented once your changes are merged or what have you already documented?
update module-variables

@codspeed-hq
Copy link

codspeed-hq bot commented Nov 26, 2025

CodSpeed Performance Report

Merging #20170 will not alter performance

Comparing xiaoxiaojx:issue-20139-2 (87c5630) with main (c6215ca)

Summary

✅ 69 untouched

@alexander-akait
Copy link
Member

alexander-akait commented Nov 28, 2025

@JSerFeng Can you look at this solution too, thanks?

@JSerFeng
Copy link
Contributor

We Rspack, want to provide a global variable that can let user interop with current runtime, for example:

__webpack_global__ will be transformed to the __webpack_require__ in the output.

Once this is done, there should be no __webpack_global__ in the output, plugin author can make sure they just interop with runtime of current compilation.

And this PR inject __webpack_global__ through the module factory parameter, I may missing something, what if the module is entry and is concatenated, no module factory at all

@changeset-bot
Copy link

changeset-bot bot commented Nov 29, 2025

🦋 Changeset detected

Latest commit: 87c5630

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@xiaoxiaojx
Copy link
Member Author

xiaoxiaojx commented Nov 29, 2025

@JSerFeng We use the module factory parameter here because we want to provide a stable runtime variable, similar to Node.js, without needing any build-time intervention. As shown in the screenshot, __webpack_global__ remains intact after bundling — it’s purely a runtime variable.

When __webpack_global__ is used, webpack will only set allowInlineStartup: false to preserve the existence of the module factory.

We’re not handling this problem in a very complex way for now, to be honest, the implementation of react-refresh is a bit of a hack. Now that this problem #20139 can be solved.

image

@JSerFeng
Copy link
Contributor

JSerFeng commented Dec 1, 2025

If loader and plugin wants to inject code to do something with current webpack runtime, f user code has already __webpack_global__ definition, does the output code can correctly access the current webpack runtime instead of the __webpack_global__ in user code

@xiaoxiaojx

This comment was marked as resolved.

@xiaoxiaojx
Copy link
Member Author

Replacing it at build time sounds like a good idea. It also reminded me of how __webpack_public_path__ is used — for example, Next.js has code like this:

__webpack_public_path__ = "https://example.com/public/path/";

We can implement it by following the existing behavior of __webpack_public_path__.

@xiaoxiaojx xiaoxiaojx changed the title feat: provide a stable runtime function variable __webpack_global__ feat: adds the global variable __webpack_global__ for this compilation Dec 1, 2025
@alexander-akait alexander-akait merged commit 5983843 into webpack:main Dec 2, 2025
50 of 51 checks passed
@alexander-akait
Copy link
Member

Thanks

@github-actions
Copy link
Contributor

github-actions bot commented Dec 2, 2025

This PR is packaged and the instant preview is available (5983843).

Install it locally:

  • npm
npm i -D webpack@https://pkg.pr.new/webpack@5983843
  • yarn
yarn add -D webpack@https://pkg.pr.new/webpack@5983843
  • pnpm
pnpm add -D webpack@https://pkg.pr.new/webpack@5983843

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.

[Bug] The handling of __webpack_require__ caused a runtime error of React refresh

3 participants