Skip to content
Source
Sign inSign up
TanStack/devtools
Open History to see the last commit

vite-plugin.md

250 lines7.7 KB

1---
2title: Vite Plugin
3id: vite-plugin
4---
5​
6The Vite Plugin for TanStack Devtools provides a seamless integration for using the devtools in your Vite-powered applications. With this plugin, you get complementary features on top of the
7existing features built into the devtools like better console logs, server event bus, and enhanced debugging capabilities.
8​
9## Installation
10​
11To add the devtools vite plugin you need to install it as a development dependency:
12​
13```sh
14npm install -D @tanstack/devtools-vite
15```
16​
17Then add it as the *FIRST* plugin in your Vite config:
18​
19```ts
20import { devtools } from '@tanstack/devtools-vite'
21​
22export default {
23 plugins: [
24 devtools(),
25 // ... rest of your plugins here
26 ],
28```
29​
30And you're done!
31​
32## Configuration
33​
34You can configure the devtools plugin by passing options to the `devtools` function:
35​
36```ts
37import { devtools } from '@tanstack/devtools-vite'
38​
39export default {
40 plugins: [
41 devtools({
42 // options here
43 }),
44 // ... rest of your plugins here
45 ],
47```
48​
49### eventBusConfig
50​
51 Configuration for the event bus that the devtools use to communicate with the client
52​
53```ts
54import { devtools } from '@tanstack/devtools-vite'
55​
56export default {
57 plugins: [
58 devtools({
59 eventBusConfig: {
60 // port to run the event bus on
61 port: 1234,
62 // console log debug logs or not
63 debug: false,
64 // enables the server event bus (defaults to true), you can disable it if you're running devtools in something like storybook or vitest
65 enabled: true
66 },
67 }),
68 // ... rest of your plugins here
69 ],
71​
72```
73​
74### editor
75​
76> [!IMPORTANT]
77> `editor` is used as an escape hatch to implement your own go-to-source functionality if your system/editor does not work OOTB. We use `launch-editor` under the hood which supports a lot of editors but not all. If your editor is not supported you can implement your own version here. Here is the list of supported editors: https://github.com/yyx990803/launch-editor?tab=readme-ov-file#supported-editors
78​
79The open in editor configuration which has two fields, `name` and `open`,
80`name` is the name of your editor, and `open` is a function that opens the editor with the given file and line number. You can implement your version for your editor as follows:
81​
82 ```ts
83import { devtools } from '@tanstack/devtools-vite'
84​
85export default {
86 plugins: [
87 devtools({
88 editor: {
89 name: 'VSCode',
90 open: async (path, lineNumber, columnNumber) => {
91 const { exec } = await import('node:child_process')
92 exec(
93 // or windsurf/cursor/webstorm
94 `code -g "${(path).replaceAll('$', '\\$')}${lineNumber ? `:${lineNumber}` : ''}${columnNumber ? `:${columnNumber}` : ''}"`,
95 )
96 },
97 },
98 }),
99 // ... rest of your plugins here
100 ],
102​
103```
104​
105### enhancedLogs
106​
107 Configuration for enhanced logging. Defaults to enabled.
108​
109```ts
110import { devtools } from '@tanstack/devtools-vite'
111​
112export default {
113 plugins: [
114 devtools({
115 enhancedLogs: {
116 enabled: true
117 }
118 }),
119 // ... rest of your plugins here
120 ],
122```
123​
124### removeDevtoolsOnBuild
125​
126Whether to remove devtools from the production build. Defaults to true.
127​
128```ts
129import { devtools } from '@tanstack/devtools-vite'
130​
131export default {
132 plugins: [
133 devtools({
134 removeDevtoolsOnBuild: true
135 }),
136 // ... rest of your plugins here
137 ],
139```
140​
141### logging
142 Whether to log information to the console. Defaults to true.
143​
144```ts
145import { devtools } from '@tanstack/devtools-vite'
146​
147export default {
148 plugins: [
149 devtools({
150 logging: true
151 }),
152 // ... rest of your plugins here
153 ],
155```
156​
157### injectSource
158​
159Configuration for source injection. Defaults to enabled.
160​
161​
162```ts
163import { devtools } from '@tanstack/devtools-vite'
164​
165export default {
166 plugins: [
167 devtools({
168 injectSource: {
169 enabled: true,
170 ignore: {
171 // files to ignore source injection for
172 files: ['node_modules', /.*\.test\.(js|ts|jsx|tsx)$/],
173 // components to ignore source injection for
174 components: ['YourComponent', /.*Lazy$/],
175 },
176 }
177 }),
178 // ... rest of your plugins here
179 ],
181```
182​
183### consolePiping
184​
185Configuration for bidirectional console piping between client and server. When enabled, console logs from the client will appear in your terminal, and server logs will appear in the browser console. Defaults to enabled.
186​
187```ts
188import { devtools } from '@tanstack/devtools-vite'
189​
190export default {
191 plugins: [
192 devtools({
193 consolePiping: {
194 // Whether to enable console piping (defaults to true)
195 enabled: true,
196 // Which console methods to pipe (defaults to all)
197 levels: ['log', 'warn', 'error', 'info', 'debug'],
198 }
199 }),
200 // ... rest of your plugins here
201 ],
203```
204​
205## Features
206​
207The Vite plugin is composed of several sub-plugins, each handling a specific concern:
208​
209```mermaid
210graph TD
211 vite["@tanstack/devtools-vite"]
212 vite --> source["Source Injection<br/><i>AST β†’ data-tsd-source attrs</i>"]
213 vite --> server["Server Event Bus<br/><i>WebSocket + SSE transport</i>"]
214 vite --> strip["Production Stripping<br/><i>Remove devtools on build</i>"]
215 vite --> pipe["Console Piping<br/><i>Client ↔ Server logs</i>"]
216 vite --> logs["Enhanced Logs<br/><i>Source location in console</i>"]
217 vite --> market["Marketplace<br/><i>Install plugins via UI</i>"]
218 vite --> connect["Connection Injection<br/><i>Port/host placeholders</i>"]
219```
220​
221### Go to Source
222​
223The "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​
225To 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.
226​
227For a complete guide on configuration and usage, see the [Source Inspector](./source-inspector) docs.
228​
229### Console Piping
230​
231When enabled (default), `console.log()` and other console methods in the browser are piped to your terminal, and server-side console output appears in the browser console. This is particularly useful when debugging SSR or API routes β€” you see all logs in one place without switching between terminal and browser. Configure which log levels are piped via `consolePiping.levels`.
232​
233### Enhanced Logs
234​
235Console logs are enhanced with clickable source locations. In the browser console, each log shows the file and line number where it originated. Click to open the source file in your editor. Enable/disable via the `enhancedLogs` config option.
236​
237### Production Build Stripping
238​
239By default (`removeDevtoolsOnBuild: true`), the Vite plugin replaces all devtools imports with empty modules in production builds. This includes:
240- `@tanstack/react-devtools`
241- `@tanstack/vue-devtools`
242- `@tanstack/solid-devtools`
243- `@tanstack/preact-devtools`
244- `@tanstack/devtools`
245​
246This ensures zero devtools code reaches production. Set `removeDevtoolsOnBuild: false` to keep devtools in production (see [Production](./production) docs).
247​
248### Plugin Marketplace
249​
250The Vite plugin enables the in-devtools plugin marketplace. When you browse available plugins in the devtools Settings tab and click "Install", the Vite plugin handles the npm/pnpm/yarn installation and automatically injects the plugin import into your devtools setup file. This only works during development with the Vite dev server running.