|
| 1 | +/* global __react_refresh_error_overlay__, __react_refresh_socket__, __resourceQuery */ |
| 2 | +/** |
| 3 | + * The following code is modified based on |
| 4 | + * https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/f1c8b9a44198449093ca95f85af5df97925e1cfc/client/ErrorOverlayEntry.js |
| 5 | + * |
| 6 | + * MIT Licensed |
| 7 | + * Author Michael Mok |
| 8 | + * Copyright (c) 2019 Michael Mok |
| 9 | + * https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/0b960573797bf38926937994c481e4fec9ed8aa6/LICENSE |
| 10 | + */ |
| 11 | +const events = require('./utils/errorEventHandlers.js'); |
| 12 | +const formatWebpackErrors = require('./utils/formatWebpackErrors.js'); |
| 13 | +const runWithRetry = require('./utils/retry.js'); |
| 14 | + |
| 15 | +// Setup error states |
| 16 | +let isHotReload = false; |
| 17 | +let hasRuntimeErrors = false; |
| 18 | + |
| 19 | +/** |
| 20 | + * Try dismissing the compile error overlay. |
| 21 | + * This will also reset runtime error records (if any), |
| 22 | + * because we have new source to evaluate. |
| 23 | + * @returns {void} |
| 24 | + */ |
| 25 | +function tryDismissErrorOverlay() { |
| 26 | + __react_refresh_error_overlay__.clearCompileError(); |
| 27 | + __react_refresh_error_overlay__.clearRuntimeErrors(!hasRuntimeErrors); |
| 28 | + hasRuntimeErrors = false; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * A function called after a compile success signal is received from Webpack. |
| 33 | + * @returns {void} |
| 34 | + */ |
| 35 | +function handleCompileSuccess() { |
| 36 | + isHotReload = true; |
| 37 | + |
| 38 | + if (isHotReload) { |
| 39 | + tryDismissErrorOverlay(); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * A function called after a compile errored signal is received from Webpack. |
| 45 | + * @param {string[]} errors |
| 46 | + * @returns {void} |
| 47 | + */ |
| 48 | +function handleCompileErrors(errors) { |
| 49 | + isHotReload = true; |
| 50 | + |
| 51 | + const formattedErrors = formatWebpackErrors(errors); |
| 52 | + |
| 53 | + // Only show the first error |
| 54 | + __react_refresh_error_overlay__.showCompileError(formattedErrors[0]); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Handles compilation messages from Webpack. |
| 59 | + * Integrates with a compile error overlay. |
| 60 | + * @param {*} message A Webpack HMR message sent via WebSockets. |
| 61 | + * @returns {void} |
| 62 | + */ |
| 63 | +function compileMessageHandler(message) { |
| 64 | + switch (message.type) { |
| 65 | + case 'ok': |
| 66 | + case 'still-ok': |
| 67 | + case 'warnings': { |
| 68 | + // TODO: Implement handling for warnings |
| 69 | + handleCompileSuccess(); |
| 70 | + break; |
| 71 | + } |
| 72 | + case 'errors': { |
| 73 | + handleCompileErrors(message.data); |
| 74 | + break; |
| 75 | + } |
| 76 | + default: { |
| 77 | + // Do nothing. |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +if (process.env.NODE_ENV !== 'production') { |
| 83 | + if (typeof window !== 'undefined') { |
| 84 | + function setupOverlay() { |
| 85 | + // Only register if no other overlay have been registered |
| 86 | + if (!window.__reactRefreshOverlayInjected && __react_refresh_socket__) { |
| 87 | + // Registers handlers for compile errors with retry - |
| 88 | + // This is to prevent mismatching injection order causing errors to be thrown |
| 89 | + runWithRetry(function initSocket() { |
| 90 | + __react_refresh_socket__.init(compileMessageHandler, __resourceQuery); |
| 91 | + }, 3); |
| 92 | + // Registers handlers for runtime errors |
| 93 | + events.handleError(function handleError(error) { |
| 94 | + hasRuntimeErrors = true; |
| 95 | + __react_refresh_error_overlay__.handleRuntimeError(error); |
| 96 | + }); |
| 97 | + events.handleUnhandledRejection( |
| 98 | + function handleUnhandledPromiseRejection(error) { |
| 99 | + hasRuntimeErrors = true; |
| 100 | + __react_refresh_error_overlay__.handleRuntimeError(error); |
| 101 | + }, |
| 102 | + ); |
| 103 | + |
| 104 | + // Mark overlay as injected to prevent double-injection |
| 105 | + window.__reactRefreshOverlayInjected = true; |
| 106 | + } |
| 107 | + } |
| 108 | + setupOverlay(); |
| 109 | + } |
| 110 | +} |
0 commit comments