diff --git a/.changeset/remove-devtools-plugin-factory-imports.md b/.changeset/remove-devtools-plugin-factory-imports.md new file mode 100644 index 00000000..510d3b68 --- /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 e8b927bb..8648b11c 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 2de41989..03db17ce 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 (