|
12 | 12 |
|
13 | 13 | import Platform from '../Utilities/Platform';
|
14 | 14 |
|
| 15 | +declare var console: typeof console & { |
| 16 | + _isPolyfilled: boolean, |
| 17 | +}; |
| 18 | + |
15 | 19 | /**
|
16 | 20 | * Sets up developer tools for React Native.
|
17 | 21 | * You can use this module directly, or just require InitializeCore.
|
@@ -60,25 +64,54 @@ if (__DEV__) {
|
60 | 64 | JSInspector.registerAgent(require('../JSInspector/NetworkAgent'));
|
61 | 65 | }
|
62 | 66 |
|
| 67 | + // Note we can't check if console is "native" because it would appear "native" in JSC and Hermes. |
| 68 | + // We also can't check any properties that don't exist in the Chrome worker environment. |
| 69 | + // So we check a navigator property that's set to a particular value ("Netscape") in all real browsers. |
| 70 | + const isLikelyARealBrowser = |
| 71 | + global.navigator != null && |
| 72 | + /* _ |
| 73 | + * | | |
| 74 | + * _ __ ___| |_ ___ ___ __ _ _ __ ___ |
| 75 | + * | '_ \ / _ \ __/ __|/ __/ _` | '_ \ / _ \ |
| 76 | + * | | | | __/ |_\__ \ (_| (_| | |_) | __/ |
| 77 | + * |_| |_|\___|\__|___/\___\__,_| .__/ \___| |
| 78 | + * | | |
| 79 | + * |_| |
| 80 | + */ |
| 81 | + global.navigator.appName === 'Netscape'; // Any real browser |
| 82 | + |
63 | 83 | if (!Platform.isTesting) {
|
64 | 84 | const HMRClient = require('../Utilities/HMRClient');
|
65 |
| - [ |
66 |
| - 'trace', |
67 |
| - 'info', |
68 |
| - 'warn', |
69 |
| - 'log', |
70 |
| - 'group', |
71 |
| - 'groupCollapsed', |
72 |
| - 'groupEnd', |
73 |
| - 'debug', |
74 |
| - ].forEach(level => { |
75 |
| - const originalFunction = console[level]; |
76 |
| - // $FlowFixMe Overwrite console methods |
77 |
| - console[level] = function(...args) { |
78 |
| - HMRClient.log(level, args); |
79 |
| - originalFunction.apply(console, args); |
80 |
| - }; |
81 |
| - }); |
| 85 | + |
| 86 | + if (console._isPolyfilled) { |
| 87 | + // We assume full control over the console and send JavaScript logs to Metro. |
| 88 | + [ |
| 89 | + 'trace', |
| 90 | + 'info', |
| 91 | + 'warn', |
| 92 | + 'log', |
| 93 | + 'group', |
| 94 | + 'groupCollapsed', |
| 95 | + 'groupEnd', |
| 96 | + 'debug', |
| 97 | + ].forEach(level => { |
| 98 | + const originalFunction = console[level]; |
| 99 | + // $FlowFixMe Overwrite console methods |
| 100 | + console[level] = function(...args) { |
| 101 | + HMRClient.log(level, args); |
| 102 | + originalFunction.apply(console, args); |
| 103 | + }; |
| 104 | + }); |
| 105 | + } else { |
| 106 | + // We assume the environment has a real rich console (like Chrome), and don't hijack it to log to Metro. |
| 107 | + // It's likely the developer is using rich console to debug anyway, and hijacking it would |
| 108 | + // lose the filenames in console.log calls: https://github.com/facebook/react-native/issues/26788. |
| 109 | + HMRClient.log('log', [ |
| 110 | + `JavaScript logs will appear in your ${ |
| 111 | + isLikelyARealBrowser ? 'browser' : 'environment' |
| 112 | + } console`, |
| 113 | + ]); |
| 114 | + } |
82 | 115 | }
|
83 | 116 |
|
84 | 117 | require('./setUpReactRefresh');
|
|
0 commit comments