Files
react/packages/react-devtools-shell/src/app/console.js
Luna Ruan 5b57bc6e31 [Draft] don't patch console during first render (#22308)
Previously, DevTools always overrode the native console to dim or supress StrictMode double logging. It also overrode console.log (in addition to console.error and console.warn). However, this changes the location shown by the browser console, which causes a bad developer experience. There is currently a TC39 proposal that would allow us to extend console without breaking developer experience, but in the meantime this PR changes the StrictMode console override behavior so that we only patch the console during the StrictMode double render so that, during the first render, the location points to developer code rather than our DevTools console code.
2021-09-21 15:00:11 -07:00

43 lines
1.2 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
function ignoreStrings(
methodName: string,
stringsToIgnore: Array<string>,
): void {
console[methodName] = (...args) => {
const maybeString = args[0];
if (typeof maybeString === 'string') {
for (let i = 0; i < stringsToIgnore.length; i++) {
if (maybeString.startsWith(stringsToIgnore[i])) {
return;
}
}
}
// HACKY In the test harness, DevTools overrides the parent window's console.
// Our test app code uses the iframe's console though.
// To simulate a more accurate end-to-end environment,
// the shell's console patching should pass through to the parent override methods.
window.parent.console[methodName](...args);
};
}
export function ignoreErrors(errorsToIgnore: Array<string>): void {
ignoreStrings('error', errorsToIgnore);
}
export function ignoreWarnings(warningsToIgnore: Array<string>): void {
ignoreStrings('warn', warningsToIgnore);
}
export function ignoreLogs(logsToIgnore: Array<string>): void {
ignoreStrings('log', logsToIgnore);
}