mirror of
https://github.com/status-im/metro.git
synced 2025-02-26 09:45:14 +00:00
Always stub console object
Summary: In some execution environment there's no console object available. Reviewed By: davidaurelio Differential Revision: D5303105 fbshipit-source-id: 1d348640c966c9460105ea92ec8a5087ee1b75f1
This commit is contained in:
parent
30458c15ae
commit
a0fcbd7f11
@ -373,11 +373,7 @@ INSPECTOR_LEVELS[LOG_LEVELS.error] = 'error';
|
|||||||
// strip method printing to originalConsole.
|
// strip method printing to originalConsole.
|
||||||
const INSPECTOR_FRAMES_TO_SKIP = __DEV__ ? 2 : 1;
|
const INSPECTOR_FRAMES_TO_SKIP = __DEV__ ? 2 : 1;
|
||||||
|
|
||||||
function setupConsole(global) {
|
if (global.nativeLoggingHook) {
|
||||||
if (!global.nativeLoggingHook) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getNativeLogFunction(level) {
|
function getNativeLogFunction(level) {
|
||||||
return function() {
|
return function() {
|
||||||
let str;
|
let str;
|
||||||
@ -474,13 +470,7 @@ function setupConsole(global) {
|
|||||||
global.nativeLoggingHook('\n' + table.join('\n'), LOG_LEVELS.info);
|
global.nativeLoggingHook('\n' + table.join('\n'), LOG_LEVELS.info);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Preserve the original `console` as `originalConsole`
|
const originalConsole = global.console;
|
||||||
var originalConsole = global.console;
|
|
||||||
var descriptor = Object.getOwnPropertyDescriptor(global, 'console');
|
|
||||||
if (descriptor) {
|
|
||||||
Object.defineProperty(global, 'originalConsole', descriptor);
|
|
||||||
}
|
|
||||||
|
|
||||||
global.console = {
|
global.console = {
|
||||||
error: getNativeLogFunction(LOG_LEVELS.error),
|
error: getNativeLogFunction(LOG_LEVELS.error),
|
||||||
info: getNativeLogFunction(LOG_LEVELS.info),
|
info: getNativeLogFunction(LOG_LEVELS.info),
|
||||||
@ -495,8 +485,14 @@ function setupConsole(global) {
|
|||||||
// sometimes useful. Ex: on OS X, this will let you see rich output in
|
// sometimes useful. Ex: on OS X, this will let you see rich output in
|
||||||
// the Safari Web Inspector console.
|
// the Safari Web Inspector console.
|
||||||
if (__DEV__ && originalConsole) {
|
if (__DEV__ && originalConsole) {
|
||||||
|
// Preserve the original `console` as `originalConsole`
|
||||||
|
const descriptor = Object.getOwnPropertyDescriptor(global, 'console');
|
||||||
|
if (descriptor) {
|
||||||
|
Object.defineProperty(global, 'originalConsole', descriptor);
|
||||||
|
}
|
||||||
|
|
||||||
Object.keys(console).forEach(methodName => {
|
Object.keys(console).forEach(methodName => {
|
||||||
var reactNativeMethod = console[methodName];
|
const reactNativeMethod = console[methodName];
|
||||||
if (originalConsole[methodName]) {
|
if (originalConsole[methodName]) {
|
||||||
console[methodName] = function() {
|
console[methodName] = function() {
|
||||||
originalConsole[methodName](...arguments);
|
originalConsole[methodName](...arguments);
|
||||||
@ -505,10 +501,15 @@ function setupConsole(global) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
} else if (!global.console) {
|
||||||
|
function consoleLoggingStub() {};
|
||||||
if (typeof module !== 'undefined') {
|
global.console = {
|
||||||
module.exports = setupConsole;
|
error: consoleLoggingStub,
|
||||||
} else {
|
info: consoleLoggingStub,
|
||||||
setupConsole(global);
|
log: consoleLoggingStub,
|
||||||
|
warn: consoleLoggingStub,
|
||||||
|
trace: consoleLoggingStub,
|
||||||
|
debug: consoleLoggingStub,
|
||||||
|
table: consoleLoggingStub
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user