|
1 |
| -/* global __react_refresh_error_overlay__, __react_refresh_socket__, __resourceQuery */ |
| 1 | +/* global __react_refresh_error_overlay__, __react_refresh_socket__ */ |
2 | 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'); |
| 3 | +if (process.env.NODE_ENV !== 'production') { |
| 4 | + const events = require('./utils/errorEventHandlers.js'); |
| 5 | + const formatWebpackErrors = require('./utils/formatWebpackErrors.js'); |
| 6 | + const runWithRetry = require('./utils/retry.js'); |
7 | 7 |
|
8 |
| -// Setup error states |
9 |
| -let isHotReload = false; |
10 |
| -let hasRuntimeErrors = false; |
| 8 | + // Setup error states |
| 9 | + let isHotReload = false; |
| 10 | + let hasRuntimeErrors = false; |
11 | 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 |
| -} |
| 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 | + const tryDismissErrorOverlay = function () { |
| 19 | + __react_refresh_error_overlay__.clearCompileError(); |
| 20 | + __react_refresh_error_overlay__.clearRuntimeErrors(!hasRuntimeErrors); |
| 21 | + hasRuntimeErrors = false; |
| 22 | + }; |
23 | 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; |
| 24 | + /** |
| 25 | + * A function called after a compile success signal is received from Webpack. |
| 26 | + * @returns {void} |
| 27 | + */ |
| 28 | + const handleCompileSuccess = function () { |
| 29 | + isHotReload = true; |
30 | 30 |
|
31 |
| - if (isHotReload) { |
32 |
| - tryDismissErrorOverlay(); |
33 |
| - } |
34 |
| -} |
| 31 | + if (isHotReload) { |
| 32 | + tryDismissErrorOverlay(); |
| 33 | + } |
| 34 | + }; |
35 | 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; |
| 36 | + /** |
| 37 | + * A function called after a compile errored signal is received from Webpack. |
| 38 | + * @param {string[]} errors |
| 39 | + * @returns {void} |
| 40 | + */ |
| 41 | + const handleCompileErrors = function (errors) { |
| 42 | + isHotReload = true; |
43 | 43 |
|
44 |
| - const formattedErrors = formatWebpackErrors(errors); |
| 44 | + const formattedErrors = formatWebpackErrors(errors); |
45 | 45 |
|
46 |
| - // Only show the first error |
47 |
| - __react_refresh_error_overlay__.showCompileError(formattedErrors[0]); |
48 |
| -} |
| 46 | + // Only show the first error |
| 47 | + __react_refresh_error_overlay__.showCompileError(formattedErrors[0]); |
| 48 | + }; |
49 | 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. |
| 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 | + const compileMessageHandler = function (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 | + } |
71 | 72 | }
|
72 |
| - } |
73 |
| -} |
| 73 | + }; |
74 | 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 |
| - } |
| 75 | + // Only register if no other overlay have been registered |
| 76 | + if ( |
| 77 | + typeof window !== 'undefined' && |
| 78 | + !window.__reactRefreshOverlayInjected && |
| 79 | + __react_refresh_socket__ |
| 80 | + ) { |
| 81 | + // Registers handlers for compile errors with retry - |
| 82 | + // This is to prevent mismatching injection order causing errors to be thrown |
| 83 | + runWithRetry( |
| 84 | + function initSocket() { |
| 85 | + __react_refresh_socket__.init(compileMessageHandler); |
| 86 | + }, |
| 87 | + 3, |
| 88 | + 'Failed to set up the socket connection.' |
| 89 | + ); |
| 90 | + // Registers handlers for runtime errors |
| 91 | + events.handleError(function handleError(error) { |
| 92 | + hasRuntimeErrors = true; |
| 93 | + __react_refresh_error_overlay__.handleRuntimeError(error); |
98 | 94 | });
|
| 95 | + events.handleUnhandledRejection(function handleUnhandledPromiseRejection(error) { |
| 96 | + hasRuntimeErrors = true; |
| 97 | + __react_refresh_error_overlay__.handleRuntimeError(error); |
| 98 | + }); |
| 99 | + |
| 100 | + // Mark overlay as injected to prevent double-injection |
| 101 | + window.__reactRefreshOverlayInjected = true; |
99 | 102 | }
|
100 | 103 | }
|
0 commit comments