@tanstack/devtools-vite 0.8.1 → 0.8.3
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.
- package/dist/esm/plugin.d.ts +4 -74
- package/dist/esm/plugin.js +2 -12
- package/dist/esm/plugin.js.map +1 -1
- package/package.json +2 -6
- package/src/plugin.ts +25 -97
- package/dist/esm/ast-utils.d.ts +0 -11
- package/dist/esm/ast-utils.js +0 -44
- package/dist/esm/ast-utils.js.map +0 -1
- package/dist/esm/ast-utils.test.d.ts +0 -1
- package/dist/esm/devtools-packages.d.ts +0 -2
- package/dist/esm/devtools-packages.js +0 -15
- package/dist/esm/devtools-packages.js.map +0 -1
- package/dist/esm/devtools-packages.test.d.ts +0 -1
- package/dist/esm/editor.d.ts +0 -25
- package/dist/esm/editor.js +0 -20
- package/dist/esm/editor.js.map +0 -1
- package/dist/esm/editor.test.d.ts +0 -1
- package/dist/esm/enhance-logs.d.ts +0 -4
- package/dist/esm/enhance-logs.js +0 -51
- package/dist/esm/enhance-logs.js.map +0 -1
- package/dist/esm/enhance-logs.test.d.ts +0 -1
- package/dist/esm/inject-plugin.d.ts +0 -22
- package/dist/esm/inject-plugin.js +0 -179
- package/dist/esm/inject-plugin.js.map +0 -1
- package/dist/esm/inject-plugin.test.d.ts +0 -1
- package/dist/esm/inject-source.d.ts +0 -7
- package/dist/esm/inject-source.js +0 -114
- package/dist/esm/inject-source.js.map +0 -1
- package/dist/esm/inject-source.test.d.ts +0 -1
- package/dist/esm/matcher.d.ts +0 -1
- package/dist/esm/matcher.js +0 -13
- package/dist/esm/matcher.js.map +0 -1
- package/dist/esm/matcher.test.d.ts +0 -1
- package/dist/esm/offset-to-loc.d.ts +0 -14
- package/dist/esm/offset-to-loc.js +0 -29
- package/dist/esm/offset-to-loc.js.map +0 -1
- package/dist/esm/offset-to-loc.test.d.ts +0 -1
- package/dist/esm/package-manager.d.ts +0 -16
- package/dist/esm/package-manager.js +0 -98
- package/dist/esm/package-manager.js.map +0 -1
- package/dist/esm/remove-devtools.d.ts +0 -4
- package/dist/esm/remove-devtools.js +0 -96
- package/dist/esm/remove-devtools.js.map +0 -1
- package/dist/esm/remove-devtools.test.d.ts +0 -1
- package/dist/esm/runtime-bridge.d.ts +0 -31
- package/dist/esm/runtime-bridge.js +0 -75
- package/dist/esm/runtime-bridge.js.map +0 -1
- package/dist/esm/runtime-bridge.test.d.ts +0 -1
- package/dist/esm/utils.d.ts +0 -31
- package/dist/esm/utils.js +0 -155
- package/dist/esm/utils.js.map +0 -1
- package/dist/esm/utils.test.d.ts +0 -1
- package/dist/esm/virtual-console.d.ts +0 -19
- package/dist/esm/virtual-console.js +0 -336
- package/dist/esm/virtual-console.js.map +0 -1
- package/dist/esm/virtual-console.test.d.ts +0 -1
- package/src/ast-utils.test.ts +0 -109
- package/src/ast-utils.ts +0 -58
- package/src/devtools-packages.test.ts +0 -58
- package/src/devtools-packages.ts +0 -12
- package/src/editor.test.ts +0 -75
- package/src/editor.ts +0 -53
- package/src/enhance-logs.test.ts +0 -132
- package/src/enhance-logs.ts +0 -71
- package/src/inject-plugin.test.ts +0 -1139
- package/src/inject-plugin.ts +0 -300
- package/src/inject-source.test.ts +0 -939
- package/src/inject-source.ts +0 -235
- package/src/matcher.test.ts +0 -33
- package/src/matcher.ts +0 -19
- package/src/offset-to-loc.test.ts +0 -34
- package/src/offset-to-loc.ts +0 -39
- package/src/package-manager.ts +0 -181
- package/src/remove-devtools.test.ts +0 -607
- package/src/remove-devtools.ts +0 -190
- package/src/runtime-bridge.test.ts +0 -179
- package/src/runtime-bridge.ts +0 -113
- package/src/utils.test.ts +0 -184
- package/src/utils.ts +0 -247
- package/src/virtual-console.test.ts +0 -244
- package/src/virtual-console.ts +0 -341
package/src/inject-plugin.ts
DELETED
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
import { readFileSync, writeFileSync } from 'node:fs'
|
|
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'
|
|
7
|
-
import type { PluginInjection } from '@tanstack/devtools-client'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Detects if a file imports TanStack devtools packages
|
|
11
|
-
*/
|
|
12
|
-
const detectDevtoolsImport = (code: string): boolean => {
|
|
13
|
-
try {
|
|
14
|
-
const result = parseSync('input.tsx', code, {
|
|
15
|
-
sourceType: 'module',
|
|
16
|
-
lang: 'tsx',
|
|
17
|
-
})
|
|
18
|
-
if (result.errors.length > 0) return false
|
|
19
|
-
|
|
20
|
-
let hasDevtoolsImport = false
|
|
21
|
-
|
|
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
|
-
}
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
return hasDevtoolsImport
|
|
33
|
-
} catch (e) {
|
|
34
|
-
return false
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Finds the TanStackDevtools component name in the file
|
|
40
|
-
* Handles renamed imports and namespace imports
|
|
41
|
-
*/
|
|
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
|
|
65
|
-
return
|
|
66
|
-
}
|
|
67
|
-
// import * as X from '...'
|
|
68
|
-
if (spec.type === 'ImportNamespaceSpecifier') {
|
|
69
|
-
componentName = `${spec.local.name}.TanStackDevtools`
|
|
70
|
-
return
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
})
|
|
74
|
-
|
|
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} /> }`
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export const transformAndInject = (
|
|
135
|
-
code: string,
|
|
136
|
-
injection: PluginInjection,
|
|
137
|
-
devtoolsComponentName: string,
|
|
138
|
-
): { code: string; transformed: boolean } | null => {
|
|
139
|
-
const importName = injection.pluginImport?.importName
|
|
140
|
-
const pluginType = injection.pluginImport?.type || 'jsx'
|
|
141
|
-
const displayName = injection.pluginName
|
|
142
|
-
|
|
143
|
-
if (!importName) {
|
|
144
|
-
return null
|
|
145
|
-
}
|
|
146
|
-
|
|
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
|
|
159
|
-
|
|
160
|
-
let matches = false
|
|
161
|
-
if (isNamespaceImport) {
|
|
162
|
-
if (node.name.type === 'JSXMemberExpression') {
|
|
163
|
-
const fullName = `${node.name.object.type === 'JSXIdentifier' ? node.name.object.name : ''}.${node.name.property.name}`
|
|
164
|
-
matches = fullName === devtoolsComponentName
|
|
165
|
-
}
|
|
166
|
-
} else {
|
|
167
|
-
matches =
|
|
168
|
-
node.name.type === 'JSXIdentifier' &&
|
|
169
|
-
node.name.name === devtoolsComponentName
|
|
170
|
-
}
|
|
171
|
-
|
|
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(',') ? ' ' : ', '
|
|
209
|
-
}
|
|
210
|
-
s.appendLeft(arrayEnd, prefix + pluginStr)
|
|
211
|
-
}
|
|
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)
|
|
220
|
-
} else {
|
|
221
|
-
s.appendLeft(node.end - 1, attrStr)
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
})
|
|
225
|
-
|
|
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
|
-
}
|
|
248
|
-
|
|
249
|
-
return { code: s.toString(), transformed: s.hasChanged() }
|
|
250
|
-
} catch (e) {
|
|
251
|
-
return null
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Detects if a file contains TanStack devtools import
|
|
257
|
-
*/
|
|
258
|
-
export function detectDevtoolsFile(code: string): boolean {
|
|
259
|
-
return detectDevtoolsImport(code)
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* Injects a plugin into the TanStackDevtools component in a file
|
|
264
|
-
* Reads the file, transforms it, and writes it back
|
|
265
|
-
*/
|
|
266
|
-
export function injectPluginIntoFile(
|
|
267
|
-
filePath: string,
|
|
268
|
-
injection: PluginInjection,
|
|
269
|
-
): { success: boolean; error?: string } {
|
|
270
|
-
try {
|
|
271
|
-
const code = readFileSync(filePath, 'utf-8')
|
|
272
|
-
|
|
273
|
-
const devtoolsComponentName = findDevtoolsComponentName(code)
|
|
274
|
-
if (!devtoolsComponentName) {
|
|
275
|
-
return {
|
|
276
|
-
success: false,
|
|
277
|
-
error: 'Could not find TanStackDevtools import',
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
const result = transformAndInject(code, injection, devtoolsComponentName)
|
|
282
|
-
|
|
283
|
-
if (!result?.transformed) {
|
|
284
|
-
return {
|
|
285
|
-
success: false,
|
|
286
|
-
error: 'Plugin already exists or no TanStackDevtools component found',
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
writeFileSync(filePath, result.code, 'utf-8')
|
|
291
|
-
|
|
292
|
-
return { success: true }
|
|
293
|
-
} catch (e) {
|
|
294
|
-
console.error('Error injecting plugin:', e)
|
|
295
|
-
return {
|
|
296
|
-
success: false,
|
|
297
|
-
error: e instanceof Error ? e.message : 'Unknown error',
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
}
|