File size: 4,985 Bytes
1e92f2d |
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
import { dim } from '../../lib/picocolors'
import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external'
type InterceptableConsoleMethod =
| 'error'
| 'assert'
| 'debug'
| 'dir'
| 'dirxml'
| 'group'
| 'groupCollapsed'
| 'groupEnd'
| 'info'
| 'log'
| 'table'
| 'trace'
| 'warn'
// This function could be used from multiple places, including hook.
// Skips CSS and object arguments, inlines other in the first argument as a template string
/**
* 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.
*
* @source https://github.com/facebook/react/blob/b44a99bf58d69d52b5288d9eadcc6d226d705e11/packages/react-devtools-shared/src/backend/utils/formatConsoleArguments.js#L14
*/
function formatConsoleArguments(maybeMessage: any, ...inputArgs: any[]): any[] {
if (inputArgs.length === 0 || typeof maybeMessage !== 'string') {
return [maybeMessage, ...inputArgs]
}
const args = inputArgs.slice()
let template = ''
let argumentsPointer = 0
for (let i = 0; i < maybeMessage.length; ++i) {
const currentChar = maybeMessage[i]
if (currentChar !== '%') {
template += currentChar
continue
}
const nextChar = maybeMessage[i + 1]
++i
// Only keep CSS and objects, inline other arguments
switch (nextChar) {
case 'c':
case 'O':
case 'o': {
++argumentsPointer
template += `%${nextChar}`
break
}
case 'd':
case 'i': {
const [arg] = args.splice(argumentsPointer, 1)
template += parseInt(arg, 10).toString()
break
}
case 'f': {
const [arg] = args.splice(argumentsPointer, 1)
template += parseFloat(arg).toString()
break
}
case 's': {
const [arg] = args.splice(argumentsPointer, 1)
template += String(arg)
break
}
default:
template += `%${nextChar}`
}
}
return [template, ...args]
}
const isColorSupported = dim('test') !== 'test'
// TODO: Breaks when complex objects are logged
// dim("%s") does not work in Chrome
const ANSI_STYLE_DIMMING_TEMPLATE = isColorSupported
? '\x1b[2;38;2;124;124;124m%s\x1b[0m'
: '%s'
function dimConsoleCall(
methodName: InterceptableConsoleMethod,
args: any[]
): any[] {
switch (methodName) {
case 'dir':
case 'dirxml':
case 'group':
case 'groupCollapsed':
case 'groupEnd':
case 'table': {
// These methods cannot be colorized because they don't take a formatting string.
return args
}
case 'assert': {
// assert takes formatting options as the second argument.
return [args[0]].concat(
ANSI_STYLE_DIMMING_TEMPLATE,
...formatConsoleArguments(args[1], ...args.slice(2))
)
}
case 'error':
case 'debug':
case 'info':
case 'log':
case 'trace':
case 'warn':
return [ANSI_STYLE_DIMMING_TEMPLATE].concat(
...formatConsoleArguments(args[0], ...args.slice(1))
)
default:
return methodName satisfies never
}
}
// Based on https://github.com/facebook/react/blob/28dc0776be2e1370fe217549d32aee2519f0cf05/packages/react-server/src/ReactFlightServer.js#L248
function patchConsoleMethodDEV(methodName: InterceptableConsoleMethod): void {
const descriptor = Object.getOwnPropertyDescriptor(console, methodName)
if (
descriptor &&
(descriptor.configurable || descriptor.writable) &&
typeof descriptor.value === 'function'
) {
const originalMethod = descriptor.value
const originalName = Object.getOwnPropertyDescriptor(originalMethod, 'name')
const wrapperMethod = function (this: typeof console, ...args: any[]) {
const workUnitStore = workUnitAsyncStorage.getStore()
switch (workUnitStore?.type) {
case 'prerender':
case 'prerender-client':
originalMethod.apply(this, dimConsoleCall(methodName, args))
break
case 'prerender-ppr':
case 'prerender-legacy':
case 'request':
case 'cache':
case 'private-cache':
case 'unstable-cache':
case undefined:
originalMethod.apply(this, args)
break
default:
workUnitStore satisfies never
}
}
if (originalName) {
Object.defineProperty(wrapperMethod, 'name', originalName)
}
Object.defineProperty(console, methodName, {
value: wrapperMethod,
})
}
}
patchConsoleMethodDEV('error')
patchConsoleMethodDEV('assert')
patchConsoleMethodDEV('debug')
patchConsoleMethodDEV('dir')
patchConsoleMethodDEV('dirxml')
patchConsoleMethodDEV('group')
patchConsoleMethodDEV('groupCollapsed')
patchConsoleMethodDEV('groupEnd')
patchConsoleMethodDEV('info')
patchConsoleMethodDEV('log')
patchConsoleMethodDEV('table')
patchConsoleMethodDEV('trace')
patchConsoleMethodDEV('warn')
|