Skip to content

Commit a836e06

Browse files
authored
fix: rename __webpack_require__ in arrow function scopes (#20661)
1 parent 0b344de commit a836e06

5 files changed

Lines changed: 27 additions & 5 deletions

File tree

.changeset/late-dingos-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Fix `CompatibilityPlugin` to correctly rename `__webpack_require__` when it appears as an arrow function parameter (e.g. `(__webpack_module, __webpack_exports, __webpack_require__) => { ... }`).

lib/CompatibilityPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class CompatibilityPlugin {
147147
range: /** @type {Range} */ (pattern.range)
148148
}
149149
});
150-
if (!parser.scope.topLevelScope) {
150+
if (parser.scope.topLevelScope !== true) {
151151
return true;
152152
}
153153
});

test/configCases/module/consume-webpack-runtime/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import defaultUse from './runtime-export-default'
33
import { __webpack_require__ as namedDeclUse } from './runtime-export-decl'
44
import { __webpack_require__ as objectRequire, __webpack_exports__ as objectExport } from './runtime-single-require-and-export'
55
import defaultUseNested from './runtime-multiple-nested'
6+
import "./runtime-arrow-scope";
67

78
it("should compile and run", () => {
89
expect(namedUse()).toBe(42);
@@ -17,7 +18,7 @@ it("should compile and run", () => {
1718
{
1819
const content = fs.readFileSync(path.resolve(__dirname, './bundle0.js'), 'utf-8');
1920
const NESTED_RE = /__nested_webpack_require_([^_]+)__/g;
20-
expect(content.match(NESTED_RE).length).toBe(13);
21+
expect(content.match(NESTED_RE).length).toBe(17);
2122
}
2223

2324
{
@@ -29,7 +30,7 @@ it("should compile and run", () => {
2930
{
3031
const content = fs.readFileSync(path.resolve(__dirname, './bundle1.js'), 'utf-8');
3132
const NESTED_RE = /__nested_webpack_require_([^_]+)__/g;
32-
expect(content.match(NESTED_RE).length).toBe(15);
33+
expect(content.match(NESTED_RE).length).toBe(19);
3334
}
3435

3536
{
@@ -41,7 +42,7 @@ it("should compile and run", () => {
4142
{
4243
const content = fs.readFileSync(path.resolve(__dirname, './bundle2.js'), 'utf-8');
4344
const NESTED_RE = /__nested_webpack_require_([^_]+)__/g;
44-
expect(content.match(NESTED_RE).length).toBe(13);
45+
expect(content.match(NESTED_RE).length).toBe(17);
4546
}
4647

4748
{
@@ -53,7 +54,7 @@ it("should compile and run", () => {
5354
{
5455
const content = fs.readFileSync(path.resolve(__dirname, './bundle3.js'), 'utf-8');
5556
const NESTED_RE = /__nested_webpack_require_([^_]+)__/g;
56-
expect(content.match(NESTED_RE).length).toBe(15);
57+
expect(content.match(NESTED_RE).length).toBe(19);
5758
}
5859

5960
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function __webpack_require__() {
2+
throw new Error("Should be unreached");
3+
}
4+
5+
const modules = [
6+
// `__webpack_require__` as arrow fn params should be replaced
7+
((__webpack_require__) => {
8+
require("./shared");
9+
try{
10+
__webpack_require__()
11+
} catch {}
12+
13+
})
14+
];
15+
16+
modules[0](__webpack_require__);

test/configCases/module/consume-webpack-runtime/shared.js

Whitespace-only changes.

0 commit comments

Comments
 (0)