You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
refactor(devtools-vite): migrate from Babel to oxc-parser + MagicString (#397)
* refactor(devtools-vite): migrate from Babel to oxc-parser + MagicString
* changeset+knip fix
* docs update
* ci: apply automated fixes
* refactor(devtools-vite): address CodeRabbit feedback on oxc migration
- Extract shared TANSTACK_DEVTOOLS_PACKAGES constant; remove-devtools now
recognizes preact/vue/svelte/angular packages (previously only
react/solid/devtools were stripped).
- Escape displayName/packageName via JSON.stringify when generating
plugin injection code so quotes/backslashes do not break the output.
- Skip appending a duplicate import when the plugin symbol is already
imported from the same package.
- collectJsx no longer crosses function boundaries; inner components
keep their own propsName context and aren't annotated with an outer
function's data-tsd-source.
- Drop the redundant VariableDeclaration branch in visitFunctions;
arrow/function expressions are reached via forEachChild, so the
prior code walked them twice.
- Add regression tests covering non-ASCII source positions (emoji,
CJK) — oxc-parser napi bindings already return UTF-16 code-unit
indices, so no offset conversion is needed.
- Sync the devtools-production SKILL.md package list with the
implementation.
* chore(deps): bump oxc-parser to ^0.120.0
Verified empirically that the napi bindings continue to return UTF-16
code-unit indices (matching the assumption in inject-source.ts and the
non-ASCII regression tests). All 167 devtools-vite tests pass on the new
version; lint, typecheck, build, and knip clean.
* ci: apply automated fixes
* fix(devtools-vite): emit correct source positions for enhanced logs
console-pipe-transform prepends a multi-line IIFE to root entry files
before better-console-logs runs. Because both plugins are enforce:'pre'
and execute in array order, enhanceConsoleLog was parsing the
post-prepend source and reporting line numbers shifted past the end of
the user's file -- 'Go to Source' links pointed nowhere.
Swap the two plugin entries so enhanceConsoleLog sees the original
source. Add a regression test asserting better-console-logs is
registered before console-pipe-transform.
* fix(devtools): snapshot data-tsd-source before mutating click state
The click handler in SourceInspector called `setDisabledAfterClick(true)`
before reading `highlightState.dataSource`. That signal write flipped
`isActive` to false, triggering the highlight effect's `resetHighlight()`
synchronously enough (via the chained `createStore` reactions) to clear
`dataSource` before the subsequent reads. Result: the open-source fetch
was issued with `?source=` empty and clipboard writes wrote an empty
string.
Capture `highlightState.dataSource` into a local before any state
mutations, then use the captured value for both the clipboard path and
the URL construction.
* ci: apply automated fixes
---------
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Alem Tuzlak <t.zlak@hotmail.com>
Copy file name to clipboardExpand all lines: docs/architecture.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -194,7 +194,7 @@ Adapters do not re-implement the devtools UI, manage settings, handle events, or
194
194
`@tanstack/devtools-vite` is a collection of Vite plugins that enhance the development experience and clean up production builds. It returns an array of Vite plugins, each handling a specific concern:
Uses Babel to parse JSX/TSX files and injects `data-tsd-source` attributes on every JSX element. These attributes encode the file path, line number, and column number of each element in source code, which the source inspector feature uses to implement click-to-open-in-editor.
197
+
Parses JSX/TSX files with oxc-parser and injects `data-tsd-source` attributes on every JSX element via MagicString. These attributes encode the file path, line number, and column number of each element in source code, which the source inspector feature uses to implement click-to-open-in-editor.
198
198
199
199
### Server event bus (`@tanstack/devtools:custom-server`)
200
200
Starts a `ServerEventBus` on the Vite dev server. Also sets up middleware for the go-to-source editor integration and bidirectional console piping (client logs appear in the terminal, server logs appear in the browser).
B -- "Hold inspect hotkey<br/>+ click element" --> C["Devtools reads<br/>data-tsd-source"]
23
23
C -- "HTTP request" --> D["Vite dev server"]
24
24
D -- "launch-editor" --> E["Opens file in editor<br/>at exact line"]
25
25
```
26
26
27
-
The Vite plugin uses Babel to parse your JSX/TSX files during development. It adds a `data-tsd-source="filepath:line:column"` attribute to every JSX element. When you activate the source inspector and click an element, the devtools reads this attribute and sends a request to the Vite dev server. The server then launches your editor at the specified file and line using `launch-editor`.
27
+
The Vite plugin uses oxc-parser to parse your JSX/TSX files during development. It adds a `data-tsd-source="filepath:line:column"` attribute to every JSX element via MagicString. When you activate the source inspector and click an element, the devtools reads this attribute and sends a request to the Vite dev server. The server then launches your editor at the specified file and line using `launch-editor`.
Copy file name to clipboardExpand all lines: docs/vite-plugin.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -209,7 +209,7 @@ The Vite plugin is composed of several sub-plugins, each handling a specific con
209
209
```mermaid
210
210
graph TD
211
211
vite["@tanstack/devtools-vite"]
212
-
vite --> source["Source Injection<br/><i>Babel → data-tsd-source attrs</i>"]
212
+
vite --> source["Source Injection<br/><i>AST → data-tsd-source attrs</i>"]
213
213
vite --> server["Server Event Bus<br/><i>WebSocket + SSE transport</i>"]
214
214
vite --> strip["Production Stripping<br/><i>Remove devtools on build</i>"]
215
215
vite --> pipe["Console Piping<br/><i>Client ↔ Server logs</i>"]
@@ -220,7 +220,7 @@ graph TD
220
220
221
221
### Go to Source
222
222
223
-
The "Go to Source" feature lets you click on any element in your browser and open its source file in your editor at the exact line where it's defined. It works by injecting `data-tsd-source` attributes into your components via a Babel transformation during development. These attributes encode the file path and line number of each element.
223
+
The "Go to Source" feature lets you click on any element in your browser and open its source file in your editor at the exact line where it's defined. It works by injecting `data-tsd-source` attributes into your components via an AST transformation during development. These attributes encode the file path and line number of each element.
224
224
225
225
To use it, activate the source inspector by holding the inspect hotkey (default: Shift+Alt+Ctrl/Meta). An overlay will highlight elements under your cursor and display their source location. Clicking on a highlighted element opens the corresponding file in your editor at the exact line, powered by `launch-editor` under the hood.
|`@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 |
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
0 commit comments