RefreshModule.runtime.js
3.24 KB
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
/* global __react_refresh_error_overlay__, __react_refresh_test__, __react_refresh_utils__, __webpack_require__ */
/**
* Code appended to each JS-like module for react-refresh capabilities.
*
* `__react_refresh_utils__` will be replaced with actual utils during source parsing by `webpack.ProvidePlugin`.
*
* The function declaration syntax below is needed for `Template.getFunctionContent` to parse this.
*
* [Reference for Runtime Injection](https://github.com/webpack/webpack/blob/b07d3b67d2252f08e4bb65d354a11c9b69f8b434/lib/HotModuleReplacementPlugin.js#L419)
* [Reference for HMR Error Recovery](https://github.com/webpack/webpack/issues/418#issuecomment-490296365)
*/
module.exports = function () {
const currentExports = __react_refresh_utils__.getModuleExports(module.id);
__react_refresh_utils__.registerExportsForReactRefresh(currentExports, module.id);
if (module.hot) {
const isHotUpdate = !!module.hot.data;
const prevExports = isHotUpdate ? module.hot.data.prevExports : null;
if (__react_refresh_utils__.isReactRefreshBoundary(currentExports)) {
module.hot.dispose(
/**
* A callback to performs a full refresh if React has unrecoverable errors,
* and also caches the to-be-disposed module.
* @param {*} data A hot module data object from Webpack HMR.
* @returns {void}
*/
function hotDisposeCallback(data) {
// We have to mutate the data object to get data registered and cached
data.prevExports = currentExports;
}
);
module.hot.accept(
/**
* An error handler to allow self-recovering behaviours.
* @param {Error} error An error occurred during evaluation of a module.
* @returns {void}
*/
function hotErrorHandler(error) {
if (
typeof __react_refresh_error_overlay__ !== 'undefined' &&
__react_refresh_error_overlay__
) {
__react_refresh_error_overlay__.handleRuntimeError(error);
}
if (typeof __react_refresh_test__ !== 'undefined' && __react_refresh_test__) {
if (window.onHotAcceptError) {
window.onHotAcceptError(error.message);
}
}
__webpack_require__.c[module.id].hot.accept(hotErrorHandler);
}
);
if (isHotUpdate) {
if (
__react_refresh_utils__.isReactRefreshBoundary(prevExports) &&
__react_refresh_utils__.shouldInvalidateReactRefreshBoundary(prevExports, currentExports)
) {
module.hot.invalidate();
} else {
__react_refresh_utils__.enqueueUpdate(
/**
* A function to dismiss the error overlay after performing React refresh.
* @returns {void}
*/
function updateCallback() {
if (
typeof __react_refresh_error_overlay__ !== 'undefined' &&
__react_refresh_error_overlay__
) {
__react_refresh_error_overlay__.clearRuntimeErrors();
}
}
);
}
}
} else {
if (isHotUpdate && __react_refresh_utils__.isReactRefreshBoundary(prevExports)) {
module.hot.invalidate();
}
}
}
};