Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/happy-clubs-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/devtools-vite': minor
---

Fix devtools JSX removal logic.
88 changes: 88 additions & 0 deletions packages/devtools-vite/src/remove-devtools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,94 @@ export function DevtoolsProvider() {
null
)
}
`),
)
})

test('replaces removed devtools expressions with null', () => {
const output = removeEmptySpace(
removeDevtools(
`
import { TanStackDevtools } from '@tanstack/react-devtools'

function functionCall(value: unknown) {
return value
}

export function DevtoolsProvider() {
const devtools1 = <TanStackDevtools />
const devtools2 = functionCall(<TanStackDevtools />)
const devtools3 = true ? <TanStackDevtools /> : <>fallback</>
const devtools4 = {
devtools: <TanStackDevtools />
}
const devtools5 = (<div>
{<TanStackDevtools plugins={[]} />}
</div>)
return { devtools1, devtools2, devtools3, devtools4, devtools5 }
}
`,
'test.tsx',
)!.code,
)

expect(output).toBe(
removeEmptySpace(`
function functionCall(value: unknown) {
return value
}

export function DevtoolsProvider() {
const devtools1 = null
const devtools2 = functionCall(null)
const devtools3 = true ? null : <>fallback</>
const devtools4 = {
devtools: null
}
const devtools5 = (<div>
{null}
</div>)
return { devtools1, devtools2, devtools3, devtools4, devtools5 }
}
`),
)
})

test('removes devtools jsx children entirely', () => {
const output = removeEmptySpace(
removeDevtools(
`
import { TanStackDevtools } from '@tanstack/react-devtools'

export function DevtoolsProvider() {
return (
<>
<div>
<div>before</div>
<TanStackDevtools />
<div>after</div>
</div>
<TanStackDevtools />
</>
)
}
`,
'test.tsx',
)!.code,
)

expect(output).toBe(
removeEmptySpace(`
export function DevtoolsProvider() {
return (
<>
<div>
<div>before</div>
<div>after</div>
</div>
</>
)
}
`),
)
})
Expand Down
13 changes: 10 additions & 3 deletions packages/devtools-vite/src/remove-devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,17 @@ export function removeDevtools(code: string, id: string) {

let end = node.end
if (code[end] === '\n') end++
if (parentNode?.type === 'ParenthesizedExpression') {
s.overwrite(node.start, end, 'null')
} else {
/**
* Devtools nodes can be removed safely when nested in JSX.
* In all other contexts, replace them with `null` to avoid leaving invalid syntax.
*/
if (
parentNode?.type === 'JSXElement' ||
parentNode?.type === 'JSXFragment'
) {
s.remove(node.start, end)
} else {
s.overwrite(node.start, end, 'null')
}
})

Expand Down