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