Skip to content

Commit 17c823b

Browse files
fix: add css format specifier support to enhancedConsoleLog (#300)
* Added CSS Formatting to Enhanced Logs * Fixed Failing Enhanced Logs Tests * Added CSS Formatting Test to Enhanced Console Logs * Added @tanstack/devtools-vite Changeset * 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 * fix: make the console logs always conditionally render --------- Co-authored-by: Alem Tuzlak <[email protected]>
1 parent cbe2d05 commit 17c823b

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

.changeset/sad-weeks-pay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/devtools-vite': patch
3+
---
4+
5+
Added css format specifier support to enhancedLogs

examples/react/start/src/routes/__root.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const Route = createRootRoute({
3333
})
3434

3535
function RootDocument({ children }: { children: React.ReactNode }) {
36+
console.log('Rendering Root Document')
3637
return (
3738
<html lang="en">
3839
<head>

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ describe('remove-devtools', () => {
9494
3000,
9595
)!.code,
9696
)
97-
console.log('output', output)
9897
expect(
9998
output.includes(
10099
'http://localhost:3000/__tsd/open-source?source=test.jsx',
@@ -114,4 +113,20 @@ describe('remove-devtools', () => {
114113
)
115114
expect(output).toBe(undefined)
116115
})
116+
117+
test('it adds enhanced console.log with css formatting to console.log()', () => {
118+
const output = removeEmptySpace(
119+
enhanceConsoleLog(
120+
`
121+
console.log('This is a log')
122+
`,
123+
'test.jsx',
124+
3000,
125+
)!.code,
126+
)
127+
expect(output.includes('color:#A0A')).toEqual(true)
128+
expect(output.includes('color:#FFF')).toEqual(true)
129+
expect(output.includes('color:#55F')).toEqual(true)
130+
expect(output.includes('color:#FFF')).toEqual(true)
131+
})
117132
})

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,42 @@ const transform = (
3131
location.start.column,
3232
]
3333
const finalPath = `${filePath}:${lineNumber}:${column + 1}`
34-
path.node.arguments.unshift(
34+
const logMessage = `${chalk.magenta('LOG')} ${chalk.blueBright(`${finalPath}`)}\n → `
35+
36+
const serverLogMessage = t.arrayExpression([
37+
t.stringLiteral(logMessage),
38+
])
39+
const browserLogMessage = t.arrayExpression([
40+
// LOG with css formatting specifiers: %c
3541
t.stringLiteral(
36-
`${chalk.magenta('LOG')} ${chalk.blueBright(`${finalPath} - http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(finalPath)}`)}\n → `,
42+
`%c${'LOG'}%c %c${`Go to Source: http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(finalPath)}`}%c \n → `,
43+
),
44+
// magenta
45+
t.stringLiteral('color:#A0A'),
46+
t.stringLiteral('color:#FFF'),
47+
// blueBright
48+
t.stringLiteral('color:#55F'),
49+
t.stringLiteral('color:#FFF'),
50+
])
51+
52+
// typeof window === "undefined"
53+
const checkServerCondition = t.binaryExpression(
54+
'===',
55+
t.unaryExpression('typeof', t.identifier('window')),
56+
t.stringLiteral('undefined'),
57+
)
58+
59+
// ...(isServer ? serverLogMessage : browserLogMessage)
60+
path.node.arguments.unshift(
61+
t.spreadElement(
62+
t.conditionalExpression(
63+
checkServerCondition,
64+
serverLogMessage,
65+
browserLogMessage,
66+
),
3767
),
3868
)
69+
3970
didTransform = true
4071
}
4172
},

0 commit comments

Comments
 (0)