UNPKG

3.32 kBJavaScriptView Raw
1/* global __react_refresh_error_overlay__, __react_refresh_test__, __react_refresh_utils__, __webpack_require__ */
2
3/**
4 * Code appended to each JS-like module for react-refresh capabilities.
5 *
6 * `__react_refresh_utils__` will be replaced with actual utils during source parsing by `webpack.ProvidePlugin`.
7 *
8 * The function declaration syntax below is needed for `Template.getFunctionContent` to parse this.
9 *
10 * [Reference for Runtime Injection](https://github.com/webpack/webpack/blob/b07d3b67d2252f08e4bb65d354a11c9b69f8b434/lib/HotModuleReplacementPlugin.js#L419)
11 * [Reference for HMR Error Recovery](https://github.com/webpack/webpack/issues/418#issuecomment-490296365)
12 */
13module.exports = function () {
14 const currentExports = __react_refresh_utils__.getModuleExports(module.id);
15 __react_refresh_utils__.registerExportsForReactRefresh(currentExports, module.id);
16
17 if (module.hot) {
18 const isHotUpdate = !!module.hot.data;
19 const prevExports = isHotUpdate ? module.hot.data.prevExports : null;
20
21 if (__react_refresh_utils__.isReactRefreshBoundary(currentExports)) {
22 module.hot.dispose(
23 /**
24 * A callback to performs a full refresh if React has unrecoverable errors,
25 * and also caches the to-be-disposed module.
26 * @param {*} data A hot module data object from Webpack HMR.
27 * @returns {void}
28 */
29 function hotDisposeCallback(data) {
30 // We have to mutate the data object to get data registered and cached
31 data.prevExports = currentExports;
32 }
33 );
34 module.hot.accept(
35 /**
36 * An error handler to allow self-recovering behaviours.
37 * @param {Error} error An error occurred during evaluation of a module.
38 * @returns {void}
39 */
40 function hotErrorHandler(error) {
41 if (
42 typeof __react_refresh_error_overlay__ !== 'undefined' &&
43 __react_refresh_error_overlay__
44 ) {
45 __react_refresh_error_overlay__.handleRuntimeError(error);
46 }
47
48 if (typeof __react_refresh_test__ !== 'undefined' && __react_refresh_test__) {
49 if (window.onHotAcceptError) {
50 window.onHotAcceptError(error.message);
51 }
52 }
53
54 __webpack_require__.c[module.id].hot.accept(hotErrorHandler);
55 }
56 );
57
58 if (isHotUpdate) {
59 if (
60 __react_refresh_utils__.isReactRefreshBoundary(prevExports) &&
61 __react_refresh_utils__.shouldInvalidateReactRefreshBoundary(prevExports, currentExports)
62 ) {
63 module.hot.invalidate();
64 } else {
65 __react_refresh_utils__.enqueueUpdate(
66 /**
67 * A function to dismiss the error overlay after performing React refresh.
68 * @returns {void}
69 */
70 function updateCallback() {
71 if (
72 typeof __react_refresh_error_overlay__ !== 'undefined' &&
73 __react_refresh_error_overlay__
74 ) {
75 __react_refresh_error_overlay__.clearRuntimeErrors();
76 }
77 }
78 );
79 }
80 }
81 } else {
82 if (isHotUpdate && __react_refresh_utils__.isReactRefreshBoundary(prevExports)) {
83 module.hot.invalidate();
84 }
85 }
86 }
87};