RefreshModule.runtime.js 3.2 KB

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