Skip to content

Commit d4d039f

Browse files
committed
Check for server enviroment in enhanceConsoleLog
- This change is made so log coloring can work in both Server and Browser - It will use ANSI for Server and CSS for Browser
1 parent bbcc482 commit d4d039f

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

packages/devtools-vite/src/enhance-logs.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ const transform = (
3232
location.start.column,
3333
]
3434
const finalPath = `${filePath}:${lineNumber}:${column + 1}`
35+
const logMessage = `${chalk.magenta('LOG')} ${chalk.blueBright(`${finalPath} - http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(finalPath)}`)}\n → `
3536
// Prefer ANSI escape codes for formatting.
3637
if (!cssFormatting) {
37-
path.node.arguments.unshift(
38-
t.stringLiteral(
39-
`${chalk.magenta('LOG')} ${chalk.blueBright(`${finalPath} - http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(finalPath)}`)}\n → `,
40-
),
41-
)
38+
path.node.arguments.unshift(t.stringLiteral(logMessage))
4239
} else {
43-
path.node.arguments.unshift(
40+
const serverLogMessage = t.arrayExpression([
41+
t.stringLiteral(logMessage),
42+
])
43+
const browserLogMessage = t.arrayExpression([
4444
// LOG with css formatting specifiers: %c
4545
t.stringLiteral(
4646
`%c${'LOG'}%c %c${`${finalPath} - http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(finalPath)}`}%c \n → `,
@@ -51,6 +51,27 @@ const transform = (
5151
// blueBright
5252
t.stringLiteral('color:#55F'),
5353
t.stringLiteral('color:#FFF'),
54+
])
55+
56+
// globalThis.window == undefined
57+
const checkServerCondition = t.binaryExpression(
58+
'==',
59+
t.memberExpression(
60+
t.identifier('globalThis'),
61+
t.identifier('window'),
62+
),
63+
t.identifier('undefined'),
64+
)
65+
66+
// ...(isServer ? checkServerCondition : browserLogMessage)
67+
path.node.arguments.unshift(
68+
t.spreadElement(
69+
t.conditionalExpression(
70+
checkServerCondition,
71+
serverLogMessage,
72+
browserLogMessage,
73+
),
74+
),
5475
)
5576
}
5677
didTransform = true

0 commit comments

Comments
 (0)