Skip to content

Commit e35dc79

Browse files
authored
fix(esm-output): __webpack_require__.C is not a function (#656)
1 parent fa44ec1 commit e35dc79

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

packages/core/src/core/plugins/mockRuntimeCode.js

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,32 @@
22
// Rstest runtime code should be prefixed with `rstest_` to avoid conflicts with other runtimes.
33

44
const originalWebpackRequire = __webpack_require__;
5-
__webpack_require__ = function (...args) {
6-
try {
7-
return originalWebpackRequire(...args);
8-
} catch (e) {
9-
const errMsg = e.message ?? e.toString();
10-
if (errMsg.includes('__webpack_modules__[moduleId] is not a function')) {
11-
throw new Error(`[Rstest] Cannot find module "${args[0]}"`);
5+
__webpack_require__ = new Proxy(
6+
function (...args) {
7+
try {
8+
return originalWebpackRequire(...args);
9+
} catch (e) {
10+
const errMsg = e.message ?? e.toString();
11+
if (errMsg.includes('__webpack_modules__[moduleId] is not a function')) {
12+
throw new Error(`[Rstest] Cannot find module "${args[0]}"`);
13+
}
14+
throw e;
1215
}
13-
throw e;
14-
}
15-
};
16-
17-
Object.keys(originalWebpackRequire).forEach((key) => {
18-
__webpack_require__[key] = originalWebpackRequire[key];
19-
});
16+
},
17+
{
18+
set(target, property, value) {
19+
target[property] = value;
20+
originalWebpackRequire[property] = value;
21+
return true;
22+
},
23+
get(target, property) {
24+
if (property in target) {
25+
return target[property];
26+
}
27+
return originalWebpackRequire[property];
28+
},
29+
},
30+
);
2031

2132
__webpack_require__.rstest_original_modules = {};
2233
__webpack_require__.rstest_original_module_factories = {};
@@ -50,7 +61,7 @@ __webpack_require__.rstest_require_actual =
5061

5162
// #region rs.mock
5263
__webpack_require__.rstest_mock = (id, modFactory) => {
53-
let requiredModule = undefined;
64+
let requiredModule;
5465
try {
5566
requiredModule = __webpack_require__(id);
5667
} catch {
@@ -85,7 +96,7 @@ __webpack_require__.rstest_mock = (id, modFactory) => {
8596

8697
// #region rs.mockRequire
8798
__webpack_require__.rstest_mock_require = (id, modFactory) => {
88-
let requiredModule = undefined;
99+
let requiredModule;
89100
try {
90101
requiredModule = __webpack_require__(id);
91102
} catch {
@@ -107,7 +118,7 @@ __webpack_require__.rstest_mock_require = (id, modFactory) => {
107118

108119
// #region rs.doMock
109120
__webpack_require__.rstest_do_mock = (id, modFactory) => {
110-
let requiredModule = undefined;
121+
let requiredModule;
111122
try {
112123
requiredModule = __webpack_require__(id);
113124
} catch {
@@ -128,7 +139,7 @@ __webpack_require__.rstest_do_mock = (id, modFactory) => {
128139

129140
// #region rs.doMockRequire
130141
__webpack_require__.rstest_do_mock_require = (id, modFactory) => {
131-
let requiredModule = undefined;
142+
let requiredModule;
132143
try {
133144
requiredModule = __webpack_require__(id);
134145
} catch {

0 commit comments

Comments
 (0)