Open History to see the last commit
1import MagicString from 'magic-string'
2import { parseSync } from 'oxc-parser'
3import { walk } from './ast-utils'
4import { isTanStackDevtoolsImport } from './devtools-packages'
5import type { JSXOpeningElement } from 'oxc-parser'
6β
7/**
8 * Extract component names referenced in the `plugins` prop of a devtools element.
9 */
10function getPluginReferences(opening: JSXOpeningElement): Array<string> {
11 const refs: Array<string> = []
12β
13 for (const attr of opening.attributes) {
14 if (attr.type !== 'JSXAttribute') continue
15 if (attr.name.type !== 'JSXIdentifier' || attr.name.name !== 'plugins')
16 continue
17 if (
18 !attr.value ||
19 attr.value.type !== 'JSXExpressionContainer' ||
20 attr.value.expression.type !== 'ArrayExpression'
21 )
22 continue
23β
24 for (const el of attr.value.expression.elements) {
25 if (!el || el.type !== 'ObjectExpression') continue
26β
27 for (const prop of el.properties) {
28 if (
29 prop.type !== 'Property' ||
30 prop.key.type !== 'Identifier' ||
31 prop.key.name !== 'render'
32 )
33 continue
34β
35 const value = prop.value
36β
37 // handle <ReactRouterPanel />
38 if (
39 value.type === 'JSXElement' &&
40 value.openingElement.name.type === 'JSXIdentifier'
41 ) {
42 refs.push(value.openingElement.name.name)
43 }
44 // handle () => <ReactRouterPanel />
45 else if (
46 value.type === 'ArrowFunctionExpression' ||
47 value.type === 'FunctionExpression'
48 ) {
49 const body = value.body
50 if (
51 body &&
52 body.type === 'JSXElement' &&
53 body.openingElement.name.type === 'JSXIdentifier'
54 ) {
55 refs.push(body.openingElement.name.name)
56 }
57 }
58 // handle render: SomeComponent
59 else if (value.type === 'Identifier') {
60 refs.push(value.name)
61 }
62 // handle render: someFunction()
63 else if (
64 value.type === 'CallExpression' &&
65 value.callee.type === 'Identifier'
66 ) {
67 refs.push(value.callee.name)
68 }
69 }
70 }
71 }
72β
73 return refs
74}
75β
76export function removeDevtools(code: string, id: string) {
77 const filePath = id.split('?')[0]!
78β
79 try {
80 const result = parseSync(filePath, code, {
81 sourceType: 'module',
82 lang: 'tsx',
83 })
84 if (result.errors.length > 0) return
85β
86 const s = new MagicString(code)
87 const devtoolsNames = new Set<string>()
88 const pluginRefs: Array<string> = []
89β
90 // Pass 1: Collect devtools import names and mark for removal
91 walk(result.program, (node) => {
92 if (
93 node.type === 'ImportDeclaration' &&
94 isTanStackDevtoolsImport(node.source.value)
95 ) {
96 for (const spec of node.specifiers) {
97 devtoolsNames.add(spec.local.name)
98 }
99 let end = node.end
100 if (code[end] === '\n') end++
101 s.remove(node.start, end)
102 }
103 })
104β
105 if (devtoolsNames.size === 0) return
106β
107 // Pass 2: Find and remove devtools JSX elements, collect plugin references
108 walk(result.program, (node, parentNode) => {
109 if (node.type !== 'JSXElement') return
110β
111 const opening = node.openingElement
112 let matches = false
113β
114 if (
115 opening.name.type === 'JSXIdentifier' &&
116 devtoolsNames.has(opening.name.name)
117 ) {
118 matches = true
119 } else if (
120 opening.name.type === 'JSXMemberExpression' &&
121 opening.name.object.type === 'JSXIdentifier' &&
122 devtoolsNames.has(opening.name.object.name)
123 ) {
124 matches = true
125 }
126β
127 if (!matches) return
128β
129 pluginRefs.push(...getPluginReferences(opening))
130β
131 let end = node.end
132 if (code[end] === '\n') end++
133 if (parentNode?.type === 'ParenthesizedExpression') {
134 s.overwrite(node.start, end, 'null')
135 } else {
136 s.remove(node.start, end)
137 }
138 })
139β
140 // Pass 3: Remove plugin imports that are no longer referenced
141 if (pluginRefs.length > 0) {
142 walk(result.program, (node) => {
143 if (node.type !== 'ImportDeclaration') return
144 if (isTanStackDevtoolsImport(node.source.value)) return
145β
146 const toRemove = node.specifiers.filter(
147 (spec) =>
148 spec.type === 'ImportSpecifier' &&
149 pluginRefs.includes(spec.local.name),
150 )
151β
152 if (toRemove.length === 0) return
153β
154 const remaining = node.specifiers.filter(
155 (spec) => !toRemove.includes(spec),
156 )
157β
158 if (remaining.length === 0) {
159 let end = node.end
160 if (code[end] === '\n') end++
161 s.remove(node.start, end)
162 } else {
163 // Rebuild import with remaining specifiers
164 const specTexts = remaining.map((spec) =>
165 code.slice(spec.start, spec.end),
166 )
167 const sourceText = code.slice(node.source.start, node.source.end)
168 s.overwrite(
169 node.start,
170 node.end,
171 `import { ${specTexts.join(', ')} } from ${sourceText}`,
172 )
173 }
174 })
175 }
176β
177 if (!s.hasChanged()) return
178β
179 return {
180 code: s.toString(),
181 map: s.generateMap({
182 source: filePath,
183 file: id,
184 includeContent: true,
185 }),
186 }
187 } catch (e) {
188 return
189 }
190}