Skip to content

Commit b27aba1

Browse files
authored
feat: inline birpc-x into the rpc package (#170)
1 parent 54ae855 commit b27aba1

38 files changed

+2811
-46
lines changed

‎alias.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export const alias = {
99
'@vitejs/devtools-rpc/presets/ws/server': r('rpc/src/presets/ws/server.ts'),
1010
'@vitejs/devtools-rpc/presets/ws/client': r('rpc/src/presets/ws/client.ts'),
1111
'@vitejs/devtools-rpc/presets': r('rpc/src/presets/index.ts'),
12+
'@vitejs/devtools-rpc/client': r('rpc/src/client.ts'),
13+
'@vitejs/devtools-rpc/server': r('rpc/src/server.ts'),
1214
'@vitejs/devtools-rpc': r('rpc/src'),
1315
'@vitejs/devtools-kit/client': r('kit/src/client/index.ts'),
1416
'@vitejs/devtools-kit/constants': r('kit/src/constants.ts'),

‎docs/kit/rpc.md‎

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ const plugin: Plugin = {
6060

6161
### Function Types
6262

63-
| Type | Description | Caching |
64-
|------|-------------|---------|
65-
| `query` | Fetch data, read operations | Can be cached |
66-
| `action` | Side effects, mutations | Not cached |
67-
| `static` | Constant data that never changes | Cached indefinitely |
63+
| Type | Description | Caching | Dump Support |
64+
|------|-------------|---------|--------------|
65+
| `query` | Fetch data, read operations | Can be cached | ✓ (manual) |
66+
| `static` | Constant data that never changes | Cached indefinitely | ✓ (automatic) |
67+
| `action` | Side effects, mutations | Not cached ||
68+
| `event` | Emit events, no response | Not cached ||
6869

6970
### Handler Arguments
7071

@@ -104,6 +105,99 @@ setup: (ctx) => {
104105
}
105106
```
106107

108+
> [!IMPORTANT]
109+
> For build mode compatibility, compute data in the setup function using the context rather than relying on runtime global state. This allows the dump feature to pre-compute results at build time.
110+
111+
### Dump Feature for Build Mode
112+
113+
When using `vite devtools build` to create a static DevTools build, the server cannot execute functions at runtime. The **dump feature** solves this by pre-computing RPC results at build time.
114+
115+
#### How It Works
116+
117+
1. At build time, `dumpFunctions()` executes your RPC handlers with predefined arguments
118+
2. Results are stored in `.vdt-rpc-dump.json` in the build output
119+
3. The static client reads from this JSON file instead of making live RPC calls
120+
121+
#### Static Functions (Recommended)
122+
123+
Functions with `type: 'static'` are **automatically dumped** with no arguments:
124+
125+
```ts
126+
const getConfig = defineRpcFunction({
127+
name: 'my-plugin:get-config',
128+
type: 'static', // Auto-dumped with inputs: [[]]
129+
setup: ctx => ({
130+
handler: async () => ({
131+
root: ctx.viteConfig.root,
132+
plugins: ctx.viteConfig.plugins.map(p => p.name),
133+
}),
134+
}),
135+
})
136+
```
137+
138+
This works in both dev mode (live) and build mode (pre-computed).
139+
140+
#### Query Functions with Dumps
141+
142+
For `query` functions that need arguments, define `dump` in the setup:
143+
144+
```ts
145+
const getModule = defineRpcFunction({
146+
name: 'my-plugin:get-module',
147+
type: 'query',
148+
setup: (ctx) => {
149+
// Collect all module IDs at build time
150+
const moduleIds = Array.from(ctx.viteServer?.moduleGraph?.idToModuleMap.keys() || [])
151+
152+
return {
153+
handler: async (id: string) => {
154+
const module = ctx.viteServer?.moduleGraph?.getModuleById(id)
155+
return module ? { id, size: module.transformResult?.code.length } : null
156+
},
157+
dump: {
158+
inputs: moduleIds.map(id => [id]), // Pre-compute for all modules
159+
fallback: null, // Return null for unknown modules
160+
},
161+
}
162+
},
163+
})
164+
```
165+
166+
#### Recommendations for Plugin Authors
167+
168+
To ensure your DevTools work in build mode:
169+
170+
1. **Prefer `type: 'static'`** for functions that return constant data
171+
2. **Return context-based data in setup** rather than accessing global state in handlers
172+
3. **Define dumps in setup function** for query functions that need pre-computation
173+
4. **Use fallback values** for graceful degradation when arguments don't match
174+
175+
```ts
176+
// ✓ Good: Returns static data, works in build mode
177+
const getPluginInfo = defineRpcFunction({
178+
name: 'my-plugin:info',
179+
type: 'static',
180+
setup: ctx => ({
181+
handler: async () => ({
182+
version: '1.0.0',
183+
root: ctx.viteConfig.root,
184+
}),
185+
}),
186+
})
187+
188+
// ✗ Avoid: Depends on runtime server state
189+
const getLiveMetrics = defineRpcFunction({
190+
name: 'my-plugin:metrics',
191+
type: 'query', // No dump - won't work in build mode
192+
handler: async () => {
193+
return getCurrentMetrics() // Requires live server
194+
},
195+
})
196+
```
197+
198+
> [!TIP]
199+
> If your data genuinely needs live server state, use `type: 'query'` without dumps. The function will work in dev mode but gracefully fail in build mode.
200+
107201
## Client-Side Calls
108202

109203
### In Iframe Pages

‎packages/core/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
"@vitejs/devtools-rolldown": "workspace:*",
5757
"@vitejs/devtools-rpc": "workspace:*",
5858
"birpc": "catalog:deps",
59-
"birpc-x": "catalog:deps",
6059
"cac": "catalog:deps",
6160
"h3": "catalog:deps",
6261
"immer": "catalog:deps",

‎packages/core/src/node/host-functions.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { DevToolsNodeContext, DevToolsNodeRpcSession, DevToolsRpcClientFunctions, DevToolsRpcServerFunctions, RpcBroadcastOptions, RpcFunctionsHost as RpcFunctionsHostType, RpcSharedStateHost } from '@vitejs/devtools-kit'
22
import type { BirpcGroup } from 'birpc'
33
import type { AsyncLocalStorage } from 'node:async_hooks'
4-
import { RpcFunctionsCollectorBase } from 'birpc-x'
4+
import { RpcFunctionsCollectorBase } from '@vitejs/devtools-rpc'
55
import { createDebug } from 'obug'
66
import { createRpcSharedStateServerHost } from './rpc-shared-state'
77

‎packages/core/src/node/ws.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import type { WebSocket } from 'ws'
44
import type { RpcFunctionsHost } from './host-functions'
55
import { AsyncLocalStorage } from 'node:async_hooks'
66
import process from 'node:process'
7-
import { createRpcServer } from '@vitejs/devtools-rpc'
87
import { createWsRpcPreset } from '@vitejs/devtools-rpc/presets/ws/server'
8+
import { createRpcServer } from '@vitejs/devtools-rpc/server'
99
import c from 'ansis'
1010
import { getPort } from 'get-port-please'
1111
import { createDebug } from 'obug'

‎packages/kit/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
"dependencies": {
4343
"@vitejs/devtools-rpc": "workspace:*",
4444
"birpc": "catalog:deps",
45-
"birpc-x": "catalog:deps",
4645
"immer": "catalog:deps"
4746
},
4847
"devDependencies": {

‎packages/kit/src/client/docks.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { RpcFunctionsCollector } from 'birpc-x'
1+
import type { RpcFunctionsCollector } from '@vitejs/devtools-rpc'
22
import type { Raw } from 'vue'
33
import type { DevToolsDockEntriesGrouped } from '../../../core/src/client/webcomponents/state/dock-settings'
44
import type { DevToolsDockEntry, DevToolsDocksUserSettings, DevToolsDockUserEntry, DevToolsRpcClientFunctions, EventEmitter } from '../types'

‎packages/kit/src/client/rpc.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { WebSocketRpcClientOptions } from '@vitejs/devtools-rpc/presets/ws/
22
import type { BirpcOptions, BirpcReturn } from 'birpc'
33
import type { ConnectionMeta, DevToolsRpcClientFunctions, DevToolsRpcServerFunctions, EventEmitter, RpcSharedStateHost } from '../types'
44
import type { DevToolsClientContext, DevToolsClientRpcHost, RpcClientEvents } from './docks'
5-
import { createRpcClient } from '@vitejs/devtools-rpc'
5+
import { RpcFunctionsCollectorBase } from '@vitejs/devtools-rpc'
6+
import { createRpcClient } from '@vitejs/devtools-rpc/client'
67
import { createWsRpcPreset } from '@vitejs/devtools-rpc/presets/ws/client'
7-
import { RpcFunctionsCollectorBase } from 'birpc-x'
88
import { UAParser } from 'my-ua-parser'
99
import { createEventEmitter } from '../utils/events'
1010
import { nanoid } from '../utils/nanoid'

‎packages/kit/src/types/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ export * from './views'
99
export * from './vite-augment'
1010
export * from './vite-plugin'
1111

12-
export type { RpcDefinitionsFilter, RpcDefinitionsToFunctions } from 'birpc-x'
12+
export type { RpcDefinitionsFilter, RpcDefinitionsToFunctions } from '@vitejs/devtools-rpc'

‎packages/kit/src/types/rpc.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import type { RpcFunctionsCollectorBase } from '@vitejs/devtools-rpc'
12
import type { DevToolsNodeRpcSessionMeta } from '@vitejs/devtools-rpc/presets/ws/server'
23
import type { BirpcReturn } from 'birpc'
3-
import type { RpcFunctionsCollectorBase } from 'birpc-x'
44
import type { SharedState } from '../utils/shared-state'
55
import type { DevToolsRpcClientFunctions, DevToolsRpcServerFunctions, DevToolsRpcSharedStates } from './rpc-augments'
66
import type { DevToolsNodeContext } from './vite-plugin'

0 commit comments

Comments
 (0)