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
7 changes: 7 additions & 0 deletions .changeset/remove-devtools-plugin-factory-imports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@tanstack/devtools-bundler-core': patch
---

Remove imports of plugins used exclusively in the devtools `plugins` array (for example `plugins={[hotkeysDevtoolsPlugin()]}`) when stripping devtools from production builds. Preserve plugin imports still used by application code and retain the original import kinds in mixed declarations.

A `walk()` visitor can return `false` to skip the current node’s children. Other return values allow traversal to continue.
6 changes: 3 additions & 3 deletions packages/devtools-bundler-core/src/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ export function forEachChild(node: Node, callback: (child: Node) => void) {
}

/**
* Recursively walk AST nodes, calling `visitor` for each node with a `type`.
* Recursively walk AST nodes. Return false from the visitor to skip children.
*/
export function walk(
node: Node,
visitor: (node: Node, parentNode?: Node) => void,
visitor: (node: Node, parentNode?: Node) => unknown,
parentNode?: Node,
) {
visitor(node, parentNode)
if (visitor(node, parentNode) === false) return
forEachChild(node, (child) => walk(child, visitor, node))
}
86 changes: 86 additions & 0 deletions packages/devtools-bundler-core/src/remove-devtools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,92 @@ export default function DevtoolsExample() {
)
})

test('it removes the import when a plugin factory is called directly in the plugins array', () => {
const output = removeEmptySpace(
removeDevtools(
`
import { TanStackDevtools } from '@tanstack/react-devtools'
import { hotkeysDevtoolsPlugin } from '@tanstack/react-hotkeys-devtools'

export const App = () => (
<TanStackDevtools plugins={[hotkeysDevtoolsPlugin()]} />
)
`,
'test.tsx',
)!.code,
)

expect(output).toBe(
removeEmptySpace(`
export const App = () => (null)
`),
)
})

test('it removes the import when a plugin identifier is used directly in the plugins array', () => {
const output = removeEmptySpace(
removeDevtools(
`
import { TanStackDevtools } from '@tanstack/react-devtools'
import { myPlugin } from './my-plugin'

export const App = () => (
<TanStackDevtools plugins={[myPlugin]} />
)
`,
'test.tsx',
)!.code,
)

expect(output).toBe(
removeEmptySpace(`
export const App = () => (null)
`),
)
})

test('retains plugin imports referenced by an export or a call outside devtools', () => {
const output = removeDevtools(
`
import { TanStackDevtools } from '@tanstack/react-devtools'
import { exportedPlugin, createPlugin, removedPlugin } from './plugins'

export { exportedPlugin }
export const standalone = createPlugin()
export const App = () => (
<TanStackDevtools plugins={[exportedPlugin, createPlugin(), removedPlugin]} />
)
`,
'test.tsx',
)

expect(output?.code).toContain(
"import { exportedPlugin, createPlugin } from './plugins'",
)
expect(output?.code).not.toContain('removedPlugin')
})

test('preserves a default specifier when removing a named plugin', () => {
const output = removeDevtools(
`
import { TanStackDevtools } from '@tanstack/react-devtools'
import Widget, { myPlugin } from './plugins'

export const App = () => (
<>
<Widget />
<TanStackDevtools plugins={[myPlugin]} />
</>
)
`,
'test.tsx',
)

expect(output?.code).toContain("import Widget from './plugins'")
expect(output?.code).not.toContain('myPlugin')
expect(output?.code).toContain('<Widget />')
})

test('it removes the import completely even if used as a function instead of jsx', () => {
const output = removeEmptySpace(
removeDevtools(
Expand Down
54 changes: 44 additions & 10 deletions packages/devtools-bundler-core/src/remove-devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import MagicString from 'magic-string'
import { parseSync } from 'oxc-parser'
import { walk } from './ast-utils'
import { isTanStackDevtoolsImport } from './devtools-packages'
import type { JSXOpeningElement } from 'oxc-parser'
import type { JSXElement, JSXOpeningElement } from 'oxc-parser'

/**
* Extract component names referenced in the `plugins` prop of a devtools element.
Expand All @@ -22,7 +22,19 @@ function getPluginReferences(opening: JSXOpeningElement): Array<string> {
continue

for (const el of attr.value.expression.elements) {
if (!el || el.type !== 'ObjectExpression') continue
if (!el) continue

// handle plugins={[myPlugin]} and plugins={[myPlugin()]}
if (el.type === 'Identifier') {
refs.push(el.name)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
continue
}
if (el.type === 'CallExpression' && el.callee.type === 'Identifier') {
refs.push(el.callee.name)
continue
}

if (el.type !== 'ObjectExpression') continue

for (const prop of el.properties) {
if (
Expand Down Expand Up @@ -86,6 +98,7 @@ export function removeDevtools(code: string, id: string) {
const s = new MagicString(code)
const devtoolsNames = new Set<string>()
const pluginRefs: Array<string> = []
const removedElements = new Set<JSXElement>()

// Pass 1: Collect devtools import names and mark for removal
walk(result.program, (node) => {
Expand All @@ -104,7 +117,7 @@ export function removeDevtools(code: string, id: string) {

if (devtoolsNames.size === 0) return

// Pass 2: Find and remove devtools JSX elements, collect plugin references
// Pass 2: Find and remove devtools JSX elements, collect plugin references.
walk(result.program, (node, parentNode) => {
if (node.type !== 'JSXElement') return

Expand All @@ -126,6 +139,7 @@ export function removeDevtools(code: string, id: string) {

if (!matches) return

removedElements.add(node)
pluginRefs.push(...getPluginReferences(opening))

let end = node.end
Expand All @@ -146,14 +160,27 @@ export function removeDevtools(code: string, id: string) {

// Pass 3: Remove plugin imports that are no longer referenced
if (pluginRefs.length > 0) {
const retainedNames = new Set<string>()
walk(result.program, (node) => {
if (node.type === 'ImportDeclaration') return false
if (node.type === 'JSXElement' && removedElements.has(node))
return false
// Keep the import whenever the name might still refer to its binding.
if (node.type === 'Identifier' || node.type === 'JSXIdentifier') {
retainedNames.add(node.name)
}
return undefined
})

walk(result.program, (node) => {
if (node.type !== 'ImportDeclaration') return
if (isTanStackDevtoolsImport(node.source.value)) return

const toRemove = node.specifiers.filter(
(spec) =>
spec.type === 'ImportSpecifier' &&
pluginRefs.includes(spec.local.name),
pluginRefs.includes(spec.local.name) &&
!retainedNames.has(spec.local.name),
)

if (toRemove.length === 0) return
Expand All @@ -167,15 +194,22 @@ export function removeDevtools(code: string, id: string) {
if (code[end] === '\n') end++
s.remove(node.start, end)
} else {
// Rebuild import with remaining specifiers
const specTexts = remaining.map((spec) =>
code.slice(spec.start, spec.end),
)
const sourceText = code.slice(node.source.start, node.source.end)
const specTexts: Array<string> = []
const named: Array<string> = []
for (const spec of remaining) {
const specText = code.slice(spec.start, spec.end)
if (spec.type === 'ImportSpecifier') {
named.push(specText)
} else {
specTexts.push(specText)
}
}
if (named.length > 0) specTexts.push(`{ ${named.join(', ')} }`)
const sourceText = code.slice(node.source.start, node.end)
s.overwrite(
node.start,
node.end,
`import { ${specTexts.join(', ')} } from ${sourceText}`,
`import ${specTexts.join(', ')} from ${sourceText}`,
)
}
})
Expand Down