Skip to content

Commit 5983843

Browse files
authored
feat: adds the global variable __webpack_global__ for this compilation (#20170)
1 parent c6215ca commit 5983843

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

.changeset/shaggy-humans-grin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Provide a stable runtime function variable `__webpack_global__`.

lib/APIPlugin.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ function getReplacements() {
4242
type: "function",
4343
assign: false
4444
},
45+
__webpack_global__: {
46+
expr: RuntimeGlobals.require,
47+
req: [RuntimeGlobals.require],
48+
type: "function",
49+
assign: false
50+
},
4551
__webpack_public_path__: {
4652
expr: RuntimeGlobals.publicPath,
4753
req: [RuntimeGlobals.publicPath],
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const bar = "bar";
2+
__webpack_global__.bar = "bar";
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
__webpack_global__.myCustomProperty = "myCustomProperty";
2+
3+
"use strict";
4+
var __webpack_require__ = {
5+
test(__webpack_global__) {
6+
__webpack_global__.test = 1;
7+
}
8+
};
9+
(() => {
10+
__webpack_require__.d = (exports1, definition) => {
11+
for (var key in definition)
12+
if (
13+
__webpack_require__.o(definition, key) &&
14+
!__webpack_require__.o(exports1, key)
15+
)
16+
Object.defineProperty(exports1, key, {
17+
enumerable: true,
18+
get: definition[key],
19+
});
20+
};
21+
})();
22+
(() => {
23+
__webpack_require__.o = (obj, prop) =>
24+
Object.prototype.hasOwnProperty.call(obj, prop);
25+
})();
26+
(() => {
27+
__webpack_require__.r = (exports1) => {
28+
if ("undefined" != typeof Symbol && Symbol.toStringTag)
29+
Object.defineProperty(exports1, Symbol.toStringTag, {
30+
value: "Module",
31+
});
32+
Object.defineProperty(exports1, "__esModule", {
33+
value: true,
34+
});
35+
};
36+
})();
37+
var __webpack_exports__ = {};
38+
__webpack_require__.r(__webpack_exports__);
39+
__webpack_require__.d(__webpack_exports__, {
40+
Counter: () => Counter,
41+
});
42+
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
43+
const Counter = () =>
44+
/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
45+
children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("h1", {
46+
className: "title",
47+
children: "React",
48+
}),
49+
});
50+
exports.Counter = __webpack_exports__.Counter;
51+
for (var __webpack_i__ in __webpack_exports__)
52+
if (-1 === ["Counter"].indexOf(__webpack_i__))
53+
exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
54+
Object.defineProperty(exports, "__esModule", {
55+
value: true,
56+
});
57+
58+
__webpack_global__.anotherProperty = "anotherProperty";
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as foo from "./foo";
2+
import { bar } from "./bar";
3+
4+
it("should load module with nested __webpack_require__", () => {
5+
expect(foo).toBeDefined();
6+
expect(bar).toBe("bar");
7+
});
8+
9+
it("should provide __webpack_global__ when module uses __webpack_require__", () => {
10+
expect(__webpack_require__.myCustomProperty).toBe("myCustomProperty");
11+
expect(__webpack_global__.anotherProperty).toBe("anotherProperty");
12+
expect(__webpack_global__.bar).toBe("bar");
13+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration[]} */
4+
module.exports = [
5+
{},
6+
{
7+
optimization: {
8+
concatenateModules: true
9+
}
10+
}
11+
];

0 commit comments

Comments
 (0)