Skip to content

Commit 2311334

Browse files
committed
docs: expand the docs
1 parent bd22d46 commit 2311334

5 files changed

Lines changed: 1095 additions & 266 deletions

File tree

docs/kit/devtools-plugin.md

Lines changed: 182 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,210 @@ A DevTools plugin is a **superset** of a Vite plugin—meaning any Vite plugin c
88

99
## Installation
1010

11-
To get started, install the `@vitejs/devtools-kit` package in your Vite plugin project. It's typically fine to add it as a dev dependency since we only need it for types on the Node.js side.
11+
Install the `@vitejs/devtools-kit` package in your Vite plugin project:
1212

13-
```zsh
14-
pnpm install -D @vitejs/devtools-kit
13+
::: code-group
14+
15+
```bash [pnpm]
16+
pnpm add -D @vitejs/devtools-kit
17+
```
18+
19+
```bash [npm]
20+
npm install -D @vitejs/devtools-kit
1521
```
1622

17-
Then referencing it in your plugin code, it will augment the `Plugin` interface with the `devtools` property.
23+
```bash [yarn]
24+
yarn add -D @vitejs/devtools-kit
25+
```
26+
27+
:::
28+
29+
> [!NOTE]
30+
> It's typically fine to add it as a dev dependency since we only need it for types on the Node.js side.
31+
32+
## Basic Setup
1833

19-
Inside `devtools.setup`, you will get tools to register custom data visualization, actions, and more. See the sections below for details on [registering dock entries](./dock-system), and [RPC functions](./rpc).
34+
Add the triple-slash reference to augment Vite's `Plugin` interface with the `devtools` property:
2035

21-
```ts {1,9-14}
36+
```ts
2237
/// <reference types="@vitejs/devtools-kit" />
23-
import { Plugin } from 'vite'
38+
import type { Plugin } from 'vite'
2439

2540
export default function myPlugin(): Plugin {
2641
return {
2742
name: 'my-plugin',
28-
// Do other plugin stuff...
29-
transform(code, id) {},
30-
// Devtools setup
43+
44+
// Regular Vite plugin hooks
45+
configResolved(config) {
46+
// ...
47+
},
48+
49+
// DevTools setup - only called when DevTools is enabled
3150
devtools: {
3251
setup(ctx) {
33-
console.log('My plugin setup')
52+
console.log('DevTools setup for my-plugin')
53+
54+
// Register dock entries, RPC functions, etc.
55+
ctx.docks.register({
56+
id: 'my-plugin',
57+
title: 'My Plugin',
58+
icon: 'ph:puzzle-piece-duotone',
59+
type: 'iframe',
60+
url: '/.my-plugin/',
61+
})
3462
},
3563
},
3664
}
3765
}
3866
```
3967

40-
When users run their app with Vite DevTools enabled (`vite dev --ui`), your devtools setup function will be called.
68+
> [!TIP] When is `setup` called?
69+
> The `devtools.setup` function is called when users run their app with Vite DevTools enabled via `vite dev --ui` or `vite build --ui` (not implemented in Vite yet). It runs once during Vite server initialization.
4170
4271
## DevTools Context
4372

44-
The `setup` function receives a `DevToolsNodeContext` which provides access to:
73+
The `setup` function receives a `DevToolsNodeContext` object that provides access to all DevTools APIs:
74+
75+
```ts
76+
const plugin: Plugin = {
77+
devtools: {
78+
setup(ctx) {
79+
// ctx contains everything you need
80+
}
81+
}
82+
}
83+
```
84+
85+
### Available Properties
86+
87+
| Property | Type | Description |
88+
|----------|------|-------------|
89+
| `ctx.docks` | `DocksHost` | Register and manage [dock entries](./dock-system) |
90+
| `ctx.views` | `ViewsHost` | Host static files for your DevTools UI |
91+
| `ctx.rpc` | `RpcHost` | Register [RPC functions](./rpc) and broadcast to clients |
92+
| `ctx.viteConfig` | `ResolvedConfig` | The resolved Vite configuration |
93+
| `ctx.viteServer` | `ViteDevServer \| undefined` | Vite dev server instance (only in dev mode) |
94+
| `ctx.mode` | `'dev' \| 'build'` | Current mode |
95+
| `ctx.cwd` | `string` | Current working directory |
96+
| `ctx.workspaceRoot` | `string` | Workspace root directory |
97+
98+
### Example: Accessing Vite Config
99+
100+
```ts
101+
const plugin: Plugin = {
102+
devtools: {
103+
setup(ctx) {
104+
// Access Vite configuration
105+
console.log('Root:', ctx.viteConfig.root)
106+
console.log('Mode:', ctx.mode)
107+
108+
// Check if we're in dev mode
109+
if (ctx.viteServer) {
110+
console.log('Dev server is running')
111+
}
112+
}
113+
}
114+
}
115+
```
116+
117+
## Hosting Static Files
118+
119+
If you have a pre-built UI (e.g., a Vue/React SPA), use `ctx.views.hostStatic()` to serve it:
120+
121+
```ts
122+
import { fileURLToPath } from 'node:url'
45123

46-
- `ctx.docks`: Manage [dock entries](./dock-system)
47-
- `ctx.views`: Host static views
48-
- `ctx.rpc`: Register [RPC functions](./rpc) or broadcast to clients
49-
- `ctx.utils`: Utility functions
50-
- `ctx.viteConfig`: Vite configuration
51-
- `ctx.viteServer`: Vite dev server (in dev mode)
52-
- `ctx.mode`: Current mode (`'dev'` or `'build'`)
124+
const plugin: Plugin = {
125+
devtools: {
126+
setup(ctx) {
127+
// Resolve path to your built client files
128+
const clientPath = fileURLToPath(
129+
new URL('../dist/client', import.meta.url)
130+
)
131+
132+
// Host at a specific route
133+
ctx.views.hostStatic('/.my-plugin/', clientPath)
134+
135+
// Register as a dock entry
136+
ctx.docks.register({
137+
id: 'my-plugin',
138+
title: 'My Plugin',
139+
icon: 'ph:puzzle-piece-duotone',
140+
type: 'iframe',
141+
url: '/.my-plugin/',
142+
})
143+
}
144+
}
145+
}
146+
```
147+
148+
DevTools handles:
149+
- Dev server middleware to serve the static files
150+
- Copying static files to the output directory during production builds
151+
152+
## Complete Example
153+
154+
Here's a complete example showing a DevTools plugin with dock entry, RPC function, and shared state:
155+
156+
```ts
157+
/// <reference types="@vitejs/devtools-kit" />
158+
import type { Plugin } from 'vite'
159+
import { fileURLToPath } from 'node:url'
160+
import { defineRpcFunction } from '@vitejs/devtools-kit'
161+
162+
export default function myAnalyzerPlugin(): Plugin {
163+
const analyzedModules = new Map<string, { size: number, imports: string[] }>()
164+
165+
return {
166+
name: 'my-analyzer',
167+
168+
// Collect data during transforms
169+
transform(code, id) {
170+
analyzedModules.set(id, {
171+
size: code.length,
172+
imports: [], // Parse imports here
173+
})
174+
},
175+
176+
devtools: {
177+
setup(ctx) {
178+
// Host the UI
179+
const clientPath = fileURLToPath(
180+
new URL('../dist/client', import.meta.url)
181+
)
182+
ctx.views.hostStatic('/.my-analyzer/', clientPath)
183+
184+
// Register dock entry
185+
ctx.docks.register({
186+
id: 'my-analyzer',
187+
title: 'Module Analyzer',
188+
icon: 'ph:chart-bar-duotone',
189+
type: 'iframe',
190+
url: '/.my-analyzer/',
191+
})
192+
193+
// Register RPC function to fetch data
194+
ctx.rpc.register(
195+
defineRpcFunction({
196+
name: 'my-analyzer:get-modules',
197+
type: 'query',
198+
setup: () => ({
199+
handler: async () => {
200+
return Array.from(analyzedModules.entries()).map(
201+
([id, data]) => ({ id, ...data })
202+
)
203+
},
204+
}),
205+
})
206+
)
207+
},
208+
},
209+
}
210+
}
211+
```
53212

54213
## Next Steps
55214

56-
- Learn how to [register dock entries](./dock-system) to create UI panels, actions, or custom renderers
57-
- Set up [RPC functions](./rpc) for server-client communication
58-
- Use [shared state](./shared-state) to synchronize data between server and client
215+
- **[Dock System](./dock-system)** - Learn about different dock entry types (iframe, action, custom renderer)
216+
- **[RPC](./rpc)** - Set up bidirectional server-client communication
217+
- **[Shared State](./shared-state)** - Synchronize state between server and all clients

0 commit comments

Comments
 (0)