Skip to content

Commit 1798897

Browse files
cpojerfacebook-github-bot
authored andcommitted
Fall back to JSON.stringify in console.log if Symbol is unavailable
Summary: Symbol is not available in older versions of JSON resulting in crashes in `prettyFormat` because we are using a clowny transform. Reviewed By: sebmck Differential Revision: D16501208 fbshipit-source-id: 9952bf4993ae05335707cd386f9aa4bbc14b7564
1 parent d550256 commit 1798897

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

Libraries/Utilities/HMRClient.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
const Platform = require('./Platform');
1313
const invariant = require('invariant');
14-
const prettyFormat = require('pretty-format');
1514

1615
const MetroHMRClient = require('metro/src/lib/bundle-modules/HMRClient');
1716

@@ -104,23 +103,36 @@ const HMRClient: HMRClientNativeInterface = {
104103
log(level: LogLevel, data: Array<mixed>) {
105104
try {
106105
if (hmrClient) {
107-
hmrClient.send(
108-
JSON.stringify({
106+
let message;
107+
if (global.Symbol) {
108+
message = JSON.stringify({
109109
type: 'log',
110110
level,
111-
data: data.map(message =>
112-
typeof message === 'string'
113-
? message
114-
: prettyFormat(message, {
111+
data: data.map(item =>
112+
typeof item === 'string'
113+
? item
114+
: require('pretty-format')(item, {
115115
escapeString: true,
116116
highlight: true,
117117
maxDepth: 3,
118118
min: true,
119-
plugins: [prettyFormat.plugins.ReactElement],
119+
plugins: [require('pretty-format').plugins.ReactElement],
120120
}),
121121
),
122-
}),
123-
);
122+
});
123+
} else {
124+
try {
125+
message = JSON.stringify({type: 'log', level, data});
126+
} catch (error) {
127+
message = JSON.stringify({
128+
type: 'log',
129+
level,
130+
data: [error.message],
131+
});
132+
}
133+
}
134+
135+
hmrClient.send(message);
124136
}
125137
} catch (error) {
126138
// If sending logs causes any failures we want to silently ignore them

0 commit comments

Comments
 (0)