Skip to content

Commit d76c1b7

Browse files
authored
Wrap console calls into a check (#2290)
1 parent f3ff3f2 commit d76c1b7

File tree

2 files changed

+45
-31
lines changed

2 files changed

+45
-31
lines changed

packages/react-dev-utils/webpackHotDevClient.js

+17-9
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,11 @@ var connection = new SockJS(
172172
// to avoid spamming the console. Disconnect usually happens
173173
// when developer stops the server.
174174
connection.onclose = function() {
175-
console.info(
176-
'The development server has disconnected.\nRefresh the page if necessary.'
177-
);
175+
if (typeof console !== 'undefined') {
176+
console.info(
177+
'The development server has disconnected.\nRefresh the page if necessary.'
178+
);
179+
}
178180
};
179181

180182
// Remember some state related to hot module replacement.
@@ -184,8 +186,10 @@ var hasCompileErrors = false;
184186

185187
function clearOutdatedErrors() {
186188
// Clean up outdated compile errors, if any.
187-
if (hasCompileErrors && typeof console.clear === 'function') {
188-
console.clear();
189+
if (typeof console !== 'undefined') {
190+
if (hasCompileErrors && typeof console.clear === 'function') {
191+
console.clear();
192+
}
189193
}
190194
}
191195

@@ -222,8 +226,10 @@ function handleWarnings(warnings) {
222226
errors: [],
223227
});
224228

225-
for (var i = 0; i < formatted.warnings.length; i++) {
226-
console.warn(stripAnsi(formatted.warnings[i]));
229+
if (typeof console !== 'undefined') {
230+
for (var i = 0; i < formatted.warnings.length; i++) {
231+
console.warn(stripAnsi(formatted.warnings[i]));
232+
}
227233
}
228234
}
229235

@@ -260,8 +266,10 @@ function handleErrors(errors) {
260266
showErrorOverlay(formatted.errors[0]);
261267

262268
// Also log them to the console.
263-
for (var i = 0; i < formatted.errors.length; i++) {
264-
console.error(stripAnsi(formatted.errors[i]));
269+
if (typeof console !== 'undefined') {
270+
for (var i = 0; i < formatted.errors.length; i++) {
271+
console.error(stripAnsi(formatted.errors[i]));
272+
}
265273
}
266274

267275
// Do not attempt to reload now.

packages/react-error-overlay/src/effects/proxyConsole.js

+28-22
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,45 @@ export type { ReactFrame };
2626
/// TODO: a more comprehensive implementation.
2727

2828
const registerReactStack = () => {
29-
// $FlowFixMe
30-
console.reactStack = frames => reactFrameStack.push(frames);
31-
// $FlowFixMe
32-
console.reactStackEnd = frames => reactFrameStack.pop();
29+
if (typeof console !== 'undefined') {
30+
// $FlowFixMe
31+
console.reactStack = frames => reactFrameStack.push(frames);
32+
// $FlowFixMe
33+
console.reactStackEnd = frames => reactFrameStack.pop();
34+
}
3335
};
3436

3537
const unregisterReactStack = () => {
36-
// $FlowFixMe
37-
console.reactStack = undefined;
38-
// $FlowFixMe
39-
console.reactStackEnd = undefined;
38+
if (typeof console !== 'undefined') {
39+
// $FlowFixMe
40+
console.reactStack = undefined;
41+
// $FlowFixMe
42+
console.reactStackEnd = undefined;
43+
}
4044
};
4145

4246
type ConsoleProxyCallback = (message: string, frames: ReactFrame[]) => void;
4347
const permanentRegister = function proxyConsole(
4448
type: string,
4549
callback: ConsoleProxyCallback
4650
) {
47-
const orig = console[type];
48-
console[type] = function __stack_frame_overlay_proxy_console__() {
49-
try {
50-
const message = arguments[0];
51-
if (typeof message === 'string' && reactFrameStack.length > 0) {
52-
callback(message, reactFrameStack[reactFrameStack.length - 1]);
51+
if (typeof console !== 'undefined') {
52+
const orig = console[type];
53+
console[type] = function __stack_frame_overlay_proxy_console__() {
54+
try {
55+
const message = arguments[0];
56+
if (typeof message === 'string' && reactFrameStack.length > 0) {
57+
callback(message, reactFrameStack[reactFrameStack.length - 1]);
58+
}
59+
} catch (err) {
60+
// Warnings must never crash. Rethrow with a clean stack.
61+
setTimeout(function() {
62+
throw err;
63+
});
5364
}
54-
} catch (err) {
55-
// Warnings must never crash. Rethrow with a clean stack.
56-
setTimeout(function() {
57-
throw err;
58-
});
59-
}
60-
return orig.apply(this, arguments);
61-
};
65+
return orig.apply(this, arguments);
66+
};
67+
}
6268
};
6369

6470
export { permanentRegister, registerReactStack, unregisterReactStack };

0 commit comments

Comments
 (0)