mirror of
https://github.com/zhigang1992/create-react-app.git
synced 2026-04-29 09:55:43 +08:00
Wrap console calls into a check (#2290)
This commit is contained in:
26
packages/react-dev-utils/webpackHotDevClient.js
vendored
26
packages/react-dev-utils/webpackHotDevClient.js
vendored
@@ -172,9 +172,11 @@ var connection = new SockJS(
|
|||||||
// to avoid spamming the console. Disconnect usually happens
|
// to avoid spamming the console. Disconnect usually happens
|
||||||
// when developer stops the server.
|
// when developer stops the server.
|
||||||
connection.onclose = function() {
|
connection.onclose = function() {
|
||||||
console.info(
|
if (typeof console !== 'undefined') {
|
||||||
'The development server has disconnected.\nRefresh the page if necessary.'
|
console.info(
|
||||||
);
|
'The development server has disconnected.\nRefresh the page if necessary.'
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Remember some state related to hot module replacement.
|
// Remember some state related to hot module replacement.
|
||||||
@@ -184,8 +186,10 @@ var hasCompileErrors = false;
|
|||||||
|
|
||||||
function clearOutdatedErrors() {
|
function clearOutdatedErrors() {
|
||||||
// Clean up outdated compile errors, if any.
|
// Clean up outdated compile errors, if any.
|
||||||
if (hasCompileErrors && typeof console.clear === 'function') {
|
if (typeof console !== 'undefined') {
|
||||||
console.clear();
|
if (hasCompileErrors && typeof console.clear === 'function') {
|
||||||
|
console.clear();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,8 +226,10 @@ function handleWarnings(warnings) {
|
|||||||
errors: [],
|
errors: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
for (var i = 0; i < formatted.warnings.length; i++) {
|
if (typeof console !== 'undefined') {
|
||||||
console.warn(stripAnsi(formatted.warnings[i]));
|
for (var i = 0; i < formatted.warnings.length; i++) {
|
||||||
|
console.warn(stripAnsi(formatted.warnings[i]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,8 +266,10 @@ function handleErrors(errors) {
|
|||||||
showErrorOverlay(formatted.errors[0]);
|
showErrorOverlay(formatted.errors[0]);
|
||||||
|
|
||||||
// Also log them to the console.
|
// Also log them to the console.
|
||||||
for (var i = 0; i < formatted.errors.length; i++) {
|
if (typeof console !== 'undefined') {
|
||||||
console.error(stripAnsi(formatted.errors[i]));
|
for (var i = 0; i < formatted.errors.length; i++) {
|
||||||
|
console.error(stripAnsi(formatted.errors[i]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do not attempt to reload now.
|
// Do not attempt to reload now.
|
||||||
|
|||||||
@@ -26,17 +26,21 @@ export type { ReactFrame };
|
|||||||
/// TODO: a more comprehensive implementation.
|
/// TODO: a more comprehensive implementation.
|
||||||
|
|
||||||
const registerReactStack = () => {
|
const registerReactStack = () => {
|
||||||
// $FlowFixMe
|
if (typeof console !== 'undefined') {
|
||||||
console.reactStack = frames => reactFrameStack.push(frames);
|
// $FlowFixMe
|
||||||
// $FlowFixMe
|
console.reactStack = frames => reactFrameStack.push(frames);
|
||||||
console.reactStackEnd = frames => reactFrameStack.pop();
|
// $FlowFixMe
|
||||||
|
console.reactStackEnd = frames => reactFrameStack.pop();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const unregisterReactStack = () => {
|
const unregisterReactStack = () => {
|
||||||
// $FlowFixMe
|
if (typeof console !== 'undefined') {
|
||||||
console.reactStack = undefined;
|
// $FlowFixMe
|
||||||
// $FlowFixMe
|
console.reactStack = undefined;
|
||||||
console.reactStackEnd = undefined;
|
// $FlowFixMe
|
||||||
|
console.reactStackEnd = undefined;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
type ConsoleProxyCallback = (message: string, frames: ReactFrame[]) => void;
|
type ConsoleProxyCallback = (message: string, frames: ReactFrame[]) => void;
|
||||||
@@ -44,21 +48,23 @@ const permanentRegister = function proxyConsole(
|
|||||||
type: string,
|
type: string,
|
||||||
callback: ConsoleProxyCallback
|
callback: ConsoleProxyCallback
|
||||||
) {
|
) {
|
||||||
const orig = console[type];
|
if (typeof console !== 'undefined') {
|
||||||
console[type] = function __stack_frame_overlay_proxy_console__() {
|
const orig = console[type];
|
||||||
try {
|
console[type] = function __stack_frame_overlay_proxy_console__() {
|
||||||
const message = arguments[0];
|
try {
|
||||||
if (typeof message === 'string' && reactFrameStack.length > 0) {
|
const message = arguments[0];
|
||||||
callback(message, reactFrameStack[reactFrameStack.length - 1]);
|
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) {
|
return orig.apply(this, arguments);
|
||||||
// Warnings must never crash. Rethrow with a clean stack.
|
};
|
||||||
setTimeout(function() {
|
}
|
||||||
throw err;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return orig.apply(this, arguments);
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export { permanentRegister, registerReactStack, unregisterReactStack };
|
export { permanentRegister, registerReactStack, unregisterReactStack };
|
||||||
|
|||||||
Reference in New Issue
Block a user