Skip to content

Commit 856f02b

Browse files
committed
fix(devtools-vite): emit correct source positions for enhanced logs
console-pipe-transform prepends a multi-line IIFE to root entry files before better-console-logs runs. Because both plugins are enforce:'pre' and execute in array order, enhanceConsoleLog was parsing the post-prepend source and reporting line numbers shifted past the end of the user's file -- 'Go to Source' links pointed nowhere. Swap the two plugin entries so enhanceConsoleLog sees the original source. Add a regression test asserting better-console-logs is registered before console-pipe-transform.
1 parent e5041c0 commit 856f02b

2 files changed

Lines changed: 38 additions & 20 deletions

File tree

packages/devtools-vite/src/plugin.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,30 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
540540
}
541541
},
542542
},
543+
// Enhanced logs must run BEFORE console-pipe-transform: the latter
544+
// prepends a multi-line IIFE to root entry files, which would shift
545+
// every line number computed by the oxc-parser AST and produce
546+
// "Go to Source" links pointing past the end of the user's file.
547+
{
548+
name: '@tanstack/devtools:better-console-logs',
549+
enforce: 'pre',
550+
apply(config) {
551+
return config.mode === 'development' && enhancedLogsConfig.enabled
552+
},
553+
transform: {
554+
filter: {
555+
id: {
556+
exclude: [/node_modules/, /\?raw/, /\/dist\//, /\/build\//],
557+
},
558+
code: {
559+
include: 'console.',
560+
},
561+
},
562+
handler(code, id) {
563+
return enhanceConsoleLog(code, id, port)
564+
},
565+
},
566+
},
543567
// Inject console piping code into entry files (both client and server)
544568
{
545569
name: '@tanstack/devtools:console-pipe-transform',
@@ -586,26 +610,6 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
586610
},
587611
},
588612
},
589-
{
590-
name: '@tanstack/devtools:better-console-logs',
591-
enforce: 'pre',
592-
apply(config) {
593-
return config.mode === 'development' && enhancedLogsConfig.enabled
594-
},
595-
transform: {
596-
filter: {
597-
id: {
598-
exclude: [/node_modules/, /\?raw/, /\/dist\//, /\/build\//],
599-
},
600-
code: {
601-
include: 'console.',
602-
},
603-
},
604-
handler(code, id) {
605-
return enhanceConsoleLog(code, id, port)
606-
},
607-
},
608-
},
609613
{
610614
name: '@tanstack/devtools:inject-plugin',
611615
apply(config, { command }) {

packages/devtools-vite/tests/index.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,5 +420,19 @@ describe('devtools plugin', () => {
420420
)
421421
expect(plugin).toBeDefined()
422422
})
423+
424+
it('runs better-console-logs before console-pipe-transform', () => {
425+
// Both plugins are enforce: 'pre' and run in array order. console-pipe
426+
// prepends a multi-line IIFE to root entry files; if better-console-logs
427+
// ran after, its AST line numbers would be shifted past the end of the
428+
// user's file and "Go to Source" links would miss the actual source.
429+
const plugins = devtools()
430+
const names = plugins.map((p) => p.name)
431+
const betterLogsIdx = names.indexOf('@tanstack/devtools:better-console-logs')
432+
const pipeIdx = names.indexOf('@tanstack/devtools:console-pipe-transform')
433+
expect(betterLogsIdx).toBeGreaterThanOrEqual(0)
434+
expect(pipeIdx).toBeGreaterThanOrEqual(0)
435+
expect(betterLogsIdx).toBeLessThan(pipeIdx)
436+
})
423437
})
424438
})

0 commit comments

Comments
 (0)