-
Notifications
You must be signed in to change notification settings - Fork 48.3k
/
Copy pathReactClientConsoleConfigServer.js
73 lines (67 loc) · 1.88 KB
/
ReactClientConsoleConfigServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
// This flips color using ANSI, then sets a color styling, then resets.
const badgeFormat = '\x1b[0m\x1b[7m%c%s\x1b[0m%c ';
// Same badge styling as DevTools.
const badgeStyle =
// We use a fixed background if light-dark is not supported, otherwise
// we use a transparent background.
'background: #e6e6e6;' +
'background: light-dark(rgba(0,0,0,0.1), rgba(255,255,255,0.25));' +
'color: #000000;' +
'color: light-dark(#000000, #ffffff);' +
'border-radius: 2px';
const resetStyle = '';
const pad = ' ';
const bind = Function.prototype.bind;
export function bindToConsole(
methodName: string,
args: Array<any>,
badgeName: string,
): () => any {
let offset = 0;
switch (methodName) {
case 'dir':
case 'dirxml':
case 'groupEnd':
case 'table': {
// These methods cannot be colorized because they don't take a formatting string.
// $FlowFixMe
return bind.apply(console[methodName], [console].concat(args)); // eslint-disable-line react-internal/no-production-logging
}
case 'assert': {
// assert takes formatting options as the second argument.
offset = 1;
}
}
const newArgs = args.slice(0);
if (typeof newArgs[offset] === 'string') {
newArgs.splice(
offset,
1,
badgeFormat + newArgs[offset],
badgeStyle,
pad + badgeName + pad,
resetStyle,
);
} else {
newArgs.splice(
offset,
0,
badgeFormat,
badgeStyle,
pad + badgeName + pad,
resetStyle,
);
}
// The "this" binding in the "bind";
newArgs.unshift(console);
// $FlowFixMe
return bind.apply(console[methodName], newArgs); // eslint-disable-line react-internal/no-production-logging
}