From ebe837d37a1feb6685bcfbf10c9dc54e815b36d8 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Thu, 19 Nov 2026 08:04:09 -0400 Subject: [PATCH] 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 (