Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrap console calls into a check #2290

Merged
merged 1 commit into from
May 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions packages/react-dev-utils/webpackHotDevClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,11 @@ var connection = new SockJS(
// to avoid spamming the console. Disconnect usually happens
// when developer stops the server.
connection.onclose = function() {
console.info(
'The development server has disconnected.\nRefresh the page if necessary.'
);
if (typeof console !== 'undefined') {
console.info(
'The development server has disconnected.\nRefresh the page if necessary.'
);
}
};

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

function clearOutdatedErrors() {
// Clean up outdated compile errors, if any.
if (hasCompileErrors && typeof console.clear === 'function') {
console.clear();
if (typeof console !== 'undefined') {
if (hasCompileErrors && typeof console.clear === 'function') {
console.clear();
}
}
}

Expand Down Expand Up @@ -222,8 +226,10 @@ function handleWarnings(warnings) {
errors: [],
});

for (var i = 0; i < formatted.warnings.length; i++) {
console.warn(stripAnsi(formatted.warnings[i]));
if (typeof console !== 'undefined') {
for (var i = 0; i < formatted.warnings.length; i++) {
console.warn(stripAnsi(formatted.warnings[i]));
}
}
}

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

// Also log them to the console.
for (var i = 0; i < formatted.errors.length; i++) {
console.error(stripAnsi(formatted.errors[i]));
if (typeof console !== 'undefined') {
for (var i = 0; i < formatted.errors.length; i++) {
console.error(stripAnsi(formatted.errors[i]));
}
}

// Do not attempt to reload now.
Expand Down
50 changes: 28 additions & 22 deletions packages/react-error-overlay/src/effects/proxyConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,45 @@ export type { ReactFrame };
/// TODO: a more comprehensive implementation.

const registerReactStack = () => {
// $FlowFixMe
console.reactStack = frames => reactFrameStack.push(frames);
// $FlowFixMe
console.reactStackEnd = frames => reactFrameStack.pop();
if (typeof console !== 'undefined') {
// $FlowFixMe
console.reactStack = frames => reactFrameStack.push(frames);
// $FlowFixMe
console.reactStackEnd = frames => reactFrameStack.pop();
}
};

const unregisterReactStack = () => {
// $FlowFixMe
console.reactStack = undefined;
// $FlowFixMe
console.reactStackEnd = undefined;
if (typeof console !== 'undefined') {
// $FlowFixMe
console.reactStack = undefined;
// $FlowFixMe
console.reactStackEnd = undefined;
}
};

type ConsoleProxyCallback = (message: string, frames: ReactFrame[]) => void;
const permanentRegister = function proxyConsole(
type: string,
callback: ConsoleProxyCallback
) {
const orig = console[type];
console[type] = function __stack_frame_overlay_proxy_console__() {
try {
const message = arguments[0];
if (typeof message === 'string' && reactFrameStack.length > 0) {
callback(message, reactFrameStack[reactFrameStack.length - 1]);
if (typeof console !== 'undefined') {
const orig = console[type];
console[type] = function __stack_frame_overlay_proxy_console__() {
try {
const message = arguments[0];
if (typeof message === 'string' && reactFrameStack.length > 0) {
callback(message, reactFrameStack[reactFrameStack.length - 1]);
}
} catch (err) {
// Warnings must never crash. Rethrow with a clean stack.
setTimeout(function() {
throw err;
});
}
} catch (err) {
// Warnings must never crash. Rethrow with a clean stack.
setTimeout(function() {
throw err;
});
}
return orig.apply(this, arguments);
};
return orig.apply(this, arguments);
};
}
};

export { permanentRegister, registerReactStack, unregisterReactStack };