devtools/docs/source-inspector.md
Keven Arroyo 7be3776b86
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>
2026-05-13 16:13:22 +02:00

3.5 KiB

title id
Source Inspector source-inspector

The source inspector lets you click any element in your app to open its source file in your editor. When activated, the devtools overlay highlights elements as you hover over them and shows their source file location. Click to open the file at the exact line.

Requirements

Two things are needed for the source inspector to work:

  • The @tanstack/devtools-vite plugin must be installed and running (dev server only)
  • Source injection must be enabled: injectSource.enabled: true (this is the default)

The feature only works in development. In production builds, source attributes are not injected.

How It Works

flowchart LR
    A["Your JSX/TSX files"] -- "AST transform" --> B["data-tsd-source<br/>attributes injected"]
    B -- "Hold inspect hotkey<br/>+ click element" --> C["Devtools reads<br/>data-tsd-source"]
    C -- "HTTP request" --> D["Vite dev server"]
    D -- "launch-editor" --> E["Opens file in editor<br/>at exact line"]

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.

Activating the Inspector

There are two ways to activate the source inspector:

  • Hotkey: Hold Shift+Alt+Ctrl (or Shift+Alt+Meta on Mac) — this is the default inspectHotkey. While held, the inspector overlay appears.
  • Settings panel: The inspect hotkey can be customized in the devtools Settings tab.

The hotkey can also be configured programmatically:

<TanStackDevtools
  config={{
    inspectHotkey: ['Shift', 'Alt', 'CtrlOrMeta'],
  }}
/>

Ignoring Files and Components

Not all elements need source attributes. Use the ignore config to exclude files or components:

import { devtools } from '@tanstack/devtools-vite'

export default {
  plugins: [
    devtools({
      injectSource: {
        enabled: true,
        ignore: {
          files: ['node_modules', /.*\.test\.(js|ts|jsx|tsx)$/],
          components: ['InternalComponent', /.*Provider$/],
        },
      },
    }),
  ],
}

Both files and components accept arrays of strings (exact match) or RegExp patterns.

Click Action

By default, clicking an inspected element opens the file in your editor. You can change this to copy the source path to the clipboard instead using the sourceAction setting:

<TanStackDevtools
  config={{
    sourceAction: 'copy-path',
  }}
/>
Value Behavior
"ide-warp" Opens the file in your editor at the exact line (default)
"copy-path" Copies the filepath:line:column string to the clipboard

This is useful in environments where the Vite dev server cannot reach your editor, or when you want to paste the path elsewhere.

Editor Configuration

Most popular editors work out of the box via the launch-editor package. Supported editors include VS Code, WebStorm, Sublime Text, Atom, and more (full list).

For unsupported editors, use the editor config:

devtools({
  editor: {
    name: 'My Editor',
    open: async (path, lineNumber, columnNumber) => {
      const { exec } = await import('node:child_process')
      exec(`myeditor --goto "${path}:${lineNumber}:${columnNumber}"`)
    },
  },
})