@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.
Files changed (55) 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 +18 -27
  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 +48 -81
  11. package/dist/esm/enhance-logs.js.map +1 -1
  12. package/dist/esm/index.js +1 -5
  13. package/dist/esm/inject-plugin.d.ts +5 -4
  14. package/dist/esm/inject-plugin.js +171 -220
  15. package/dist/esm/inject-plugin.js.map +1 -1
  16. package/dist/esm/inject-source.d.ts +4 -1
  17. package/dist/esm/inject-source.js +111 -205
  18. package/dist/esm/inject-source.js.map +1 -1
  19. package/dist/esm/matcher.js +11 -16
  20. package/dist/esm/matcher.js.map +1 -1
  21. package/dist/esm/offset-to-loc.d.ts +14 -0
  22. package/dist/esm/offset-to-loc.js +29 -0
  23. package/dist/esm/offset-to-loc.js.map +1 -0
  24. package/dist/esm/package-manager.js +90 -131
  25. package/dist/esm/package-manager.js.map +1 -1
  26. package/dist/esm/plugin.js +329 -448
  27. package/dist/esm/plugin.js.map +1 -1
  28. package/dist/esm/remove-devtools.d.ts +4 -1
  29. package/dist/esm/remove-devtools.js +93 -134
  30. package/dist/esm/remove-devtools.js.map +1 -1
  31. package/dist/esm/utils.js +149 -162
  32. package/dist/esm/utils.js.map +1 -1
  33. package/dist/esm/virtual-console.js +170 -14
  34. package/dist/esm/virtual-console.js.map +1 -1
  35. package/package.json +4 -10
  36. package/skills/devtools-vite-plugin/SKILL.md +7 -7
  37. package/skills/devtools-vite-plugin/references/vite-options.md +2 -2
  38. package/src/ast-utils.ts +54 -0
  39. package/src/devtools-packages.ts +12 -0
  40. package/src/enhance-logs.ts +48 -78
  41. package/src/inject-plugin.test.ts +200 -147
  42. package/src/inject-plugin.ts +195 -238
  43. package/src/inject-source.test.ts +165 -115
  44. package/src/inject-source.ts +183 -231
  45. package/src/offset-to-loc.ts +39 -0
  46. package/src/plugin.ts +35 -29
  47. package/src/remove-devtools.test.ts +76 -57
  48. package/src/remove-devtools.ts +160 -168
  49. package/src/virtual-console.test.ts +172 -1
  50. package/src/virtual-console.ts +146 -7
  51. package/dist/esm/babel.d.ts +0 -18
  52. package/dist/esm/babel.js +0 -19
  53. package/dist/esm/babel.js.map +0 -1
  54. package/dist/esm/index.js.map +0 -1
  55. package/src/babel.ts +0 -18
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/devtools-vite",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "TanStack Vite plugin used to enhance the core devtools with additional functionalities",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -46,23 +46,17 @@
46
46
  "vite": "^6.0.0 || ^7.0.0 || ^8.0.0"
47
47
  },
48
48
  "dependencies": {
49
- "@babel/core": "^7.28.4",
50
- "@babel/generator": "^7.28.3",
51
- "@babel/parser": "^7.28.4",
52
- "@babel/traverse": "^7.28.4",
53
- "@babel/types": "^7.28.4",
54
49
  "chalk": "^5.6.2",
55
50
  "launch-editor": "^2.11.1",
51
+ "magic-string": "^0.30.0",
52
+ "oxc-parser": "^0.120.0",
56
53
  "picomatch": "^4.0.3",
57
54
  "@tanstack/devtools-client": "0.0.6",
58
55
  "@tanstack/devtools-event-bus": "0.4.1"
59
56
  },
60
57
  "devDependencies": {
61
- "@types/babel__core": "^7.20.5",
62
- "@types/babel__generator": "^7.27.0",
63
- "@types/babel__traverse": "^7.28.0",
64
58
  "@types/picomatch": "^4.0.2",
65
- "happy-dom": "^18.0.1"
59
+ "happy-dom": "^20.0.0"
66
60
  },
67
61
  "scripts": {
68
62
  "clean": "premove ./build ./dist",
@@ -67,13 +67,13 @@ From `packages/devtools-vite/src/index.ts`:
67
67
 
68
68
  | Sub-plugin name | What it does | When active |
69
69
  | --------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------- |
70
- | `@tanstack/devtools:inject-source` | Babel transform adding `data-tsd-source` attrs to JSX | dev mode + `injectSource.enabled` |
70
+ | `@tanstack/devtools:inject-source` | AST transform adding `data-tsd-source` attrs to JSX | dev mode + `injectSource.enabled` |
71
71
  | `@tanstack/devtools:config` | Reserved for future config modifications | serve command only |
72
72
  | `@tanstack/devtools:custom-server` | Starts ServerEventBus, registers middleware for open-source/console-pipe endpoints | dev mode |
73
73
  | `@tanstack/devtools:remove-devtools-on-build` | Strips devtools imports/JSX from production bundles | build command or production mode + `removeDevtoolsOnBuild` |
74
74
  | `@tanstack/devtools:event-client-setup` | Marketplace: listens for install/add-plugin events via devtoolsEventClient | dev mode + serve + not CI |
75
75
  | `@tanstack/devtools:console-pipe-transform` | Injects runtime console-pipe code into entry files | dev mode + serve + `consolePiping.enabled` |
76
- | `@tanstack/devtools:better-console-logs` | Babel transform prepending source location to `console.log`/`console.error` | dev mode + `enhancedLogs.enabled` |
76
+ | `@tanstack/devtools:better-console-logs` | AST transform prepending source location to `console.log`/`console.error` | dev mode + `enhancedLogs.enabled` |
77
77
  | `@tanstack/devtools:inject-plugin` | Detects which file imports TanStackDevtools (for marketplace injection) | dev mode + serve |
78
78
  | `@tanstack/devtools:connection-injection` | Replaces `__TANSTACK_DEVTOOLS_PORT__`, `__TANSTACK_DEVTOOLS_HOST__`, `__TANSTACK_DEVTOOLS_PROTOCOL__` placeholders | dev mode + serve |
79
79
 
@@ -81,7 +81,7 @@ From `packages/devtools-vite/src/index.ts`:
81
81
 
82
82
  ### Source Injection
83
83
 
84
- Adds `data-tsd-source="<relative-path>:<line>:<column>"` attributes to every JSX opening element via Babel. This powers the "Go to Source" feature -- hold the inspect hotkey (default: Shift+Alt+Ctrl/Meta), hover over elements, click to open in editor.
84
+ Adds `data-tsd-source="<relative-path>:<line>:<column>"` attributes to every JSX opening element via oxc-parser + MagicString. This powers the "Go to Source" feature -- hold the inspect hotkey (default: Shift+Alt+Ctrl/Meta), hover over elements, click to open in editor.
85
85
 
86
86
  **Key behaviors:**
87
87
 
@@ -137,7 +137,7 @@ devtools({
137
137
 
138
138
  ### Enhanced Logging
139
139
 
140
- Babel transform that prepends source location info to `console.log()` and `console.error()` calls. In the browser, this renders as a clickable "Go to Source" link. On the server, it shows `LOG <path>:<line>:<column>` in chalk colors.
140
+ AST transform that prepends source location info to `console.log()` and `console.error()` calls. In the browser, this renders as a clickable "Go to Source" link. On the server, it shows `LOG <path>:<line>:<column>` in chalk colors.
141
141
 
142
142
  The transform inserts a spread of a conditional expression: `...(typeof window === 'undefined' ? serverLogMessage : browserLogMessage)` as the first argument of the console call.
143
143
 
@@ -261,7 +261,7 @@ Source injection, console piping, enhanced logging, the server event bus, and th
261
261
 
262
262
  ### 4. Source injection on spread-props elements (MEDIUM)
263
263
 
264
- The Babel transform in `inject-source.ts` explicitly skips any JSX element that has a `{...props}` spread where `props` is the component's parameter name. This is intentional -- the spread would overwrite the injected `data-tsd-source` attribute. If source inspection doesn't work for a specific component, check if it spreads its props parameter.
264
+ The AST transform in `inject-source.ts` explicitly skips any JSX element that has a `{...props}` spread where `props` is the component's parameter name. This is intentional -- the spread would overwrite the injected `data-tsd-source` attribute. If source inspection doesn't work for a specific component, check if it spreads its props parameter.
265
265
 
266
266
  ```tsx
267
267
  // data-tsd-source will NOT be injected on <div> here
@@ -301,8 +301,8 @@ These are registered on the Vite dev server (not the event bus server):
301
301
  ## Key Source Files
302
302
 
303
303
  - `packages/devtools-vite/src/plugin.ts` -- Main plugin factory with all sub-plugins and config type
304
- - `packages/devtools-vite/src/inject-source.ts` -- Babel transform for data-tsd-source injection
305
- - `packages/devtools-vite/src/enhance-logs.ts` -- Babel transform for enhanced console logs
304
+ - `packages/devtools-vite/src/inject-source.ts` -- AST transform for data-tsd-source injection
305
+ - `packages/devtools-vite/src/enhance-logs.ts` -- AST transform for enhanced console logs
306
306
  - `packages/devtools-vite/src/remove-devtools.ts` -- Production stripping transform
307
307
  - `packages/devtools-vite/src/virtual-console.ts` -- Console pipe runtime code generator
308
308
  - `packages/devtools-vite/src/editor.ts` -- Editor config type and launch-editor integration
@@ -43,7 +43,7 @@ declare function defineDevtoolsConfig(
43
43
 
44
44
  ## `injectSource`
45
45
 
46
- Controls source injection -- the Babel transform that adds `data-tsd-source` attributes to JSX elements for the "Go to Source" feature.
46
+ Controls source injection -- the AST transform that adds `data-tsd-source` attributes to JSX elements for the "Go to Source" feature.
47
47
 
48
48
  | Field | Type | Default | Description |
49
49
  | ------------------- | ------------------------- | ----------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
@@ -117,7 +117,7 @@ devtools({
117
117
 
118
118
  ## `enhancedLogs`
119
119
 
120
- Controls the Babel transform that prepends source location information to `console.log()` and `console.error()` calls.
120
+ Controls the AST transform that prepends source location information to `console.log()` and `console.error()` calls.
121
121
 
122
122
  | Field | Type | Default | Description |
123
123
  | --------- | --------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
@@ -0,0 +1,54 @@
1
+ import type { Node } from 'oxc-parser'
2
+
3
+ /**
4
+ * Cache of keys that hold child nodes (objects/arrays) per AST node type.
5
+ * Since oxc-parser produces AST via JSON.parse, every instance of a given
6
+ * node type has the same set of keys, so we only need to discover them once.
7
+ */
8
+ const childKeysCache = new Map<string, Array<string>>()
9
+
10
+ function getChildKeys(node: Node): Array<string> {
11
+ let keys = childKeysCache.get(node.type)
12
+ if (keys) return keys
13
+
14
+ keys = []
15
+ for (const key in node) {
16
+ if (key === 'type' || key === 'start' || key === 'end') continue
17
+ // typeof null === 'object', so nullable node fields get cached too
18
+ if (typeof (node as any)[key] === 'object') {
19
+ keys.push(key)
20
+ }
21
+ }
22
+ childKeysCache.set(node.type, keys)
23
+ return keys
24
+ }
25
+
26
+ /**
27
+ * Iterate over the direct child nodes of an AST node.
28
+ * Uses a per-type cache of which keys hold child nodes to avoid
29
+ * allocating Object.entries() arrays on every call.
30
+ */
31
+ export function forEachChild(node: Node, callback: (child: Node) => void) {
32
+ const keys = getChildKeys(node)
33
+ for (const key of keys) {
34
+ const value = (node as any)[key]
35
+ if (value === null) continue
36
+ if (Array.isArray(value)) {
37
+ for (const item of value) {
38
+ if (typeof item === 'object' && item !== null && 'type' in item) {
39
+ callback(item)
40
+ }
41
+ }
42
+ } else if ('type' in value) {
43
+ callback(value as Node)
44
+ }
45
+ }
46
+ }
47
+
48
+ /**
49
+ * Recursively walk AST nodes, calling `visitor` for each node with a `type`.
50
+ */
51
+ export function walk(node: Node, visitor: (node: Node) => void) {
52
+ visitor(node)
53
+ forEachChild(node, (child) => walk(child, visitor))
54
+ }
@@ -0,0 +1,12 @@
1
+ export const TANSTACK_DEVTOOLS_PACKAGES = [
2
+ '@tanstack/react-devtools',
3
+ '@tanstack/preact-devtools',
4
+ '@tanstack/solid-devtools',
5
+ '@tanstack/vue-devtools',
6
+ '@tanstack/svelte-devtools',
7
+ '@tanstack/angular-devtools',
8
+ '@tanstack/devtools',
9
+ ] as const
10
+
11
+ export const isTanStackDevtoolsImport = (source: string): boolean =>
12
+ (TANSTACK_DEVTOOLS_PACKAGES as ReadonlyArray<string>).includes(source)
@@ -1,100 +1,70 @@
1
1
  import chalk from 'chalk'
2
2
  import { normalizePath } from 'vite'
3
- import { gen, parse, t, trav } from './babel'
4
- import type { types as Babel } from '@babel/core'
5
- import type { ParseResult } from '@babel/parser'
3
+ import MagicString from 'magic-string'
4
+ import { parseSync } from 'oxc-parser'
5
+ import { createLocMapper } from './offset-to-loc'
6
+ import { walk } from './ast-utils'
6
7
 
7
- const transform = (
8
- ast: ParseResult<Babel.File>,
9
- filePath: string,
10
- port: number,
11
- ) => {
12
- let didTransform = false
8
+ function escapeForStringLiteral(str: string): string {
9
+ return str.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n')
10
+ }
11
+
12
+ export function enhanceConsoleLog(code: string, id: string, port: number) {
13
+ const filePath = id.split('?')[0]!
14
+ const location = filePath.replace(normalizePath(process.cwd()), '')
15
+
16
+ try {
17
+ const result = parseSync(filePath, code, {
18
+ sourceType: 'module',
19
+ lang: 'tsx',
20
+ })
21
+ if (result.errors.length > 0) return
22
+
23
+ const offsetToLoc = createLocMapper(code)
24
+ const s = new MagicString(code)
13
25
 
14
- trav(ast, {
15
- CallExpression(path) {
16
- const callee = path.node.callee
17
- // Match console.log(...) or console.error(...)
26
+ walk(result.program, (node) => {
27
+ if (node.type !== 'CallExpression') return
28
+
29
+ const callee = node.callee
18
30
  if (
19
31
  callee.type === 'MemberExpression' &&
32
+ !callee.computed &&
20
33
  callee.object.type === 'Identifier' &&
21
34
  callee.object.name === 'console' &&
22
35
  callee.property.type === 'Identifier' &&
23
36
  (callee.property.name === 'log' || callee.property.name === 'error')
24
37
  ) {
25
- const location = path.node.loc
26
- if (!location) {
27
- return
28
- }
29
- const [lineNumber, column] = [
30
- location.start.line,
31
- location.start.column,
32
- ]
33
- const finalPath = `${filePath}:${lineNumber}:${column + 1}`
38
+ const loc = offsetToLoc(node.start)
39
+ const [lineNumber, column] = [loc.line, loc.column]
40
+ const finalPath = `${location}:${lineNumber}:${column + 1}`
34
41
  const logMessage = `${chalk.magenta('LOG')} ${chalk.blueBright(`${finalPath}`)}\n → `
35
42
 
36
- const serverLogMessage = t.arrayExpression([
37
- t.stringLiteral(logMessage),
38
- ])
39
- const browserLogMessage = t.arrayExpression([
40
- // LOG with css formatting specifiers: %c
41
- t.stringLiteral(
42
- `%c${'LOG'}%c %c${`Go to Source: http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(finalPath)}`}%c \n → `,
43
- ),
44
- // magenta
45
- t.stringLiteral('color:#A0A'),
46
- t.stringLiteral('color:#FFF'),
47
- // blueBright
48
- t.stringLiteral('color:#55F'),
49
- t.stringLiteral('color:#FFF'),
50
- ])
43
+ const serverLogMessage = `["${escapeForStringLiteral(logMessage)}"]`
44
+ const browserLogMessage = `["%c${'LOG'}%c %c${`Go to Source: http://localhost:${port}/__tsd/open-source?source=${encodeURIComponent(finalPath)}`}%c \\n \\u2192 ","color:#A0A","color:#FFF","color:#55F","color:#FFF"]`
51
45
 
52
- // typeof window === "undefined"
53
- const checkServerCondition = t.binaryExpression(
54
- '===',
55
- t.unaryExpression('typeof', t.identifier('window')),
56
- t.stringLiteral('undefined'),
57
- )
46
+ const spreadStr = `...(typeof window === "undefined" ? ${serverLogMessage} : ${browserLogMessage}), `
58
47
 
59
- // ...(isServer ? serverLogMessage : browserLogMessage)
60
- path.node.arguments.unshift(
61
- t.spreadElement(
62
- t.conditionalExpression(
63
- checkServerCondition,
64
- serverLogMessage,
65
- browserLogMessage,
66
- ),
67
- ),
68
- )
69
-
70
- didTransform = true
48
+ // Find the opening '(' of the call by scanning forward from callee end
49
+ let parenOffset = callee.end
50
+ while (parenOffset < code.length && code[parenOffset] !== '(') {
51
+ parenOffset++
52
+ }
53
+ // Insert right after '('
54
+ s.appendRight(parenOffset + 1, spreadStr)
71
55
  }
72
- },
73
- })
74
-
75
- return didTransform
76
- }
56
+ })
77
57
 
78
- export function enhanceConsoleLog(code: string, id: string, port: number) {
79
- const [filePath] = id.split('?')
80
- // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
81
- const location = filePath?.replace(normalizePath(process.cwd()), '')!
58
+ if (!s.hasChanged()) return
82
59
 
83
- try {
84
- const ast = parse(code, {
85
- sourceType: 'module',
86
- plugins: ['jsx', 'typescript'],
87
- })
88
- const didTransform = transform(ast, location, port)
89
- if (!didTransform) {
90
- return
60
+ return {
61
+ code: s.toString(),
62
+ map: s.generateMap({
63
+ source: filePath,
64
+ file: id,
65
+ includeContent: true,
66
+ }),
91
67
  }
92
- return gen(ast, {
93
- sourceMaps: true,
94
- retainLines: true,
95
- filename: id,
96
- sourceFileName: filePath,
97
- })
98
68
  } catch (e) {
99
69
  return
100
70
  }