@tanstack/devtools-vite 0.6.1 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/esm/ast-utils.d.ts +11 -0
  2. package/dist/esm/ast-utils.js +44 -0
  3. package/dist/esm/ast-utils.js.map +1 -0
  4. package/dist/esm/devtools-packages.d.ts +2 -0
  5. package/dist/esm/devtools-packages.js +15 -0
  6. package/dist/esm/devtools-packages.js.map +1 -0
  7. package/dist/esm/editor.js +1 -1
  8. package/dist/esm/editor.js.map +1 -1
  9. package/dist/esm/enhance-logs.d.ts +4 -1
  10. package/dist/esm/enhance-logs.js +35 -36
  11. package/dist/esm/enhance-logs.js.map +1 -1
  12. package/dist/esm/inject-plugin.d.ts +5 -4
  13. package/dist/esm/inject-plugin.js +114 -103
  14. package/dist/esm/inject-plugin.js.map +1 -1
  15. package/dist/esm/inject-source.d.ts +4 -1
  16. package/dist/esm/inject-source.js +92 -93
  17. package/dist/esm/inject-source.js.map +1 -1
  18. package/dist/esm/offset-to-loc.d.ts +14 -0
  19. package/dist/esm/offset-to-loc.js +29 -0
  20. package/dist/esm/offset-to-loc.js.map +1 -0
  21. package/dist/esm/plugin.js +23 -28
  22. package/dist/esm/plugin.js.map +1 -1
  23. package/dist/esm/remove-devtools.d.ts +4 -1
  24. package/dist/esm/remove-devtools.js +79 -86
  25. package/dist/esm/remove-devtools.js.map +1 -1
  26. package/package.json +3 -9
  27. package/skills/devtools-vite-plugin/SKILL.md +7 -7
  28. package/skills/devtools-vite-plugin/references/vite-options.md +2 -2
  29. package/src/ast-utils.ts +54 -0
  30. package/src/devtools-packages.ts +12 -0
  31. package/src/editor.ts +1 -1
  32. package/src/enhance-logs.ts +48 -78
  33. package/src/inject-plugin.test.ts +200 -147
  34. package/src/inject-plugin.ts +195 -238
  35. package/src/inject-source.test.ts +165 -115
  36. package/src/inject-source.ts +183 -231
  37. package/src/offset-to-loc.ts +39 -0
  38. package/src/plugin.ts +26 -28
  39. package/src/remove-devtools.test.ts +76 -57
  40. package/src/remove-devtools.ts +160 -168
  41. package/dist/esm/babel.d.ts +0 -18
  42. package/dist/esm/babel.js +0 -11
  43. package/dist/esm/babel.js.map +0 -1
  44. package/src/babel.ts +0 -18
@@ -1,40 +1,32 @@
1
1
  import { readFileSync, writeFileSync } from 'node:fs'
2
- import { gen, parse, t, trav } from './babel'
2
+ import MagicString from 'magic-string'
3
+ import { parseSync } from 'oxc-parser'
4
+ import { walk } from './ast-utils'
5
+ import { isTanStackDevtoolsImport } from './devtools-packages'
6
+ import type { Node } from 'oxc-parser'
3
7
  import type { PluginInjection } from '@tanstack/devtools-client'
4
- import type { types as Babel } from '@babel/core'
5
- import type { ParseResult } from '@babel/parser'
6
8
 
7
9
  /**
8
10
  * Detects if a file imports TanStack devtools packages
9
- * Handles: import X from '@tanstack/react-devtools'
10
- * import * as X from '@tanstack/react-devtools'
11
- * import { TanStackDevtools } from '@tanstack/react-devtools'
12
11
  */
13
12
  const detectDevtoolsImport = (code: string): boolean => {
14
- const devtoolsPackages = [
15
- '@tanstack/react-devtools',
16
- '@tanstack/solid-devtools',
17
- '@tanstack/vue-devtools',
18
- '@tanstack/svelte-devtools',
19
- '@tanstack/angular-devtools',
20
- ]
21
-
22
13
  try {
23
- const ast = parse(code, {
14
+ const result = parseSync('input.tsx', code, {
24
15
  sourceType: 'module',
25
- plugins: ['jsx', 'typescript'],
16
+ lang: 'tsx',
26
17
  })
18
+ if (result.errors.length > 0) return false
27
19
 
28
20
  let hasDevtoolsImport = false
29
21
 
30
- trav(ast, {
31
- ImportDeclaration(path) {
32
- const importSource = path.node.source.value
33
- if (devtoolsPackages.includes(importSource)) {
34
- hasDevtoolsImport = true
35
- path.stop()
36
- }
37
- },
22
+ walk(result.program, (node) => {
23
+ if (hasDevtoolsImport) return
24
+ if (
25
+ node.type === 'ImportDeclaration' &&
26
+ isTanStackDevtoolsImport(node.source.value)
27
+ ) {
28
+ hasDevtoolsImport = true
29
+ }
38
30
  })
39
31
 
40
32
  return hasDevtoolsImport
@@ -47,232 +39,217 @@ const detectDevtoolsImport = (code: string): boolean => {
47
39
  * Finds the TanStackDevtools component name in the file
48
40
  * Handles renamed imports and namespace imports
49
41
  */
50
- export const findDevtoolsComponentName = (
51
- ast: ParseResult<Babel.File>,
52
- ): string | null => {
53
- let componentName: string | null = null
54
- const devtoolsPackages = [
55
- '@tanstack/react-devtools',
56
- '@tanstack/solid-devtools',
57
- '@tanstack/vue-devtools',
58
- '@tanstack/svelte-devtools',
59
- '@tanstack/angular-devtools',
60
- ]
61
-
62
- trav(ast, {
63
- ImportDeclaration(path) {
64
- const importSource = path.node.source.value
65
- if (devtoolsPackages.includes(importSource)) {
66
- // Check for: import { TanStackDevtools } from '@tanstack/...'
67
- const namedImport = path.node.specifiers.find(
68
- (spec) =>
69
- t.isImportSpecifier(spec) &&
70
- t.isIdentifier(spec.imported) &&
71
- spec.imported.name === 'TanStackDevtools',
72
- )
73
- if (namedImport && t.isImportSpecifier(namedImport)) {
74
- componentName = namedImport.local.name
75
- path.stop()
42
+ export const findDevtoolsComponentName = (code: string): string | null => {
43
+ try {
44
+ const result = parseSync('input.tsx', code, {
45
+ sourceType: 'module',
46
+ lang: 'tsx',
47
+ })
48
+ if (result.errors.length > 0) return null
49
+
50
+ let componentName: string | null = null
51
+
52
+ walk(result.program, (node) => {
53
+ if (componentName) return
54
+ if (node.type !== 'ImportDeclaration') return
55
+ if (!isTanStackDevtoolsImport(node.source.value)) return
56
+
57
+ for (const spec of node.specifiers) {
58
+ // import { TanStackDevtools } or import { TanStackDevtools as X }
59
+ if (
60
+ spec.type === 'ImportSpecifier' &&
61
+ spec.imported.type === 'Identifier' &&
62
+ spec.imported.name === 'TanStackDevtools'
63
+ ) {
64
+ componentName = spec.local.name
76
65
  return
77
66
  }
78
-
79
- // Check for: import * as DevtoolsName from '@tanstack/...'
80
- const namespaceImport = path.node.specifiers.find((spec) =>
81
- t.isImportNamespaceSpecifier(spec),
82
- )
83
- if (namespaceImport && t.isImportNamespaceSpecifier(namespaceImport)) {
84
- // For namespace imports, we need to look for DevtoolsName.TanStackDevtools
85
- componentName = `${namespaceImport.local.name}.TanStackDevtools`
86
- path.stop()
67
+ // import * as X from '...'
68
+ if (spec.type === 'ImportNamespaceSpecifier') {
69
+ componentName = `${spec.local.name}.TanStackDevtools`
87
70
  return
88
71
  }
89
72
  }
90
- },
91
- })
73
+ })
92
74
 
93
- return componentName
75
+ return componentName
76
+ } catch (e) {
77
+ return null
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Check if a plugin already exists in the array expression
83
+ */
84
+ function pluginExists(
85
+ code: string,
86
+ node: Node,
87
+ importName: string,
88
+ displayName: string,
89
+ pluginType: string,
90
+ ): boolean {
91
+ if (node.type !== 'ArrayExpression') return false
92
+
93
+ for (const element of node.elements) {
94
+ if (!element) continue
95
+
96
+ if (pluginType === 'function') {
97
+ if (
98
+ element.type === 'CallExpression' &&
99
+ element.callee.type === 'Identifier' &&
100
+ element.callee.name === importName
101
+ ) {
102
+ return true
103
+ }
104
+ } else {
105
+ if (element.type !== 'ObjectExpression') continue
106
+ for (const prop of element.properties) {
107
+ if (
108
+ prop.type === 'Property' &&
109
+ prop.key.type === 'Identifier' &&
110
+ prop.key.name === 'name' &&
111
+ prop.value.type === 'Literal' &&
112
+ code.slice(prop.value.start + 1, prop.value.end - 1) === displayName
113
+ ) {
114
+ return true
115
+ }
116
+ }
117
+ }
118
+ }
119
+
120
+ return false
121
+ }
122
+
123
+ function buildPluginString(
124
+ importName: string,
125
+ displayName: string,
126
+ pluginType: string,
127
+ ): string {
128
+ if (pluginType === 'function') {
129
+ return `${importName}()`
130
+ }
131
+ return `{ name: ${JSON.stringify(displayName)}, render: <${importName} /> }`
94
132
  }
95
133
 
96
134
  export const transformAndInject = (
97
- ast: ParseResult<Babel.File>,
135
+ code: string,
98
136
  injection: PluginInjection,
99
137
  devtoolsComponentName: string,
100
- ) => {
101
- let didTransform = false
102
-
103
- // Use pluginImport if provided, otherwise generate from package name
138
+ ): { code: string; transformed: boolean } | null => {
104
139
  const importName = injection.pluginImport?.importName
105
140
  const pluginType = injection.pluginImport?.type || 'jsx'
106
141
  const displayName = injection.pluginName
107
142
 
108
143
  if (!importName) {
109
- return false
144
+ return null
110
145
  }
111
- // Handle namespace imports like DevtoolsModule.TanStackDevtools
112
- const isNamespaceImport = devtoolsComponentName.includes('.')
113
146
 
114
- // Find and modify the TanStackDevtools JSX element
115
- trav(ast, {
116
- JSXOpeningElement(path) {
117
- const elementName = path.node.name
118
- let matches = false
147
+ try {
148
+ const result = parseSync('input.tsx', code, {
149
+ sourceType: 'module',
150
+ lang: 'tsx',
151
+ })
152
+ if (result.errors.length > 0) return null
153
+
154
+ const s = new MagicString(code)
155
+ const isNamespaceImport = devtoolsComponentName.includes('.')
156
+
157
+ walk(result.program, (node) => {
158
+ if (node.type !== 'JSXOpeningElement') return
119
159
 
160
+ let matches = false
120
161
  if (isNamespaceImport) {
121
- // Handle <DevtoolsModule.TanStackDevtools />
122
- if (t.isJSXMemberExpression(elementName)) {
123
- const fullName = `${t.isJSXIdentifier(elementName.object) ? elementName.object.name : ''}.${t.isJSXIdentifier(elementName.property) ? elementName.property.name : ''}`
162
+ if (node.name.type === 'JSXMemberExpression') {
163
+ const fullName = `${node.name.object.type === 'JSXIdentifier' ? node.name.object.name : ''}.${node.name.property.name}`
124
164
  matches = fullName === devtoolsComponentName
125
165
  }
126
166
  } else {
127
- // Handle <TanStackDevtools /> or <RenamedDevtools />
128
167
  matches =
129
- t.isJSXIdentifier(elementName) &&
130
- elementName.name === devtoolsComponentName
168
+ node.name.type === 'JSXIdentifier' &&
169
+ node.name.name === devtoolsComponentName
131
170
  }
132
171
 
133
- if (matches) {
134
- // Find the plugins prop
135
- const pluginsProp = path.node.attributes.find(
136
- (attr) =>
137
- t.isJSXAttribute(attr) &&
138
- t.isJSXIdentifier(attr.name) &&
139
- attr.name.name === 'plugins',
140
- )
141
- // plugins found
142
- if (pluginsProp && t.isJSXAttribute(pluginsProp)) {
143
- // Check if plugins prop has a value
144
- if (
145
- pluginsProp.value &&
146
- t.isJSXExpressionContainer(pluginsProp.value)
147
- ) {
148
- const expression = pluginsProp.value.expression
149
-
150
- // If it's an array expression, add our plugin to it
151
- if (t.isArrayExpression(expression)) {
152
- // Check if plugin already exists
153
- const pluginExists = expression.elements.some((element) => {
154
- if (!element) return false
155
-
156
- // For function-based plugins, check if the function call exists
157
- if (pluginType === 'function') {
158
- return (
159
- t.isCallExpression(element) &&
160
- t.isIdentifier(element.callee) &&
161
- element.callee.name === importName
162
- )
163
- }
164
-
165
- // For JSX plugins, check object with name property
166
- if (!t.isObjectExpression(element)) return false
167
-
168
- return element.properties.some((prop) => {
169
- if (
170
- !t.isObjectProperty(prop) ||
171
- !t.isIdentifier(prop.key) ||
172
- prop.key.name !== 'name'
173
- ) {
174
- return false
175
- }
176
-
177
- return (
178
- t.isStringLiteral(prop.value) &&
179
- prop.value.value === displayName
180
- )
181
- })
182
- })
183
-
184
- if (!pluginExists) {
185
- // For function-based plugins, add them directly as function calls
186
- // For JSX plugins, wrap them in objects with name and render
187
- if (pluginType === 'function') {
188
- // Add directly: FormDevtoolsPlugin()
189
- expression.elements.push(
190
- t.callExpression(t.identifier(importName), []),
191
- )
192
- } else {
193
- // Add as object: { name: "...", render: <Component /> }
194
- const renderValue = t.jsxElement(
195
- t.jsxOpeningElement(t.jsxIdentifier(importName), [], true),
196
- null,
197
- [],
198
- true,
199
- )
200
-
201
- expression.elements.push(
202
- t.objectExpression([
203
- t.objectProperty(
204
- t.identifier('name'),
205
- t.stringLiteral(displayName),
206
- ),
207
- t.objectProperty(t.identifier('render'), renderValue),
208
- ]),
209
- )
210
- }
211
-
212
- didTransform = true
172
+ if (!matches) return
173
+
174
+ // Find the plugins prop
175
+ const pluginsProp = node.attributes.find(
176
+ (attr) =>
177
+ attr.type === 'JSXAttribute' &&
178
+ attr.name.type === 'JSXIdentifier' &&
179
+ attr.name.name === 'plugins',
180
+ )
181
+
182
+ if (pluginsProp && pluginsProp.type === 'JSXAttribute') {
183
+ if (
184
+ pluginsProp.value &&
185
+ pluginsProp.value.type === 'JSXExpressionContainer'
186
+ ) {
187
+ const expression = pluginsProp.value.expression
188
+ if (expression.type === 'ArrayExpression') {
189
+ if (
190
+ !pluginExists(
191
+ code,
192
+ expression,
193
+ importName,
194
+ displayName,
195
+ pluginType,
196
+ )
197
+ ) {
198
+ const pluginStr = buildPluginString(
199
+ importName,
200
+ displayName,
201
+ pluginType,
202
+ )
203
+ // Insert before closing ']'
204
+ const arrayEnd = expression.end - 1
205
+ let prefix = ''
206
+ if (expression.elements.length > 0) {
207
+ const arrayContent = code.slice(expression.start + 1, arrayEnd)
208
+ prefix = arrayContent.trimEnd().endsWith(',') ? ' ' : ', '
213
209
  }
210
+ s.appendLeft(arrayEnd, prefix + pluginStr)
214
211
  }
215
212
  }
213
+ }
214
+ } else {
215
+ // No plugins prop — create one
216
+ const pluginStr = buildPluginString(importName, displayName, pluginType)
217
+ const attrStr = ` plugins={[${pluginStr}]}`
218
+ if (node.selfClosing) {
219
+ s.appendLeft(node.end - 2, attrStr)
216
220
  } else {
217
- // No plugins prop exists, create one with our plugin
218
- // For function-based plugins, add them directly as function calls
219
- // For JSX plugins, wrap them in objects with name and render
220
- let pluginElement
221
- if (pluginType === 'function') {
222
- // Add directly: plugins={[FormDevtoolsPlugin()]}
223
- pluginElement = t.callExpression(t.identifier(importName), [])
224
- } else {
225
- // Add as object: plugins={[{ name: "...", render: <Component /> }]}
226
- const renderValue = t.jsxElement(
227
- t.jsxOpeningElement(t.jsxIdentifier(importName), [], true),
228
- null,
229
- [],
230
- true,
231
- )
232
-
233
- pluginElement = t.objectExpression([
234
- t.objectProperty(
235
- t.identifier('name'),
236
- t.stringLiteral(displayName),
237
- ),
238
- t.objectProperty(t.identifier('render'), renderValue),
239
- ])
240
- }
241
-
242
- path.node.attributes.push(
243
- t.jsxAttribute(
244
- t.jsxIdentifier('plugins'),
245
- t.jsxExpressionContainer(t.arrayExpression([pluginElement])),
246
- ),
247
- )
248
-
249
- didTransform = true
221
+ s.appendLeft(node.end - 1, attrStr)
250
222
  }
251
223
  }
252
- },
253
- })
254
-
255
- // Add import at the top of the file if transform happened
256
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
257
- if (didTransform) {
258
- const importDeclaration = t.importDeclaration(
259
- [t.importSpecifier(t.identifier(importName), t.identifier(importName))],
260
- t.stringLiteral(injection.packageName),
261
- )
262
-
263
- // Find the last import declaration
264
- let lastImportIndex = -1
265
- ast.program.body.forEach((node, index) => {
266
- if (t.isImportDeclaration(node)) {
267
- lastImportIndex = index
268
- }
269
224
  })
270
225
 
271
- // Insert after the last import or at the beginning
272
- ast.program.body.splice(lastImportIndex + 1, 0, importDeclaration)
273
- }
226
+ // Add import at the top of the file if transform happened
227
+ if (s.hasChanged()) {
228
+ const state = { lastImportEnd: 0, alreadyImported: false }
229
+ walk(result.program, (n) => {
230
+ if (n.type !== 'ImportDeclaration') return
231
+ if (n.end > state.lastImportEnd) state.lastImportEnd = n.end
232
+ if (n.source.value !== injection.packageName) return
233
+ for (const spec of n.specifiers) {
234
+ if (
235
+ spec.type === 'ImportSpecifier' &&
236
+ spec.imported.type === 'Identifier' &&
237
+ spec.imported.name === importName
238
+ ) {
239
+ state.alreadyImported = true
240
+ }
241
+ }
242
+ })
243
+ if (!state.alreadyImported) {
244
+ const importStr = `\nimport { ${importName} } from ${JSON.stringify(injection.packageName)};`
245
+ s.appendRight(state.lastImportEnd, importStr)
246
+ }
247
+ }
274
248
 
275
- return didTransform
249
+ return { code: s.toString(), transformed: s.hasChanged() }
250
+ } catch (e) {
251
+ return null
252
+ }
276
253
  }
277
254
 
278
255
  /**
@@ -291,17 +268,9 @@ export function injectPluginIntoFile(
291
268
  injection: PluginInjection,
292
269
  ): { success: boolean; error?: string } {
293
270
  try {
294
- // Read the file
295
271
  const code = readFileSync(filePath, 'utf-8')
296
272
 
297
- // Parse the code
298
- const ast = parse(code, {
299
- sourceType: 'module',
300
- plugins: ['jsx', 'typescript'],
301
- })
302
-
303
- // Find the devtools component name (handles renamed imports)
304
- const devtoolsComponentName = findDevtoolsComponentName(ast)
273
+ const devtoolsComponentName = findDevtoolsComponentName(code)
305
274
  if (!devtoolsComponentName) {
306
275
  return {
307
276
  success: false,
@@ -309,27 +278,15 @@ export function injectPluginIntoFile(
309
278
  }
310
279
  }
311
280
 
312
- // Transform and inject
313
- const didTransform = transformAndInject(
314
- ast,
315
- injection,
316
- devtoolsComponentName,
317
- )
281
+ const result = transformAndInject(code, injection, devtoolsComponentName)
318
282
 
319
- if (!didTransform) {
283
+ if (!result?.transformed) {
320
284
  return {
321
285
  success: false,
322
286
  error: 'Plugin already exists or no TanStackDevtools component found',
323
287
  }
324
288
  }
325
289
 
326
- // Generate the new code
327
- const result = gen(ast, {
328
- sourceMaps: false,
329
- retainLines: false,
330
- })
331
-
332
- // Write back to file
333
290
  writeFileSync(filePath, result.code, 'utf-8')
334
291
 
335
292
  return { success: true }