From ebe837d37a1feb6685bcfbf10c9dc54e815b36d8 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Thu, 19 Nov 2026 08:04:09 -0400 Subject: [PATCH 1/5] fix(devtools-bundler-core): remove plugin imports used directly in the plugins array --- .../remove-devtools-plugin-factory-imports.md | 5 +++ .../src/remove-devtools.test.ts | 44 +++++++++++++++++++ .../src/remove-devtools.ts | 14 +++++- 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 .changeset/remove-devtools-plugin-factory-imports.md diff --git a/.changeset/remove-devtools-plugin-factory-imports.md b/.changeset/remove-devtools-plugin-factory-imports.md new file mode 100644 index 000000000..510d3b68f --- /dev/null +++ b/.changeset/remove-devtools-plugin-factory-imports.md @@ -0,0 +1,5 @@ +--- +'@tanstack/devtools-bundler-core': patch +--- + +Remove the import of a plugin used directly in the `plugins` array (for example `plugins={[hotkeysDevtoolsPlugin()]}`) when the devtools are stripped from production builds. diff --git a/packages/devtools-bundler-core/src/remove-devtools.test.ts b/packages/devtools-bundler-core/src/remove-devtools.test.ts index e8b927bb3..8648b11cd 100644 --- a/packages/devtools-bundler-core/src/remove-devtools.test.ts +++ b/packages/devtools-bundler-core/src/remove-devtools.test.ts @@ -427,6 +427,50 @@ 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('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 2de419894..03db17ce3 100644 --- a/packages/devtools-bundler-core/src/remove-devtools.ts +++ b/packages/devtools-bundler-core/src/remove-devtools.ts @@ -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 ( From 942a9f3985aa6754725dd57ae3713acdbe712073 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 29 Nov 2026 01:46:54 -0400 Subject: [PATCH 2/5] fix(devtools-bundler-core): preserve plugin imports used outside devtools --- .../remove-devtools-plugin-factory-imports.md | 2 +- .../devtools-bundler-core/src/ast-utils.ts | 6 +-- .../src/remove-devtools.test.ts | 42 +++++++++++++++++++ .../src/remove-devtools.ts | 38 +++++++++++++---- 4 files changed, 75 insertions(+), 13 deletions(-) diff --git a/.changeset/remove-devtools-plugin-factory-imports.md b/.changeset/remove-devtools-plugin-factory-imports.md index 510d3b68f..e5f2f94c4 100644 --- a/.changeset/remove-devtools-plugin-factory-imports.md +++ b/.changeset/remove-devtools-plugin-factory-imports.md @@ -2,4 +2,4 @@ '@tanstack/devtools-bundler-core': patch --- -Remove the import of a plugin used directly in the `plugins` array (for example `plugins={[hotkeysDevtoolsPlugin()]}`) when the devtools are stripped from production builds. +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. diff --git a/packages/devtools-bundler-core/src/ast-utils.ts b/packages/devtools-bundler-core/src/ast-utils.ts index 5cc2b45a5..37f37105f 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) => void | false, 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 8648b11cd..c77e93a51 100644 --- a/packages/devtools-bundler-core/src/remove-devtools.test.ts +++ b/packages/devtools-bundler-core/src/remove-devtools.test.ts @@ -471,6 +471,48 @@ 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 03db17ce3..9bff33824 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. @@ -98,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) => { @@ -116,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 @@ -138,6 +139,7 @@ export function removeDevtools(code: string, id: string) { if (!matches) return + removedElements.add(node) pluginRefs.push(...getPluginReferences(opening)) let end = node.end @@ -158,6 +160,16 @@ 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) + } + }) + walk(result.program, (node) => { if (node.type !== 'ImportDeclaration') return if (isTanStackDevtoolsImport(node.source.value)) return @@ -165,7 +177,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 @@ -179,15 +192,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}`, ) } }) From 092c854ec3592b56721f83f82d2fac57079a4ea9 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 29 Nov 2026 02:04:19 -0400 Subject: [PATCH 3/5] fix(devtools-bundler-core): return explicitly from plugin import visitor --- packages/devtools-bundler-core/src/remove-devtools.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/devtools-bundler-core/src/remove-devtools.ts b/packages/devtools-bundler-core/src/remove-devtools.ts index 9bff33824..5fc840591 100644 --- a/packages/devtools-bundler-core/src/remove-devtools.ts +++ b/packages/devtools-bundler-core/src/remove-devtools.ts @@ -163,11 +163,13 @@ export function removeDevtools(code: string, id: string) { const retainedNames = new Set() walk(result.program, (node) => { if (node.type === 'ImportDeclaration') return false - if (node.type === 'JSXElement' && removedElements.has(node)) 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) => { From e92f8f56448eb48c84f435ad5bee21a87deef8f9 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 29 Nov 2026 02:04:41 -0400 Subject: [PATCH 4/5] test(devtools-bundler-core): match the AST visitor return contract --- packages/devtools-bundler-core/src/ast-utils.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/devtools-bundler-core/src/ast-utils.test.ts b/packages/devtools-bundler-core/src/ast-utils.test.ts index 3e17a1c32..105ba7453 100644 --- a/packages/devtools-bundler-core/src/ast-utils.test.ts +++ b/packages/devtools-bundler-core/src/ast-utils.test.ts @@ -123,7 +123,9 @@ describe('walk', () => { const visits: Array<{ node: Node; parent: Node | undefined }> = [] walk( root, - (node, parentNode) => visits.push({ node, parent: parentNode }), + (node, parentNode) => { + visits.push({ node, parent: parentNode }) + }, fakeParent, ) From f10a2e9e70545483b5e331056925d825f9ccc8b5 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 29 Nov 2026 02:15:08 -0400 Subject: [PATCH 5/5] fix(devtools-bundler-core): preserve walk visitor compatibility --- .changeset/remove-devtools-plugin-factory-imports.md | 2 ++ packages/devtools-bundler-core/src/ast-utils.test.ts | 4 +--- packages/devtools-bundler-core/src/ast-utils.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.changeset/remove-devtools-plugin-factory-imports.md b/.changeset/remove-devtools-plugin-factory-imports.md index e5f2f94c4..2e38f009f 100644 --- a/.changeset/remove-devtools-plugin-factory-imports.md +++ b/.changeset/remove-devtools-plugin-factory-imports.md @@ -3,3 +3,5 @@ --- 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.test.ts b/packages/devtools-bundler-core/src/ast-utils.test.ts index 105ba7453..3e17a1c32 100644 --- a/packages/devtools-bundler-core/src/ast-utils.test.ts +++ b/packages/devtools-bundler-core/src/ast-utils.test.ts @@ -123,9 +123,7 @@ describe('walk', () => { const visits: Array<{ node: Node; parent: Node | undefined }> = [] walk( root, - (node, parentNode) => { - visits.push({ node, parent: parentNode }) - }, + (node, parentNode) => visits.push({ node, parent: parentNode }), fakeParent, ) diff --git a/packages/devtools-bundler-core/src/ast-utils.ts b/packages/devtools-bundler-core/src/ast-utils.ts index 37f37105f..302ecd9c0 100644 --- a/packages/devtools-bundler-core/src/ast-utils.ts +++ b/packages/devtools-bundler-core/src/ast-utils.ts @@ -53,7 +53,7 @@ export function forEachChild(node: Node, callback: (child: Node) => void) { */ export function walk( node: Node, - visitor: (node: Node, parentNode?: Node) => void | false, + visitor: (node: Node, parentNode?: Node) => unknown, parentNode?: Node, ) { if (visitor(node, parentNode) === false) return