1 |
|
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 |
|
9 | let isHotReload = false;
|
10 | let hasRuntimeErrors = false;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function tryDismissErrorOverlay() {
|
19 | __react_refresh_error_overlay__.clearCompileError();
|
20 | __react_refresh_error_overlay__.clearRuntimeErrors(!hasRuntimeErrors);
|
21 | hasRuntimeErrors = false;
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function handleCompileSuccess() {
|
29 | isHotReload = true;
|
30 |
|
31 | if (isHotReload) {
|
32 | tryDismissErrorOverlay();
|
33 | }
|
34 | }
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | function handleCompileErrors(errors) {
|
42 | isHotReload = true;
|
43 |
|
44 | const formattedErrors = formatWebpackErrors(errors);
|
45 |
|
46 |
|
47 | __react_refresh_error_overlay__.showCompileError(formattedErrors[0]);
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function compileMessageHandler(message) {
|
57 | switch (message.type) {
|
58 | case 'ok':
|
59 | case 'still-ok':
|
60 | case 'warnings': {
|
61 |
|
62 | handleCompileSuccess();
|
63 | break;
|
64 | }
|
65 | case 'errors': {
|
66 | handleCompileErrors(message.data);
|
67 | break;
|
68 | }
|
69 | default: {
|
70 |
|
71 | }
|
72 | }
|
73 | }
|
74 |
|
75 | if (process.env.NODE_ENV !== 'production') {
|
76 | if (typeof window !== 'undefined') {
|
77 | runWithPatchedUrl(function setupOverlay() {
|
78 |
|
79 | if (!window.__reactRefreshOverlayInjected && __react_refresh_socket__) {
|
80 |
|
81 |
|
82 | runWithRetry(function initSocket() {
|
83 | __react_refresh_socket__.init(compileMessageHandler, __resourceQuery);
|
84 | }, 3);
|
85 |
|
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 |
|
96 | window.__reactRefreshOverlayInjected = true;
|
97 | }
|
98 | });
|
99 | }
|
100 | }
|