-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathhelpers.js
216 lines (200 loc) Β· 6.79 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
var Refresh = require('react-refresh/runtime');
function debounce(func, delay) {
if (process.env.NODE_ENV === 'test') {
return function (args) {
func.call(null, args);
};
} else {
let timeout = undefined;
let lastTime = 0;
return function (args) {
// Call immediately if last call was more than the delay ago.
// Otherwise, set a timeout. This means the first call is fast
// (for the common case of a single update), and subsequent updates
// are batched.
let now = Date.now();
if (now - lastTime > delay) {
lastTime = now;
func.call(null, args);
} else {
clearTimeout(timeout);
timeout = setTimeout(function () {
timeout = undefined;
lastTime = Date.now();
func.call(null, args);
}, delay);
}
};
}
}
var enqueueUpdate = debounce(function () {
Refresh.performReactRefresh();
}, 30);
module.exports.init = function () {
if (!globalThis.$RefreshReg$) {
Refresh.injectIntoGlobalHook(globalThis);
globalThis.$RefreshReg$ = function () {};
globalThis.$RefreshSig$ = function () {
return function (type) {
return type;
};
};
if (typeof window !== 'undefined') {
let ErrorOverlay = require('react-error-overlay');
ErrorOverlay.setEditorHandler(function editorHandler(errorLocation) {
let file = `${errorLocation.fileName}:${
errorLocation.lineNumber || 1
}:${errorLocation.colNumber || 1}`;
fetch(`/__parcel_launch_editor?file=${encodeURIComponent(file)}`);
});
ErrorOverlay.startReportingRuntimeErrors({
onError: function () {},
});
window.addEventListener('parcelhmraccept', () => {
ErrorOverlay.dismissRuntimeErrors();
});
}
}
};
// Everything below is either adapted or copied from
// https://github.com/facebook/metro/blob/61de16bd1edd7e738dd0311c89555a644023ab2d/packages/metro/src/lib/polyfills/require.js
// MIT License - Copyright (c) Facebook, Inc. and its affiliates.
module.exports.prelude = function (module) {
globalThis.$RefreshReg$ = function (type, id) {
Refresh.register(type, module.id + ' ' + id);
};
globalThis.$RefreshSig$ = Refresh.createSignatureFunctionForTransform;
};
module.exports.postlude = function (module) {
if (typeof window === 'undefined') {
return;
}
if (isReactRefreshBoundary(module.exports)) {
registerExportsForReactRefresh(module);
if (module.hot) {
module.hot.dispose(function (data) {
if (Refresh.hasUnrecoverableErrors()) {
window.location.reload();
}
data.prevExports = module.exports;
});
module.hot.accept(function (getParents) {
var prevExports = module.hot.data.prevExports;
var nextExports = module.exports;
// Since we just executed the code for it, it's possible
// that the new exports make it ineligible for being a boundary.
var isNoLongerABoundary = !isReactRefreshBoundary(nextExports);
// It can also become ineligible if its exports are incompatible
// with the previous exports.
// For example, if you add/remove/change exports, we'll want
// to re-execute the importing modules, and force those components
// to re-render. Similarly, if you convert a class component
// to a function, we want to invalidate the boundary.
var didInvalidate = shouldInvalidateReactRefreshBoundary(
prevExports,
nextExports,
);
if (isNoLongerABoundary || didInvalidate) {
// We'll be conservative. The only case in which we won't do a full
// reload is if all parent modules are also refresh boundaries.
// In that case we'll add them to the current queue.
var parents = getParents();
if (parents.length === 0) {
// Looks like we bubbled to the root. Can't recover from that.
window.location.reload();
return;
}
return parents;
}
enqueueUpdate();
});
}
}
};
function isReactRefreshBoundary(exports) {
if (Refresh.isLikelyComponentType(exports)) {
return true;
}
if (exports == null || typeof exports !== 'object') {
// Exit if we can't iterate over exports.
return false;
}
var hasExports = false;
var areAllExportsComponents = true;
let isESM = '__esModule' in exports;
for (var key in exports) {
hasExports = true;
if (key === '__esModule') {
continue;
}
var desc = Object.getOwnPropertyDescriptor(exports, key);
if (desc && desc.get && !isESM) {
// Don't invoke getters for CJS as they may have side effects.
return false;
}
var exportValue = exports[key];
if (!Refresh.isLikelyComponentType(exportValue)) {
areAllExportsComponents = false;
}
}
return hasExports && areAllExportsComponents;
}
function shouldInvalidateReactRefreshBoundary(prevExports, nextExports) {
var prevSignature = getRefreshBoundarySignature(prevExports);
var nextSignature = getRefreshBoundarySignature(nextExports);
if (prevSignature.length !== nextSignature.length) {
return true;
}
for (var i = 0; i < nextSignature.length; i++) {
if (prevSignature[i] !== nextSignature[i]) {
return true;
}
}
return false;
}
// When this signature changes, it's unsafe to stop at this refresh boundary.
function getRefreshBoundarySignature(exports) {
var signature = [];
signature.push(Refresh.getFamilyByType(exports));
if (exports == null || typeof exports !== 'object') {
// Exit if we can't iterate over exports.
// (This is important for legacy environments.)
return signature;
}
let isESM = '__esModule' in exports;
for (var key in exports) {
if (key === '__esModule') {
continue;
}
var desc = Object.getOwnPropertyDescriptor(exports, key);
if (desc && desc.get && !isESM) {
// Don't invoke getters for CJS as they may have side effects.
continue;
}
var exportValue = exports[key];
signature.push(key);
signature.push(Refresh.getFamilyByType(exportValue));
}
return signature;
}
function registerExportsForReactRefresh(module) {
var exports = module.exports,
id = module.id;
Refresh.register(exports, id + ' %exports%');
if (exports == null || typeof exports !== 'object') {
// Exit if we can't iterate over exports.
// (This is important for legacy environments.)
return;
}
let isESM = '__esModule' in exports;
for (var key in exports) {
var desc = Object.getOwnPropertyDescriptor(exports, key);
if (desc && desc.get && !isESM) {
// Don't invoke getters for CJS as they may have side effects.
continue;
}
var exportValue = exports[key];
var typeID = id + ' %exports% ' + key;
Refresh.register(exportValue, typeID);
}
}