diff --git a/.changeset/remove-devtools-plugin-factory-imports.md b/.changeset/remove-devtools-plugin-factory-imports.md
new file mode 100644
index 00000000..2e38f009
--- /dev/null
+++ b/.changeset/remove-devtools-plugin-factory-imports.md
@@ -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.
diff --git a/packages/devtools-bundler-core/src/ast-utils.ts b/packages/devtools-bundler-core/src/ast-utils.ts
index 5cc2b45a..302ecd9c 100644
--- a/packages/devtools-bundler-core/src/ast-utils.ts
+++ b/packages/devtools-bundler-core/src/ast-utils.ts
@@ -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))
}
diff --git a/packages/devtools-bundler-core/src/remove-devtools.test.ts b/packages/devtools-bundler-core/src/remove-devtools.test.ts
index e8b927bb..c77e93a5 100644
--- a/packages/devtools-bundler-core/src/remove-devtools.test.ts
+++ b/packages/devtools-bundler-core/src/remove-devtools.test.ts
@@ -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 = () => (
+
+)
+`,
+ '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 = () => (
+
+)
+`,
+ '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 = () => (
+
+)
+`,
+ '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 = () => (
+ <>
+
+
+ >
+)
+`,
+ 'test.tsx',
+ )
+
+ expect(output?.code).toContain("import Widget from './plugins'")
+ expect(output?.code).not.toContain('myPlugin')
+ expect(output?.code).toContain('')
+ })
+
test('it removes the import completely even if used as a function instead of jsx', () => {
const output = removeEmptySpace(
removeDevtools(
diff --git a/packages/devtools-bundler-core/src/remove-devtools.ts b/packages/devtools-bundler-core/src/remove-devtools.ts
index 2de41989..5fc84059 100644
--- a/packages/devtools-bundler-core/src/remove-devtools.ts
+++ b/packages/devtools-bundler-core/src/remove-devtools.ts
@@ -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.
@@ -22,7 +22,19 @@ function getPluginReferences(opening: JSXOpeningElement): Array {
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)
+ 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 (
@@ -86,6 +98,7 @@ export function removeDevtools(code: string, id: string) {
const s = new MagicString(code)
const devtoolsNames = new Set()
const pluginRefs: Array = []
+ const removedElements = new Set()
// Pass 1: Collect devtools import names and mark for removal
walk(result.program, (node) => {
@@ -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
@@ -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
@@ -146,6 +160,18 @@ 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()
+ 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
@@ -153,7 +179,8 @@ export function removeDevtools(code: string, id: string) {
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
@@ -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 = []
+ const named: Array = []
+ 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}`,
)
}
})