@tanstack/devtools-vite 0.6.0 → 0.7.0
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/ast-utils.d.ts +11 -0
- package/dist/esm/ast-utils.js +44 -0
- package/dist/esm/ast-utils.js.map +1 -0
- package/dist/esm/devtools-packages.d.ts +2 -0
- package/dist/esm/devtools-packages.js +15 -0
- package/dist/esm/devtools-packages.js.map +1 -0
- package/dist/esm/editor.js +18 -27
- package/dist/esm/editor.js.map +1 -1
- package/dist/esm/enhance-logs.d.ts +4 -1
- package/dist/esm/enhance-logs.js +48 -81
- package/dist/esm/enhance-logs.js.map +1 -1
- package/dist/esm/index.js +1 -5
- package/dist/esm/inject-plugin.d.ts +5 -4
- package/dist/esm/inject-plugin.js +171 -220
- package/dist/esm/inject-plugin.js.map +1 -1
- package/dist/esm/inject-source.d.ts +4 -1
- package/dist/esm/inject-source.js +111 -205
- package/dist/esm/inject-source.js.map +1 -1
- package/dist/esm/matcher.js +11 -16
- package/dist/esm/matcher.js.map +1 -1
- package/dist/esm/offset-to-loc.d.ts +14 -0
- package/dist/esm/offset-to-loc.js +29 -0
- package/dist/esm/offset-to-loc.js.map +1 -0
- package/dist/esm/package-manager.js +90 -131
- package/dist/esm/package-manager.js.map +1 -1
- package/dist/esm/plugin.js +329 -448
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/remove-devtools.d.ts +4 -1
- package/dist/esm/remove-devtools.js +93 -134
- package/dist/esm/remove-devtools.js.map +1 -1
- package/dist/esm/utils.js +149 -162
- package/dist/esm/utils.js.map +1 -1
- package/dist/esm/virtual-console.js +170 -14
- package/dist/esm/virtual-console.js.map +1 -1
- package/package.json +4 -10
- package/skills/devtools-vite-plugin/SKILL.md +7 -7
- package/skills/devtools-vite-plugin/references/vite-options.md +2 -2
- package/src/ast-utils.ts +54 -0
- package/src/devtools-packages.ts +12 -0
- package/src/enhance-logs.ts +48 -78
- package/src/inject-plugin.test.ts +200 -147
- package/src/inject-plugin.ts +195 -238
- package/src/inject-source.test.ts +165 -115
- package/src/inject-source.ts +183 -231
- package/src/offset-to-loc.ts +39 -0
- package/src/plugin.ts +35 -29
- package/src/remove-devtools.test.ts +76 -57
- package/src/remove-devtools.ts +160 -168
- package/src/virtual-console.test.ts +172 -1
- package/src/virtual-console.ts +146 -7
- package/dist/esm/babel.d.ts +0 -18
- package/dist/esm/babel.js +0 -19
- package/dist/esm/babel.js.map +0 -1
- package/dist/esm/index.js.map +0 -1
- package/src/babel.ts +0 -18
package/src/inject-source.ts
CHANGED
|
@@ -1,246 +1,184 @@
|
|
|
1
1
|
import { normalizePath } from 'vite'
|
|
2
|
-
import
|
|
2
|
+
import MagicString from 'magic-string'
|
|
3
|
+
import { parseSync } from 'oxc-parser'
|
|
4
|
+
import { createLocMapper } from './offset-to-loc'
|
|
3
5
|
import { matcher } from './matcher'
|
|
4
|
-
import
|
|
5
|
-
import type {
|
|
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'
|
|
6
14
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
)
|
|
14
|
-
let propsName: string | null = null
|
|
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
|
|
15
22
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (
|
|
26
|
-
prop.type === 'RestElement' &&
|
|
27
|
-
prop.argument.type === 'Identifier'
|
|
28
|
-
) {
|
|
29
|
-
propsName = prop.argument.name
|
|
30
|
-
}
|
|
31
|
-
})
|
|
32
|
-
}
|
|
33
|
-
return propsName
|
|
34
|
-
}
|
|
35
|
-
if (functionDeclaration.type === 'ArrowFunctionExpression') {
|
|
36
|
-
const firstArgument = functionDeclaration.params[0]
|
|
37
|
-
// handles (props) => {}
|
|
38
|
-
if (firstArgument && firstArgument.type === 'Identifier') {
|
|
39
|
-
propsName = firstArgument.name
|
|
40
|
-
}
|
|
41
|
-
// handles ({ ...props }) => {}
|
|
42
|
-
if (firstArgument && firstArgument.type === 'ObjectPattern') {
|
|
43
|
-
firstArgument.properties.forEach((prop) => {
|
|
44
|
-
if (
|
|
45
|
-
prop.type === 'RestElement' &&
|
|
46
|
-
prop.argument.type === 'Identifier'
|
|
47
|
-
) {
|
|
48
|
-
propsName = prop.argument.name
|
|
49
|
-
}
|
|
50
|
-
})
|
|
51
|
-
}
|
|
52
|
-
return propsName
|
|
53
|
-
}
|
|
54
|
-
if (functionDeclaration.type === 'FunctionDeclaration') {
|
|
55
|
-
const firstArgument = functionDeclaration.params[0]
|
|
56
|
-
// handles (props) => {}
|
|
57
|
-
if (firstArgument && firstArgument.type === 'Identifier') {
|
|
58
|
-
propsName = firstArgument.name
|
|
59
|
-
}
|
|
60
|
-
// handles ({ ...props }) => {}
|
|
61
|
-
if (firstArgument && firstArgument.type === 'ObjectPattern') {
|
|
62
|
-
firstArgument.properties.forEach((prop) => {
|
|
63
|
-
if (
|
|
64
|
-
prop.type === 'RestElement' &&
|
|
65
|
-
prop.argument.type === 'Identifier'
|
|
66
|
-
) {
|
|
67
|
-
propsName = prop.argument.name
|
|
68
|
-
}
|
|
69
|
-
})
|
|
70
|
-
}
|
|
71
|
-
return propsName
|
|
72
|
-
}
|
|
73
|
-
// Arrow function case
|
|
74
|
-
if (
|
|
75
|
-
functionDeclaration.init?.type === 'ArrowFunctionExpression' ||
|
|
76
|
-
functionDeclaration.init?.type === 'FunctionExpression'
|
|
77
|
-
) {
|
|
78
|
-
const firstArgument = functionDeclaration.init.params[0]
|
|
79
|
-
// handles (props) => {}
|
|
80
|
-
if (firstArgument && firstArgument.type === 'Identifier') {
|
|
81
|
-
propsName = firstArgument.name
|
|
82
|
-
}
|
|
83
|
-
// handles ({ ...props }) => {}
|
|
84
|
-
if (firstArgument && firstArgument.type === 'ObjectPattern') {
|
|
85
|
-
firstArgument.properties.forEach((prop) => {
|
|
86
|
-
if (
|
|
87
|
-
prop.type === 'RestElement' &&
|
|
88
|
-
prop.argument.type === 'Identifier'
|
|
89
|
-
) {
|
|
90
|
-
propsName = prop.argument.name
|
|
91
|
-
}
|
|
92
|
-
})
|
|
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
|
+
}
|
|
93
32
|
}
|
|
94
33
|
}
|
|
95
|
-
|
|
34
|
+
|
|
35
|
+
return null
|
|
96
36
|
}
|
|
97
37
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
return `${getNameOfElement(element.object)}.${getNameOfElement(element.property)}`
|
|
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}`
|
|
106
45
|
}
|
|
107
|
-
|
|
108
|
-
return `${
|
|
46
|
+
// JSXNamespacedName
|
|
47
|
+
return `${name.namespace.name}:${name.name.name}`
|
|
109
48
|
}
|
|
110
49
|
|
|
111
|
-
|
|
112
|
-
|
|
50
|
+
/**
|
|
51
|
+
* Check whether a JSXOpeningElement should receive `data-tsd-source`.
|
|
52
|
+
*/
|
|
53
|
+
function shouldTransform(
|
|
54
|
+
node: JSXOpeningElement,
|
|
113
55
|
propsName: string | null,
|
|
114
|
-
file: string,
|
|
115
56
|
ignorePatterns: Array<string | RegExp>,
|
|
116
|
-
)
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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?
|
|
123
66
|
if (
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
67
|
+
node.attributes.some(
|
|
68
|
+
(a) =>
|
|
69
|
+
a.type === 'JSXAttribute' &&
|
|
70
|
+
a.name.type === 'JSXIdentifier' &&
|
|
71
|
+
a.name.name === 'data-tsd-source',
|
|
72
|
+
)
|
|
127
73
|
) {
|
|
128
|
-
return
|
|
74
|
+
return false
|
|
129
75
|
}
|
|
130
|
-
const hasDataSource = element.node.attributes.some(
|
|
131
|
-
(attr) =>
|
|
132
|
-
attr.type === 'JSXAttribute' &&
|
|
133
|
-
attr.name.type === 'JSXIdentifier' &&
|
|
134
|
-
attr.name.name === 'data-tsd-source',
|
|
135
|
-
)
|
|
136
|
-
// Check if props are spread
|
|
137
|
-
const hasSpread = element.node.attributes.some(
|
|
138
|
-
(attr) =>
|
|
139
|
-
attr.type === 'JSXSpreadAttribute' &&
|
|
140
|
-
attr.argument.type === 'Identifier' &&
|
|
141
|
-
attr.argument.name === propsName,
|
|
142
|
-
)
|
|
143
76
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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
|
|
147
87
|
}
|
|
148
88
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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'
|
|
155
97
|
)
|
|
98
|
+
}
|
|
156
99
|
|
|
157
|
-
|
|
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))
|
|
158
112
|
}
|
|
159
113
|
|
|
160
|
-
|
|
161
|
-
|
|
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>,
|
|
162
121
|
file: string,
|
|
163
122
|
ignorePatterns: Array<string | RegExp>,
|
|
164
|
-
) =>
|
|
123
|
+
offsetToLoc: (offset: number) => Loc,
|
|
124
|
+
s: MagicString,
|
|
125
|
+
code: string,
|
|
126
|
+
): boolean {
|
|
165
127
|
let didTransform = false
|
|
166
128
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
ArrowFunctionExpression(path) {
|
|
187
|
-
const propsName = getPropsNameFromFunctionDeclaration(path.node)
|
|
188
|
-
path.traverse({
|
|
189
|
-
JSXOpeningElement(element) {
|
|
190
|
-
const transformed = transformJSX(
|
|
191
|
-
element,
|
|
192
|
-
propsName,
|
|
193
|
-
file,
|
|
194
|
-
ignorePatterns,
|
|
195
|
-
)
|
|
196
|
-
if (transformed) {
|
|
197
|
-
didTransform = true
|
|
198
|
-
}
|
|
199
|
-
},
|
|
200
|
-
})
|
|
201
|
-
},
|
|
202
|
-
FunctionExpression(path) {
|
|
203
|
-
const propsName = getPropsNameFromFunctionDeclaration(path.node)
|
|
204
|
-
path.traverse({
|
|
205
|
-
JSXOpeningElement(element) {
|
|
206
|
-
const transformed = transformJSX(
|
|
207
|
-
element,
|
|
208
|
-
propsName,
|
|
209
|
-
file,
|
|
210
|
-
ignorePatterns,
|
|
211
|
-
)
|
|
212
|
-
if (transformed) {
|
|
213
|
-
didTransform = true
|
|
214
|
-
}
|
|
215
|
-
},
|
|
216
|
-
})
|
|
217
|
-
},
|
|
218
|
-
VariableDeclaration(path) {
|
|
219
|
-
const functionDeclaration = path.node.declarations.find((decl) => {
|
|
220
|
-
return (
|
|
221
|
-
decl.init?.type === 'ArrowFunctionExpression' ||
|
|
222
|
-
decl.init?.type === 'FunctionExpression'
|
|
223
|
-
)
|
|
224
|
-
})
|
|
225
|
-
if (!functionDeclaration) {
|
|
226
|
-
return
|
|
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)
|
|
227
148
|
}
|
|
228
|
-
const propsName = getPropsNameFromFunctionDeclaration(functionDeclaration)
|
|
229
149
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
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
|
+
}
|
|
244
182
|
})
|
|
245
183
|
|
|
246
184
|
return didTransform
|
|
@@ -254,29 +192,43 @@ export function addSourceToJsx(
|
|
|
254
192
|
components?: Array<string | RegExp>
|
|
255
193
|
} = {},
|
|
256
194
|
) {
|
|
257
|
-
const
|
|
258
|
-
|
|
259
|
-
const location = filePath?.replace(normalizePath(process.cwd()), '')!
|
|
195
|
+
const filePath = id.split('?')[0]!
|
|
196
|
+
const location = filePath.replace(normalizePath(process.cwd()), '')
|
|
260
197
|
|
|
261
198
|
const fileIgnored = matcher(ignore.files || [], location)
|
|
262
|
-
if (fileIgnored)
|
|
263
|
-
|
|
264
|
-
}
|
|
199
|
+
if (fileIgnored) return
|
|
200
|
+
|
|
265
201
|
try {
|
|
266
|
-
const
|
|
202
|
+
const result = parseSync(filePath, code, {
|
|
267
203
|
sourceType: 'module',
|
|
268
|
-
|
|
204
|
+
lang: 'tsx',
|
|
269
205
|
})
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
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
|
+
}),
|
|
273
231
|
}
|
|
274
|
-
return gen(ast, {
|
|
275
|
-
sourceMaps: true,
|
|
276
|
-
retainLines: true,
|
|
277
|
-
filename: id,
|
|
278
|
-
sourceFileName: filePath,
|
|
279
|
-
})
|
|
280
232
|
} catch (e) {
|
|
281
233
|
return
|
|
282
234
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
}
|
package/src/plugin.ts
CHANGED
|
@@ -12,6 +12,7 @@ import { removeDevtools } from './remove-devtools'
|
|
|
12
12
|
import { addSourceToJsx } from './inject-source'
|
|
13
13
|
import { enhanceConsoleLog } from './enhance-logs'
|
|
14
14
|
import { detectDevtoolsFile, injectPluginIntoFile } from './inject-plugin'
|
|
15
|
+
import { TANSTACK_DEVTOOLS_PACKAGES } from './devtools-packages'
|
|
15
16
|
import {
|
|
16
17
|
addPluginToDevtools,
|
|
17
18
|
emitOutdatedDeps,
|
|
@@ -223,6 +224,10 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
|
|
|
223
224
|
await editor.open(path, lineNum, columnNum)
|
|
224
225
|
}
|
|
225
226
|
|
|
227
|
+
const originalConsole = Object.fromEntries(
|
|
228
|
+
consolePipingLevels.map((l) => [l, console[l].bind(console)]),
|
|
229
|
+
) as Partial<Record<ConsoleLevel, typeof console.log>>
|
|
230
|
+
|
|
226
231
|
// SSE clients for broadcasting server logs to browser
|
|
227
232
|
const sseClients: Array<{
|
|
228
233
|
res: ServerResponse
|
|
@@ -248,7 +253,11 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
|
|
|
248
253
|
onConsolePipe: (entries) => {
|
|
249
254
|
for (const entry of entries) {
|
|
250
255
|
const prefix = chalk.cyan('[Client]')
|
|
251
|
-
const logMethod =
|
|
256
|
+
const logMethod =
|
|
257
|
+
originalConsole[entry.level as ConsoleLevel] ??
|
|
258
|
+
// log exists in consolePipingLevels
|
|
259
|
+
originalConsole.log!
|
|
260
|
+
|
|
252
261
|
const cleanedArgs = stripEnhancedLogPrefix(
|
|
253
262
|
entry.args,
|
|
254
263
|
(loc) => chalk.gray(loc),
|
|
@@ -310,17 +319,10 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
|
|
|
310
319
|
},
|
|
311
320
|
enforce: 'pre',
|
|
312
321
|
transform(code, id) {
|
|
313
|
-
const devtoolPackages = [
|
|
314
|
-
'@tanstack/react-devtools',
|
|
315
|
-
'@tanstack/preact-devtools',
|
|
316
|
-
'@tanstack/solid-devtools',
|
|
317
|
-
'@tanstack/vue-devtools',
|
|
318
|
-
'@tanstack/devtools',
|
|
319
|
-
]
|
|
320
322
|
if (
|
|
321
323
|
id.includes('node_modules') ||
|
|
322
324
|
id.includes('?raw') ||
|
|
323
|
-
!
|
|
325
|
+
!TANSTACK_DEVTOOLS_PACKAGES.some((pkg) => code.includes(pkg))
|
|
324
326
|
)
|
|
325
327
|
return
|
|
326
328
|
const transform = removeDevtools(code, id)
|
|
@@ -538,6 +540,30 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
|
|
|
538
540
|
}
|
|
539
541
|
},
|
|
540
542
|
},
|
|
543
|
+
// Enhanced logs must run BEFORE console-pipe-transform: the latter
|
|
544
|
+
// prepends a multi-line IIFE to root entry files, which would shift
|
|
545
|
+
// every line number computed by the oxc-parser AST and produce
|
|
546
|
+
// "Go to Source" links pointing past the end of the user's file.
|
|
547
|
+
{
|
|
548
|
+
name: '@tanstack/devtools:better-console-logs',
|
|
549
|
+
enforce: 'pre',
|
|
550
|
+
apply(config) {
|
|
551
|
+
return config.mode === 'development' && enhancedLogsConfig.enabled
|
|
552
|
+
},
|
|
553
|
+
transform: {
|
|
554
|
+
filter: {
|
|
555
|
+
id: {
|
|
556
|
+
exclude: [/node_modules/, /\?raw/, /\/dist\//, /\/build\//],
|
|
557
|
+
},
|
|
558
|
+
code: {
|
|
559
|
+
include: 'console.',
|
|
560
|
+
},
|
|
561
|
+
},
|
|
562
|
+
handler(code, id) {
|
|
563
|
+
return enhanceConsoleLog(code, id, port)
|
|
564
|
+
},
|
|
565
|
+
},
|
|
566
|
+
},
|
|
541
567
|
// Inject console piping code into entry files (both client and server)
|
|
542
568
|
{
|
|
543
569
|
name: '@tanstack/devtools:console-pipe-transform',
|
|
@@ -584,26 +610,6 @@ export const devtools = (args?: TanStackDevtoolsViteConfig): Array<Plugin> => {
|
|
|
584
610
|
},
|
|
585
611
|
},
|
|
586
612
|
},
|
|
587
|
-
{
|
|
588
|
-
name: '@tanstack/devtools:better-console-logs',
|
|
589
|
-
enforce: 'pre',
|
|
590
|
-
apply(config) {
|
|
591
|
-
return config.mode === 'development' && enhancedLogsConfig.enabled
|
|
592
|
-
},
|
|
593
|
-
transform: {
|
|
594
|
-
filter: {
|
|
595
|
-
id: {
|
|
596
|
-
exclude: [/node_modules/, /\?raw/, /\/dist\//, /\/build\//],
|
|
597
|
-
},
|
|
598
|
-
code: {
|
|
599
|
-
include: 'console.',
|
|
600
|
-
},
|
|
601
|
-
},
|
|
602
|
-
handler(code, id) {
|
|
603
|
-
return enhanceConsoleLog(code, id, port)
|
|
604
|
-
},
|
|
605
|
-
},
|
|
606
|
-
},
|
|
607
613
|
{
|
|
608
614
|
name: '@tanstack/devtools:inject-plugin',
|
|
609
615
|
apply(config, { command }) {
|