@tanstack/devtools-vite 0.8.1 → 0.8.2

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 (81) hide show
  1. package/dist/esm/plugin.d.ts +4 -74
  2. package/dist/esm/plugin.js +2 -12
  3. package/dist/esm/plugin.js.map +1 -1
  4. package/package.json +2 -6
  5. package/src/plugin.ts +25 -97
  6. package/dist/esm/ast-utils.d.ts +0 -11
  7. package/dist/esm/ast-utils.js +0 -44
  8. package/dist/esm/ast-utils.js.map +0 -1
  9. package/dist/esm/ast-utils.test.d.ts +0 -1
  10. package/dist/esm/devtools-packages.d.ts +0 -2
  11. package/dist/esm/devtools-packages.js +0 -15
  12. package/dist/esm/devtools-packages.js.map +0 -1
  13. package/dist/esm/devtools-packages.test.d.ts +0 -1
  14. package/dist/esm/editor.d.ts +0 -25
  15. package/dist/esm/editor.js +0 -20
  16. package/dist/esm/editor.js.map +0 -1
  17. package/dist/esm/editor.test.d.ts +0 -1
  18. package/dist/esm/enhance-logs.d.ts +0 -4
  19. package/dist/esm/enhance-logs.js +0 -51
  20. package/dist/esm/enhance-logs.js.map +0 -1
  21. package/dist/esm/enhance-logs.test.d.ts +0 -1
  22. package/dist/esm/inject-plugin.d.ts +0 -22
  23. package/dist/esm/inject-plugin.js +0 -179
  24. package/dist/esm/inject-plugin.js.map +0 -1
  25. package/dist/esm/inject-plugin.test.d.ts +0 -1
  26. package/dist/esm/inject-source.d.ts +0 -7
  27. package/dist/esm/inject-source.js +0 -114
  28. package/dist/esm/inject-source.js.map +0 -1
  29. package/dist/esm/inject-source.test.d.ts +0 -1
  30. package/dist/esm/matcher.d.ts +0 -1
  31. package/dist/esm/matcher.js +0 -13
  32. package/dist/esm/matcher.js.map +0 -1
  33. package/dist/esm/matcher.test.d.ts +0 -1
  34. package/dist/esm/offset-to-loc.d.ts +0 -14
  35. package/dist/esm/offset-to-loc.js +0 -29
  36. package/dist/esm/offset-to-loc.js.map +0 -1
  37. package/dist/esm/offset-to-loc.test.d.ts +0 -1
  38. package/dist/esm/package-manager.d.ts +0 -16
  39. package/dist/esm/package-manager.js +0 -98
  40. package/dist/esm/package-manager.js.map +0 -1
  41. package/dist/esm/remove-devtools.d.ts +0 -4
  42. package/dist/esm/remove-devtools.js +0 -96
  43. package/dist/esm/remove-devtools.js.map +0 -1
  44. package/dist/esm/remove-devtools.test.d.ts +0 -1
  45. package/dist/esm/runtime-bridge.d.ts +0 -31
  46. package/dist/esm/runtime-bridge.js +0 -75
  47. package/dist/esm/runtime-bridge.js.map +0 -1
  48. package/dist/esm/runtime-bridge.test.d.ts +0 -1
  49. package/dist/esm/utils.d.ts +0 -31
  50. package/dist/esm/utils.js +0 -155
  51. package/dist/esm/utils.js.map +0 -1
  52. package/dist/esm/utils.test.d.ts +0 -1
  53. package/dist/esm/virtual-console.d.ts +0 -19
  54. package/dist/esm/virtual-console.js +0 -336
  55. package/dist/esm/virtual-console.js.map +0 -1
  56. package/dist/esm/virtual-console.test.d.ts +0 -1
  57. package/src/ast-utils.test.ts +0 -109
  58. package/src/ast-utils.ts +0 -58
  59. package/src/devtools-packages.test.ts +0 -58
  60. package/src/devtools-packages.ts +0 -12
  61. package/src/editor.test.ts +0 -75
  62. package/src/editor.ts +0 -53
  63. package/src/enhance-logs.test.ts +0 -132
  64. package/src/enhance-logs.ts +0 -71
  65. package/src/inject-plugin.test.ts +0 -1139
  66. package/src/inject-plugin.ts +0 -300
  67. package/src/inject-source.test.ts +0 -939
  68. package/src/inject-source.ts +0 -235
  69. package/src/matcher.test.ts +0 -33
  70. package/src/matcher.ts +0 -19
  71. package/src/offset-to-loc.test.ts +0 -34
  72. package/src/offset-to-loc.ts +0 -39
  73. package/src/package-manager.ts +0 -181
  74. package/src/remove-devtools.test.ts +0 -607
  75. package/src/remove-devtools.ts +0 -190
  76. package/src/runtime-bridge.test.ts +0 -179
  77. package/src/runtime-bridge.ts +0 -113
  78. package/src/utils.test.ts +0 -184
  79. package/src/utils.ts +0 -247
  80. package/src/virtual-console.test.ts +0 -244
  81. package/src/virtual-console.ts +0 -341
@@ -1,235 +0,0 @@
1
- import { normalizePath } from 'vite'
2
- import MagicString from 'magic-string'
3
- import { parseSync } from 'oxc-parser'
4
- import { createLocMapper } from './offset-to-loc'
5
- import { matcher } from './matcher'
6
- import { forEachChild } from './ast-utils'
7
- import type {
8
- JSXElementName,
9
- JSXOpeningElement,
10
- Node,
11
- ParamPattern,
12
- } from 'oxc-parser'
13
- import type { Loc } from './offset-to-loc'
14
-
15
- /**
16
- * Extract the props name from a function's first parameter.
17
- * Handles: `(props) => {}`, `({ ...props }) => {}`, `function(props) {}`
18
- */
19
- function getPropsName(params: Array<ParamPattern>): string | null {
20
- const first = params[0]
21
- if (!first) return null
22
-
23
- // (props) => {} or function(props) {}
24
- if (first.type === 'Identifier') return first.name
25
-
26
- // ({ ...rest }) => {} or function({ ...rest }) {}
27
- if (first.type === 'ObjectPattern') {
28
- for (const prop of first.properties) {
29
- if (prop.type === 'RestElement' && prop.argument.type === 'Identifier') {
30
- return prop.argument.name
31
- }
32
- }
33
- }
34
-
35
- return null
36
- }
37
-
38
- /**
39
- * Get the display name from a JSX element name node.
40
- */
41
- function getNameOfElement(name: JSXElementName): string {
42
- if (name.type === 'JSXIdentifier') return name.name
43
- if (name.type === 'JSXMemberExpression') {
44
- return `${getNameOfElement(name.object)}.${name.property.name}`
45
- }
46
- // JSXNamespacedName
47
- return `${name.namespace.name}:${name.name.name}`
48
- }
49
-
50
- /**
51
- * Check whether a JSXOpeningElement should receive `data-tsd-source`.
52
- */
53
- function shouldTransform(
54
- node: JSXOpeningElement,
55
- propsName: string | null,
56
- ignorePatterns: Array<string | RegExp>,
57
- ): boolean {
58
- const nameOfElement = getNameOfElement(node.name)
59
-
60
- if (nameOfElement === 'Fragment' || nameOfElement === 'React.Fragment') {
61
- return false
62
- }
63
- if (matcher(ignorePatterns, nameOfElement)) return false
64
-
65
- // Already annotated?
66
- if (
67
- node.attributes.some(
68
- (a) =>
69
- a.type === 'JSXAttribute' &&
70
- a.name.type === 'JSXIdentifier' &&
71
- a.name.name === 'data-tsd-source',
72
- )
73
- ) {
74
- return false
75
- }
76
-
77
- // Props spread?
78
- if (
79
- node.attributes.some(
80
- (a) =>
81
- a.type === 'JSXSpreadAttribute' &&
82
- a.argument.type === 'Identifier' &&
83
- a.argument.name === propsName,
84
- )
85
- ) {
86
- return false
87
- }
88
-
89
- return true
90
- }
91
-
92
- function isFunctionLike(node: Node): boolean {
93
- return (
94
- node.type === 'FunctionDeclaration' ||
95
- node.type === 'FunctionExpression' ||
96
- node.type === 'ArrowFunctionExpression'
97
- )
98
- }
99
-
100
- /**
101
- * Walk a subtree collecting JSXOpeningElements owned by the current function.
102
- * Function-like nodes are traversal boundaries so inner components keep their
103
- * own propsName context and aren't annotated with an outer function's props.
104
- */
105
- function collectJsx(node: Node, out: Array<JSXOpeningElement>) {
106
- if (node.type === 'JSXOpeningElement') {
107
- out.push(node)
108
- return
109
- }
110
- if (isFunctionLike(node)) return
111
- forEachChild(node, (child) => collectJsx(child, out))
112
- }
113
-
114
- /**
115
- * Walk the AST to find all function-like nodes (in document order).
116
- * For each, extract propsName and collect JSX elements from its body.
117
- */
118
- function visitFunctions(
119
- node: Node,
120
- annotated: Set<number>,
121
- file: string,
122
- ignorePatterns: Array<string | RegExp>,
123
- offsetToLoc: (offset: number) => Loc,
124
- s: MagicString,
125
- code: string,
126
- ): boolean {
127
- let didTransform = false
128
-
129
- const processFunction = (params: Array<ParamPattern>, body: Node) => {
130
- const propsName = getPropsName(params)
131
- const jsxNodes: Array<JSXOpeningElement> = []
132
- collectJsx(body, jsxNodes)
133
-
134
- for (const jsx of jsxNodes) {
135
- if (annotated.has(jsx.start)) continue
136
- if (!shouldTransform(jsx, propsName, ignorePatterns)) continue
137
-
138
- const loc = offsetToLoc(jsx.start)
139
- const attrStr = ` data-tsd-source="${file}:${loc.line}:${loc.column + 1}"`
140
-
141
- // Insert before '>' or '/>' at the end of the opening element
142
- if (jsx.selfClosing) {
143
- // ends with '/>' — insert before '/>'
144
- s.appendLeft(jsx.end - 2, attrStr)
145
- } else {
146
- // ends with '>' — insert before '>'
147
- s.appendLeft(jsx.end - 1, attrStr)
148
- }
149
-
150
- annotated.add(jsx.start)
151
- didTransform = true
152
- }
153
- }
154
-
155
- // Check if this node is a function-like node. Variable-initialized
156
- // functions (`const Foo = () => {}`) are handled when forEachChild reaches
157
- // their init, so VariableDeclaration doesn't need a special case here.
158
- if (
159
- node.type === 'FunctionDeclaration' ||
160
- node.type === 'FunctionExpression'
161
- ) {
162
- if (node.body) processFunction(node.params, node.body)
163
- } else if (node.type === 'ArrowFunctionExpression') {
164
- processFunction(node.params, node.body)
165
- }
166
-
167
- // Recurse into children to find nested functions
168
- forEachChild(node, (child) => {
169
- if (
170
- visitFunctions(
171
- child,
172
- annotated,
173
- file,
174
- ignorePatterns,
175
- offsetToLoc,
176
- s,
177
- code,
178
- )
179
- ) {
180
- didTransform = true
181
- }
182
- })
183
-
184
- return didTransform
185
- }
186
-
187
- export function addSourceToJsx(
188
- code: string,
189
- id: string,
190
- ignore: {
191
- files?: Array<string | RegExp>
192
- components?: Array<string | RegExp>
193
- } = {},
194
- ) {
195
- const filePath = id.split('?')[0]!
196
- const location = filePath.replace(normalizePath(process.cwd()), '')
197
-
198
- const fileIgnored = matcher(ignore.files || [], location)
199
- if (fileIgnored) return
200
-
201
- try {
202
- const result = parseSync(filePath, code, {
203
- sourceType: 'module',
204
- lang: 'tsx',
205
- })
206
- if (result.errors.length > 0) return
207
-
208
- const offsetToLoc = createLocMapper(code)
209
- const s = new MagicString(code)
210
- const annotated = new Set<number>()
211
-
212
- const didTransform = visitFunctions(
213
- result.program,
214
- annotated,
215
- location,
216
- ignore.components || [],
217
- offsetToLoc,
218
- s,
219
- code,
220
- )
221
-
222
- if (!didTransform) return
223
-
224
- return {
225
- code: s.toString(),
226
- map: s.generateMap({
227
- source: filePath,
228
- file: id,
229
- includeContent: true,
230
- }),
231
- }
232
- } catch (e) {
233
- return
234
- }
235
- }
@@ -1,33 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import { matcher } from './matcher'
3
-
4
- describe('matcher', () => {
5
- it('returns false when patterns is empty', () => {
6
- expect(matcher([], 'foo')).toBe(false)
7
- })
8
-
9
- it('matches string patterns against component names', () => {
10
- const patterns = ['Button', 'MyComponent']
11
- expect(matcher(patterns, 'Button')).toBe(true)
12
- expect(matcher(patterns, 'Other')).toBe(false)
13
- })
14
-
15
- it('matches regex patterns against component names', () => {
16
- const patterns = [/^Button$/, /Comp/]
17
- expect(matcher(patterns, 'Button')).toBe(true)
18
- expect(matcher(patterns, 'MyComp')).toBe(true)
19
- expect(matcher(patterns, 'NoMatch')).toBe(false)
20
- })
21
-
22
- it('matches file paths with glob patterns', () => {
23
- const patterns = ['**/ignored-file.jsx']
24
- expect(matcher(patterns, 'src/components/ignored-file.jsx')).toBe(true)
25
- expect(matcher(patterns, 'src/components/other.jsx')).toBe(false)
26
- })
27
-
28
- it('matches file paths with regex', () => {
29
- const patterns = [/ignored-file\.jsx$/]
30
- expect(matcher(patterns, 'src/components/ignored-file.jsx')).toBe(true)
31
- expect(matcher(patterns, 'src/components/other.jsx')).toBe(false)
32
- })
33
- })
package/src/matcher.ts DELETED
@@ -1,19 +0,0 @@
1
- import picomatch from 'picomatch'
2
-
3
- export const matcher = (
4
- patterns: Array<string | RegExp>,
5
- str: string,
6
- ): boolean => {
7
- if (patterns.length === 0) {
8
- return false
9
- }
10
- const matchers = patterns.map((pattern) => {
11
- if (typeof pattern === 'string') {
12
- return picomatch(pattern)
13
- } else {
14
- return (s: string) => pattern.test(s)
15
- }
16
- })
17
-
18
- return matchers.some((isMatch) => isMatch(str))
19
- }
@@ -1,34 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
- import { createLocMapper } from './offset-to-loc'
3
-
4
- describe('createLocMapper', () => {
5
- // Source: "ab\ncde\nf"
6
- // Indices: 0 1 2 3 4 5 6 7
7
- // lineStarts: [0, 3, 7]
8
- const source = 'ab\ncde\nf'
9
- const toLoc = createLocMapper(source)
10
-
11
- it('maps offset 0 to line 1, column 0', () => {
12
- expect(toLoc(0)).toEqual({ line: 1, column: 0 })
13
- })
14
-
15
- it('maps the start of line 2 (offset 3, just after first \\n) to line 2, column 0', () => {
16
- expect(toLoc(3)).toEqual({ line: 2, column: 0 })
17
- })
18
-
19
- it('maps a mid-line-2 offset (offset 5, "e") to line 2, column 2', () => {
20
- expect(toLoc(5)).toEqual({ line: 2, column: 2 })
21
- })
22
-
23
- it('maps the final offset (offset 7, "f") to line 3, column 0', () => {
24
- expect(toLoc(7)).toEqual({ line: 3, column: 0 })
25
- })
26
-
27
- it('maps an offset on a \\n boundary char (offset 2) to line 1, column 2', () => {
28
- expect(toLoc(2)).toEqual({ line: 1, column: 2 })
29
- })
30
-
31
- it('maps the second \\n boundary (offset 6) to line 2, column 3', () => {
32
- expect(toLoc(6)).toEqual({ line: 2, column: 3 })
33
- })
34
- })
@@ -1,39 +0,0 @@
1
- export interface Loc {
2
- /** 1-based line number */
3
- line: number
4
- /** 0-based column offset */
5
- column: number
6
- }
7
-
8
- /**
9
- * Pre-compute line start offsets for fast byte-offset to line/column conversion.
10
- * Returns a function that maps a byte offset to `{ line, column }`.
11
- *
12
- * Build: O(n) single pass over the source string.
13
- * Lookup: O(log n) binary search.
14
- */
15
- export function createLocMapper(source: string): (offset: number) => Loc {
16
- const lineStarts: Array<number> = [0]
17
- for (let i = 0; i < source.length; i++) {
18
- if (source.charCodeAt(i) === 10 /* \n */) {
19
- lineStarts.push(i + 1)
20
- }
21
- }
22
-
23
- return (offset: number): Loc => {
24
- let lo = 0
25
- let hi = lineStarts.length - 1
26
- while (lo < hi) {
27
- const mid = (lo + hi + 1) >> 1
28
- if (lineStarts[mid]! <= offset) {
29
- lo = mid
30
- } else {
31
- hi = mid - 1
32
- }
33
- }
34
- return {
35
- line: lo + 1,
36
- column: offset - lineStarts[lo]!,
37
- }
38
- }
39
- }
@@ -1,181 +0,0 @@
1
- import { exec } from 'node:child_process'
2
- import { existsSync } from 'node:fs'
3
- import { join } from 'node:path'
4
- import { devtoolsEventClient } from '@tanstack/devtools-client'
5
- import chalk from 'chalk'
6
- import { readPackageJson, tryParseJson } from './utils'
7
- import { injectPluginIntoFile } from './inject-plugin'
8
- import type { OutdatedDeps } from '@tanstack/devtools-client'
9
-
10
- /**
11
- * Gets the outdated command for the detected package manager
12
- */
13
- const getOutdatedCommand = (packageManager: string): string => {
14
- switch (packageManager) {
15
- case 'yarn':
16
- return 'yarn outdated --json'
17
- case 'pnpm':
18
- return 'pnpm outdated --format json'
19
- case 'bun':
20
- return 'bun outdated --json'
21
- case 'npm':
22
- default:
23
- return 'npm outdated --json'
24
- }
25
- }
26
-
27
- /**
28
- * Adds a plugin to the devtools configuration file
29
- */
30
- export const addPluginToDevtools = (
31
- devtoolsFileId: string | null,
32
- packageName: string,
33
- pluginName: string,
34
- pluginImport?: { importName: string; type: 'jsx' | 'function' },
35
- ): { success: boolean; error?: string } => {
36
- // Check if we found the devtools file
37
- if (!devtoolsFileId) {
38
- const error = 'Devtools file not found'
39
- console.log(
40
- chalk.yellowBright(
41
- `[@tanstack/devtools-vite] Could not add plugin. ${error}.`,
42
- ),
43
- )
44
- return { success: false, error }
45
- }
46
-
47
- // Inject the plugin into the file
48
- const result = injectPluginIntoFile(devtoolsFileId, {
49
- packageName,
50
- pluginName,
51
- pluginImport,
52
- })
53
-
54
- if (result.success) {
55
- console.log(
56
- chalk.greenBright(
57
- `[@tanstack/devtools-vite] Successfully added ${packageName} to devtools!`,
58
- ),
59
- )
60
- } else {
61
- console.log(
62
- chalk.yellowBright(
63
- `[@tanstack/devtools-vite] Could not add plugin: ${result.error}`,
64
- ),
65
- )
66
- }
67
-
68
- return result
69
- }
70
- /**
71
- * Gets the install command for the detected package manager
72
- */
73
- const getInstallCommand = (
74
- packageManager: string,
75
- packageName: string,
76
- ): string => {
77
- switch (packageManager) {
78
- case 'yarn':
79
- return `yarn add -D ${packageName}`
80
- case 'pnpm':
81
- return `pnpm add -D ${packageName}`
82
- case 'bun':
83
- return `bun add -D ${packageName}`
84
- case 'npm':
85
- default:
86
- return `npm install -D ${packageName}`
87
- }
88
- }
89
-
90
- export const installPackage = async (
91
- packageName: string,
92
- ): Promise<{
93
- success: boolean
94
- error?: string
95
- }> => {
96
- return new Promise((resolve) => {
97
- const packageManager = detectPackageManager()
98
- const installCommand = getInstallCommand(packageManager, packageName)
99
-
100
- console.log(
101
- chalk.blueBright(
102
- `[@tanstack/devtools-vite] Installing ${packageName}...`,
103
- ),
104
- )
105
-
106
- exec(installCommand, async (installError) => {
107
- if (installError) {
108
- console.error(
109
- chalk.redBright(
110
- `[@tanstack/devtools-vite] Failed to install ${packageName}:`,
111
- ),
112
- installError.message,
113
- )
114
- resolve({
115
- success: false,
116
- error: installError.message,
117
- })
118
- return
119
- }
120
-
121
- console.log(
122
- chalk.greenBright(
123
- `[@tanstack/devtools-vite] Successfully installed ${packageName}`,
124
- ),
125
- )
126
-
127
- // Read the updated package.json and emit the event
128
- const updatedPackageJson = await readPackageJson()
129
- devtoolsEventClient.emit('package-json-updated', {
130
- packageJson: updatedPackageJson,
131
- })
132
-
133
- resolve({ success: true })
134
- })
135
- })
136
- }
137
-
138
- /**
139
- * Detects the package manager used in the project by checking for lock files
140
- */
141
- const detectPackageManager = (): 'npm' | 'yarn' | 'pnpm' | 'bun' => {
142
- const cwd = process.cwd()
143
-
144
- // Check for lock files in order of specificity
145
- if (existsSync(join(cwd, 'bun.lockb')) || existsSync(join(cwd, 'bun.lock'))) {
146
- return 'bun'
147
- }
148
- if (existsSync(join(cwd, 'pnpm-lock.yaml'))) {
149
- return 'pnpm'
150
- }
151
- if (existsSync(join(cwd, 'yarn.lock'))) {
152
- return 'yarn'
153
- }
154
- if (existsSync(join(cwd, 'package-lock.json'))) {
155
- return 'npm'
156
- }
157
-
158
- // Default to pnpm if no lock file is found
159
- return 'pnpm'
160
- }
161
-
162
- export const emitOutdatedDeps = async () => {
163
- return await new Promise<OutdatedDeps | null>((resolve) => {
164
- const packageManager = detectPackageManager()
165
- const outdatedCommand = getOutdatedCommand(packageManager)
166
-
167
- exec(outdatedCommand, (_, stdout) => {
168
- // outdated commands exit with code 1 if there are outdated packages, but still output valid JSON
169
- if (stdout) {
170
- const newOutdatedDeps = tryParseJson<OutdatedDeps>(stdout)
171
- if (!newOutdatedDeps) {
172
- return
173
- }
174
- devtoolsEventClient.emit('outdated-deps-read', {
175
- outdatedDeps: newOutdatedDeps,
176
- })
177
- resolve(newOutdatedDeps)
178
- }
179
- })
180
- })
181
- }