From 2fb9735a8b7d8b5787a2de34295d761dd9ecee90 Mon Sep 17 00:00:00 2001 From: Charlie Cheever Date: Wed, 8 Apr 2015 01:02:55 -0700 Subject: [PATCH 1/2] Also call the original `console` methods if they exist Ex. When `console.log` or similar is called, this will call the original method, which can be quite useful. For example, under iOS, this will log to the Safari console debugger, which has an expandable UI for inspecting objects, etc., and is also just useful if you are using that as a REPL. I don't believe this incurs a meaningful performance penalty unless the console is open, but it would be easy to stick behind a flag if that is a problem. A slightly different version of this patch was submitted a while ago and merged but something got screwed up with the merge and this change was not actually pulled in. (See the weird unrelated commits and changes attached to the PR #205) --- .../haste/polyfills/console.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packager/react-packager/src/DependencyResolver/haste/polyfills/console.js b/packager/react-packager/src/DependencyResolver/haste/polyfills/console.js index 91fb970f885521..83af93303802c6 100644 --- a/packager/react-packager/src/DependencyResolver/haste/polyfills/console.js +++ b/packager/react-packager/src/DependencyResolver/haste/polyfills/console.js @@ -26,6 +26,8 @@ error: 4 }; + var originalConsole = global.console; + function setupConsole(global) { if (!global.nativeLoggingHook) { @@ -135,6 +137,24 @@ table: consoleTablePolyfill }; + // If available, also call the original `console` method since that is sometimes useful + // Ex. On OS X, this will let you see rich output to the Safari REPL console + if (originalConsole) { + for (var methodName in global.console) { + if (global.console.hasOwnProperty(methodName)) { + if (originalConsole[methodName]) { + (function () { + var nativeMethod = global.console[methodName]; + global.console[methodName] = function () { + originalConsole[methodName].apply(originalConsole, arguments); + nativeMethod.apply(global.console, arguments); + }; + })(); + } + } + } + } + } if (typeof module !== 'undefined') { From d50c2c17a7dfadb8af8373a4a7851a5cf887817d Mon Sep 17 00:00:00 2001 From: Charlie Cheever Date: Wed, 8 Apr 2015 01:17:36 -0700 Subject: [PATCH 2/2] Fixing a small bug and simplifying code in #742, calling original console methods - Fixed a small bug related to variable capture - Made code more concise and hopefully clearer and more readbale - Added a comment about what "native" refers to --- .../haste/polyfills/console.js | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/packager/react-packager/src/DependencyResolver/haste/polyfills/console.js b/packager/react-packager/src/DependencyResolver/haste/polyfills/console.js index 83af93303802c6..03dc60e24c360c 100644 --- a/packager/react-packager/src/DependencyResolver/haste/polyfills/console.js +++ b/packager/react-packager/src/DependencyResolver/haste/polyfills/console.js @@ -26,10 +26,11 @@ error: 4 }; - var originalConsole = global.console; function setupConsole(global) { + var originalConsole = global.console; + if (!global.nativeLoggingHook) { return; } @@ -140,19 +141,13 @@ // If available, also call the original `console` method since that is sometimes useful // Ex. On OS X, this will let you see rich output to the Safari REPL console if (originalConsole) { - for (var methodName in global.console) { - if (global.console.hasOwnProperty(methodName)) { - if (originalConsole[methodName]) { - (function () { - var nativeMethod = global.console[methodName]; - global.console[methodName] = function () { - originalConsole[methodName].apply(originalConsole, arguments); - nativeMethod.apply(global.console, arguments); - }; - })(); - } - } - } + Object.keys(global.console).forEach((methodName) => { + var nativeMethod = global.console[methodName]; + global.console[methodName] = function () { + originalConsole[methodName].apply(originalConsole, arguments); + nativeMethod.apply(global.console, arguments); + }; + }); } }