Skip to content

Commit c896a8a

Browse files
authored
fix: make plugin entries empty for production (#900)
1 parent 0166c14 commit c896a8a

File tree

2 files changed

+91
-89
lines changed

2 files changed

+91
-89
lines changed

client/ErrorOverlayEntry.js

Lines changed: 88 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,103 @@
11
/* global __react_refresh_error_overlay__, __react_refresh_socket__ */
22

3-
const events = require('./utils/errorEventHandlers.js');
4-
const formatWebpackErrors = require('./utils/formatWebpackErrors.js');
5-
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');
67

7-
// Setup error states
8-
let isHotReload = false;
9-
let hasRuntimeErrors = false;
8+
// Setup error states
9+
let isHotReload = false;
10+
let hasRuntimeErrors = false;
1011

11-
/**
12-
* Try dismissing the compile error overlay.
13-
* This will also reset runtime error records (if any),
14-
* because we have new source to evaluate.
15-
* @returns {void}
16-
*/
17-
function tryDismissErrorOverlay() {
18-
__react_refresh_error_overlay__.clearCompileError();
19-
__react_refresh_error_overlay__.clearRuntimeErrors(!hasRuntimeErrors);
20-
hasRuntimeErrors = false;
21-
}
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+
};
2223

23-
/**
24-
* A function called after a compile success signal is received from Webpack.
25-
* @returns {void}
26-
*/
27-
function handleCompileSuccess() {
28-
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;
2930

30-
if (isHotReload) {
31-
tryDismissErrorOverlay();
32-
}
33-
}
31+
if (isHotReload) {
32+
tryDismissErrorOverlay();
33+
}
34+
};
3435

35-
/**
36-
* A function called after a compile errored signal is received from Webpack.
37-
* @param {string[]} errors
38-
* @returns {void}
39-
*/
40-
function handleCompileErrors(errors) {
41-
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;
4243

43-
const formattedErrors = formatWebpackErrors(errors);
44+
const formattedErrors = formatWebpackErrors(errors);
4445

45-
// Only show the first error
46-
__react_refresh_error_overlay__.showCompileError(formattedErrors[0]);
47-
}
46+
// Only show the first error
47+
__react_refresh_error_overlay__.showCompileError(formattedErrors[0]);
48+
};
4849

49-
/**
50-
* Handles compilation messages from Webpack.
51-
* Integrates with a compile error overlay.
52-
* @param {*} message A Webpack HMR message sent via WebSockets.
53-
* @returns {void}
54-
*/
55-
function compileMessageHandler(message) {
56-
switch (message.type) {
57-
case 'ok':
58-
case 'still-ok':
59-
case 'warnings': {
60-
// TODO: Implement handling for warnings
61-
handleCompileSuccess();
62-
break;
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+
}
6372
}
64-
case 'errors': {
65-
handleCompileErrors(message.data);
66-
break;
67-
}
68-
default: {
69-
// Do nothing.
70-
}
71-
}
72-
}
73+
};
7374

74-
if (process.env.NODE_ENV !== 'production') {
75-
if (typeof window !== 'undefined') {
76-
// Only register if no other overlay have been registered
77-
if (!window.__reactRefreshOverlayInjected && __react_refresh_socket__) {
78-
// Registers handlers for compile errors with retry -
79-
// This is to prevent mismatching injection order causing errors to be thrown
80-
runWithRetry(
81-
function initSocket() {
82-
__react_refresh_socket__.init(compileMessageHandler);
83-
},
84-
3,
85-
'Failed to set up the socket connection.'
86-
);
87-
// Registers handlers for runtime errors
88-
events.handleError(function handleError(error) {
89-
hasRuntimeErrors = true;
90-
__react_refresh_error_overlay__.handleRuntimeError(error);
91-
});
92-
events.handleUnhandledRejection(function handleUnhandledPromiseRejection(error) {
93-
hasRuntimeErrors = true;
94-
__react_refresh_error_overlay__.handleRuntimeError(error);
95-
});
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);
94+
});
95+
events.handleUnhandledRejection(function handleUnhandledPromiseRejection(error) {
96+
hasRuntimeErrors = true;
97+
__react_refresh_error_overlay__.handleRuntimeError(error);
98+
});
9699

97-
// Mark overlay as injected to prevent double-injection
98-
window.__reactRefreshOverlayInjected = true;
99-
}
100+
// Mark overlay as injected to prevent double-injection
101+
window.__reactRefreshOverlayInjected = true;
100102
}
101103
}

client/ReactRefreshEntry.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* global __react_refresh_library__ */
22

3-
const safeThis = require('core-js-pure/features/global-this');
4-
const RefreshRuntime = require('react-refresh/runtime');
5-
63
if (process.env.NODE_ENV !== 'production') {
4+
const safeThis = require('core-js-pure/features/global-this');
5+
const RefreshRuntime = require('react-refresh/runtime');
6+
77
if (typeof safeThis !== 'undefined') {
88
var $RefreshInjected$ = '__reactRefreshInjected';
99
// Namespace the injected flag (if necessary) for monorepo compatibility

0 commit comments

Comments
 (0)