|
5 | 5 | // to allow capturing nested require calls. |
6 | 6 |
|
7 | 7 | (function (globalObject) { |
8 | | - // @ts-ignore |
9 | | - const originalDefine = globalObject.__d; |
| 8 | + const myRequire = function (id) { |
| 9 | + return globalObject.__r(id); |
| 10 | + }; |
10 | 11 |
|
11 | | - if (!originalDefine) { |
12 | | - // If __d is not defined, we are probably not in a Metro environment or |
13 | | - // the module system hasn't loaded yet. |
14 | | - return; |
15 | | - } |
| 12 | + const myImportDefault = function (id) { |
| 13 | + return globalObject.__r.importDefault(id); |
| 14 | + }; |
| 15 | + |
| 16 | + const myImportAll = function (id) { |
| 17 | + return globalObject.__r.importAll(id); |
| 18 | + }; |
16 | 19 |
|
17 | 20 | // Monkey-patch define |
18 | | - // @ts-ignore |
| 21 | + const originalDefine = globalObject.__d; |
19 | 22 | globalObject.__d = function (factory, moduleId, dependencyMap) { |
20 | | - // Create a wrapped factory |
21 | 23 | const wrappedFactory = function (...args) { |
22 | | - // 1. Your Custom Require |
23 | | - const myRequire = function (id) { |
24 | | - // Logic to capture/redirect the require |
25 | | - // globalObject.__r is the global require function from Metro |
26 | | - // @ts-ignore |
27 | | - return globalObject.__r(id); |
28 | | - }; |
29 | | - |
30 | | - // 2. Custom importDefault (MUST use myRequire) |
31 | | - const myImportDefault = function (id) { |
32 | | - const mod = myRequire(id); |
33 | | - return mod && mod.__esModule ? mod.default : mod; |
34 | | - }; |
35 | | - |
36 | | - // 3. Custom importAll (MUST use myRequire) |
37 | | - const myImportAll = function (id) { |
38 | | - const mod = myRequire(id); |
39 | | - if (mod && mod.__esModule) { |
40 | | - return mod; |
41 | | - } |
42 | | - |
43 | | - const result = {}; |
44 | | - if (mod) { |
45 | | - for (const key in mod) { |
46 | | - if (Object.prototype.hasOwnProperty.call(mod, key)) { |
47 | | - result[key] = mod[key]; |
48 | | - } |
49 | | - } |
50 | | - } |
51 | | - result.default = mod; |
52 | | - return result; |
53 | | - }; |
54 | | - |
55 | 24 | // Standard Metro with import support (7 arguments) |
56 | 25 | // args: global, require, importDefault, importAll, module, exports, dependencyMap |
57 | 26 | const global = args[0]; |
|
74 | 43 | return originalDefine.call(this, wrappedFactory, moduleId, dependencyMap); |
75 | 44 | }; |
76 | 45 |
|
77 | | - // Implement __clearModule |
78 | | - // This allows the test runner to re-evaluate modules by clearing them from the cache |
79 | | - globalObject.__clearModule = function (moduleId) { |
80 | | - if (globalObject.__r && globalObject.__r.getModules) { |
81 | | - const modules = globalObject.__r.getModules(); |
82 | | - if (modules && modules.has(moduleId)) { |
83 | | - modules.delete(moduleId); |
84 | | - } |
| 46 | + globalObject.__resetModule = function (moduleId) { |
| 47 | + const module = globalObject.__r.getModules().get(moduleId); |
| 48 | + |
| 49 | + if (!module) { |
| 50 | + return; |
85 | 51 | } |
| 52 | + |
| 53 | + module.hasError = false; |
| 54 | + module.error = undefined; |
| 55 | + module.isInitialized = false; |
86 | 56 | }; |
87 | 57 |
|
88 | | - // Implement __resetAllModules |
89 | | - // This allows the test runner to reset the state of all modules |
90 | | - globalObject.__resetAllModules = function () { |
91 | | - if (globalObject.__r && globalObject.__r.getModules) { |
92 | | - const modules = globalObject.__r.getModules(); |
93 | | - if (modules) { |
94 | | - modules.forEach(function (mod, moduleId) { |
95 | | - if (mod) { |
96 | | - // We need to create a new object to ensure that the module is re-evaluated |
97 | | - // Mutating existing module directly might not work as expected in some cases |
98 | | - const newMod = {}; |
99 | | - for (const key in mod) { |
100 | | - if (Object.prototype.hasOwnProperty.call(mod, key)) { |
101 | | - newMod[key] = mod[key]; |
102 | | - } |
103 | | - } |
104 | | - newMod.isInitialized = false; |
105 | | - // Reset publicModule.exports to ensure a clean start |
106 | | - // This is crucial because if we reuse the old exports object, |
107 | | - // the module factory might append to it instead of overwriting it, |
108 | | - // or we might be left with stale state (e.g., class definitions) |
109 | | - newMod.publicModule = { exports: {} }; |
110 | | - |
111 | | - // Also clear error state |
112 | | - newMod.hasError = false; |
113 | | - newMod.error = undefined; |
| 58 | + globalObject.__resetModules = function () { |
| 59 | + const modules = globalObject.__r.getModules(); |
114 | 60 |
|
115 | | - modules.set(moduleId, newMod); |
116 | | - } |
117 | | - }); |
118 | | - } |
119 | | - } |
| 61 | + modules.forEach(function (mod, moduleId) { |
| 62 | + globalObject.__resetModule(moduleId); |
| 63 | + }); |
120 | 64 | }; |
121 | 65 | })( |
122 | 66 | typeof globalThis !== 'undefined' |
|
0 commit comments