Skip to content

Commit 4c594ce

Browse files
antfuclaude
andauthored
feat: upgrade vite-plugin-inspect to v12, use devtools-kit RPC (#966)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 4418e61 commit 4c594ce

7 files changed

Lines changed: 174 additions & 207 deletions

File tree

packages/devtools-kit/build.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export default defineBuildConfig({
2424
'hookable',
2525
'error-stack-parser-es',
2626
'shiki',
27+
'@vitejs/devtools-kit',
28+
'@vitejs/devtools-rpc',
2729
],
2830
declaration: true,
2931
rollup: {

packages/devtools-kit/src/_types/server-ctx.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { DevToolsNodeContext } from '@vitejs/devtools-kit'
12
import type { BirpcGroup } from 'birpc'
23
import type { Nuxt, NuxtDebugModuleMutationRecord } from 'nuxt/schema'
34
import type { ModuleOptions } from './options'
@@ -31,6 +32,11 @@ export interface NuxtDevtoolsServerContext {
3132

3233
rpc: NuxtDevtoolsRpc
3334

35+
/**
36+
* The Vite DevTools Kit context, available after connection.
37+
*/
38+
devtoolsKit: DevToolsNodeContext | undefined
39+
3440
/**
3541
* Hook to open file in editor
3642
*/

packages/devtools/src/integrations/vite-inspect.ts

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,53 @@
11
import type { Plugin } from 'vite'
2-
import type { ViteInspectAPI, ViteInspectOptions } from 'vite-plugin-inspect'
2+
import type { ViteInspectOptions } from 'vite-plugin-inspect'
33
import type { NuxtDevtoolsServerContext } from '../types'
4-
import { addCustomTab } from '@nuxt/devtools-kit'
5-
import { addVitePlugin } from '@nuxt/kit'
4+
import { addVitePlugin, logger } from '@nuxt/kit'
65

7-
const DOUBLE_SLASH_RE = /\/\//g
86
const VERSION_QUERY_RE = /\?v=\w+$/
97
const VUE_EXT_RE = /\.vue($|\?v=)/
108

119
export async function createVitePluginInspect(options?: ViteInspectOptions): Promise<Plugin> {
1210
return await import('vite-plugin-inspect').then(r => r.default(options))
1311
}
1412

15-
export async function setup({ nuxt, rpc }: NuxtDevtoolsServerContext) {
13+
export async function setup({ rpc, devtoolsKit }: NuxtDevtoolsServerContext) {
1614
const plugin = await createVitePluginInspect()
1715
addVitePlugin(plugin)
1816

19-
let api: ViteInspectAPI | undefined
20-
21-
nuxt.hook('vite:serverCreated', () => {
22-
api = plugin.api
23-
})
24-
25-
addCustomTab(() => ({
26-
name: 'builtin-vite-inspect',
27-
title: 'Inspect',
28-
icon: 'carbon-ibm-watson-discovery',
29-
category: 'advanced',
30-
view: {
31-
type: 'iframe',
32-
src: `${nuxt.options.app.baseURL}${nuxt.options.app.buildAssetsDir}/__inspect/`.replace(DOUBLE_SLASH_RE, '/'),
33-
},
34-
}), nuxt)
35-
3617
async function getComponentsRelationships() {
37-
const meta = await api?.rpc.getMetadata()
18+
if (!devtoolsKit?.rpc.has('vite-plugin-inspect:get-metadata')) {
19+
logger.warn('[nuxt-devtools] vite-plugin-inspect RPC functions not registered, component relationships unavailable')
20+
return []
21+
}
22+
23+
const meta = await devtoolsKit.rpc.invokeLocal('vite-plugin-inspect:get-metadata' as any)
3824
const modules = (
3925
meta && meta.instances[0]
40-
? await api?.rpc.getModulesList({
26+
? await devtoolsKit.rpc.invokeLocal('vite-plugin-inspect:get-modules-list' as any, {
4127
vite: meta.instances[0].vite,
4228
env: meta.instances[0].environments[0]!,
4329
})
4430
: null
4531
) || []
4632

4733
const components = await rpc.functions.getComponents() || []
48-
const vueModules = modules.filter((m) => {
34+
const vueModules = modules.filter((m: any) => {
4935
const plainId = m.id.replace(VERSION_QUERY_RE, '')
5036
if (components.some(c => c.filePath === plainId))
5137
return true
5238
return m.id.match(VUE_EXT_RE)
5339
})
5440

55-
const graph = vueModules.map((i) => {
41+
const graph = vueModules.map((i: any) => {
5642
function searchForVueDeps(id: string, seen = new Set<string>()): string[] {
5743
if (seen.has(id))
5844
return []
5945
seen.add(id)
60-
const module = modules.find(m => m.id === id)
46+
const module = modules.find((m: any) => m.id === id)
6147
if (!module)
6248
return []
63-
return module.deps.flatMap((i) => {
64-
if (vueModules.some(m => m.id === i))
49+
return module.deps.flatMap((i: string) => {
50+
if (vueModules.some((m: any) => m.id === i))
6551
return [i]
6652
return searchForVueDeps(i, seen)
6753
})

packages/devtools/src/module-main.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export async function enableModule(options: ModuleOptions, nuxt: Nuxt) {
6464
addVitePlugin(DevTools)
6565

6666
// Deferred: will be set when Vite DevTools plugin setup runs
67-
let connectRpcHost: ((host: any) => void) | undefined
67+
let connectDevToolsKit: ((ctx: any) => void) | undefined
6868

6969
addVitePlugin(defineViteDevToolsPlugin({
7070
name: 'nuxt:devtools',
@@ -78,8 +78,8 @@ export async function enableModule(options: ModuleOptions, nuxt: Nuxt) {
7878
url: '/__nuxt_devtools__/client/',
7979
})
8080

81-
// Connect Nuxt DevTools RPC to Vite DevTools Kit's RPC host
82-
connectRpcHost?.(ctx.rpc)
81+
// Connect Nuxt DevTools to Vite DevTools Kit context
82+
connectDevToolsKit?.(ctx)
8383
},
8484
},
8585
}))
@@ -130,11 +130,11 @@ window.__NUXT_DEVTOOLS_TIME_METRIC__.appInit = Date.now()
130130
})
131131

132132
const {
133-
connectRpcHost: _connectRpcHost,
133+
connectDevToolsKit: _connectDevToolsKit,
134134
...ctx
135135
} = setupRPC(nuxt, options)
136136

137-
connectRpcHost = _connectRpcHost
137+
connectDevToolsKit = _connectDevToolsKit
138138

139139
const clientDirExists = existsSync(clientDir)
140140

packages/devtools/src/server-rpc/index.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { RpcFunctionsHost } from '@vitejs/devtools-kit'
1+
import type { DevToolsNodeContext } from '@vitejs/devtools-kit'
22
import type { Nuxt } from 'nuxt/schema'
33

44
import type { ModuleOptions, NuxtDevtoolsServerContext, ServerFunctions } from '../types'
@@ -21,15 +21,15 @@ import { setupTimelineRPC } from './timeline'
2121
export function setupRPC(nuxt: Nuxt, options: ModuleOptions) {
2222
const serverFunctions = {} as ServerFunctions
2323
const extendedRpcMap = new Map<string, Record<string, (...args: any[]) => any>>()
24-
let rpcHost: RpcFunctionsHost | undefined
24+
let devtoolsKitCtx: DevToolsNodeContext | undefined
2525
const pendingBroadcasts: { method: string, args: any[] }[] = []
2626

2727
function broadcast(method: string, ...args: any[]) {
28-
if (!rpcHost) {
28+
if (!devtoolsKitCtx) {
2929
pendingBroadcasts.push({ method, args })
3030
return
3131
}
32-
rpcHost.broadcast({
32+
devtoolsKitCtx.rpc.broadcast({
3333
method: method as any,
3434
args: args as any,
3535
event: true,
@@ -61,12 +61,12 @@ export function setupRPC(nuxt: Nuxt, options: ModuleOptions) {
6161
set(target, prop, value) {
6262
(target as any)[prop] = value
6363
// Also update on RpcFunctionsHost if available
64-
if (rpcHost && typeof prop === 'string') {
65-
if (rpcHost.has(prop)) {
66-
rpcHost.update({ name: prop, handler: value })
64+
if (devtoolsKitCtx && typeof prop === 'string') {
65+
if (devtoolsKitCtx.rpc.has(prop)) {
66+
devtoolsKitCtx.rpc.update({ name: prop, handler: value })
6767
}
6868
else {
69-
rpcHost.register({ name: prop, handler: value })
69+
devtoolsKitCtx.rpc.register({ name: prop, handler: value })
7070
}
7171
}
7272
return true
@@ -86,10 +86,10 @@ export function setupRPC(nuxt: Nuxt, options: ModuleOptions) {
8686
extendedRpcMap.set(namespace, functions)
8787

8888
// Register on RpcFunctionsHost if already available
89-
if (rpcHost) {
89+
if (devtoolsKitCtx) {
9090
for (const [fnName, handler] of Object.entries(functions)) {
9191
if (typeof handler === 'function') {
92-
rpcHost.register({ name: `${namespace}:${fnName}`, handler: handler as any })
92+
devtoolsKitCtx.rpc.register({ name: `${namespace}:${fnName}`, handler: handler as any })
9393
}
9494
}
9595
}
@@ -106,6 +106,7 @@ export function setupRPC(nuxt: Nuxt, options: ModuleOptions) {
106106
nuxt,
107107
options,
108108
rpc: rpc as any,
109+
get devtoolsKit() { return devtoolsKitCtx },
109110
refresh,
110111
extendServerRpc,
111112
openInEditorHooks: [],
@@ -131,11 +132,12 @@ export function setupRPC(nuxt: Nuxt, options: ModuleOptions) {
131132
} as ServerFunctions)
132133

133134
/**
134-
* Connect to Vite DevTools Kit's RPC host.
135+
* Connect to Vite DevTools Kit context.
135136
* Called from the Vite DevTools plugin setup callback.
136137
*/
137-
function connectRpcHost(host: RpcFunctionsHost) {
138-
rpcHost = host
138+
function connectDevToolsKit(ctx: DevToolsNodeContext) {
139+
devtoolsKitCtx = ctx
140+
const host = ctx.rpc
139141

140142
// Flush any broadcasts that were queued before connection
141143
for (const { method, args } of pendingBroadcasts) {
@@ -183,7 +185,7 @@ export function setupRPC(nuxt: Nuxt, options: ModuleOptions) {
183185
}
184186

185187
return {
186-
connectRpcHost,
188+
connectDevToolsKit,
187189
...ctx,
188190
}
189191
}

0 commit comments

Comments
 (0)