Skip to content

Commit dbb6bb6

Browse files
authoredMar 25, 2024··
fix(kit): respect legacy devtools hook in nuxt app (#292)
1 parent 9b2280e commit dbb6bb6

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed
 

‎packages/devtools-kit/src/core/component/tree/walker.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { SuspenseBoundary, VNode } from 'vue'
22
import type { VueAppInstance } from '../../../types'
33
import type { ComponentTreeNode } from '../types'
44
import { getAppRecord, getInstanceName, getRenderKey, getUniqueComponentId, isBeingDestroyed, isFragment } from '../utils'
5-
import { devtoolsContext } from '../../../state'
5+
import { devtoolsAppRecords, devtoolsContext } from '../../../state'
66
import { getRootElementsFromComponentInstance } from './el'
77
import type { ComponentFilter } from './filter'
88
import { createComponentFilter } from './filter'
@@ -241,8 +241,12 @@ export class ComponentWalker {
241241
*/
242242
private mark(instance: VueAppInstance, force = false) {
243243
const instanceMap = getAppRecord(instance)!.instanceMap
244-
if (force || !instanceMap.has(instance.__VUE_DEVTOOLS_UID__))
244+
if (force || !instanceMap.has(instance.__VUE_DEVTOOLS_UID__)) {
245245
instanceMap.set(instance.__VUE_DEVTOOLS_UID__, instance)
246+
247+
// force sync appRecord instanceMap
248+
devtoolsAppRecords.active.instanceMap = instanceMap
249+
}
246250
}
247251

248252
private isKeepAlive(instance: VueAppInstance) {

‎packages/devtools-kit/src/core/index.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { target } from '@vue/devtools-shared'
1+
import { isNuxtApp, target } from '@vue/devtools-shared'
22
import { createDevToolsHook, devtoolsHooks, hook, subscribeDevToolsHook } from '../hook'
33
import { DevToolsHooks } from '../types'
44
import { devtoolsAppRecords, devtoolsState, getDevToolsEnv } from '../state'
@@ -14,12 +14,16 @@ export function initDevTools() {
1414
if (target.__VUE_DEVTOOLS_GLOBAL_HOOK__ && isDevToolsNext)
1515
return
1616

17-
// compatible with old devtools
18-
if (target.__VUE_DEVTOOLS_GLOBAL_HOOK__)
19-
Object.assign(__VUE_DEVTOOLS_GLOBAL_HOOK__, createDevToolsHook())
20-
21-
else
17+
if (!target.__VUE_DEVTOOLS_GLOBAL_HOOK__) {
2218
target.__VUE_DEVTOOLS_GLOBAL_HOOK__ = createDevToolsHook()
19+
}
20+
else {
21+
// respect old devtools hook in nuxt application
22+
if (!isNuxtApp) {
23+
// override devtools hook directly
24+
Object.assign(__VUE_DEVTOOLS_GLOBAL_HOOK__, createDevToolsHook())
25+
}
26+
}
2327

2428
// setup old devtools plugin (compatible with pinia, router, etc)
2529
hook.on.setupDevtoolsPlugin((pluginDescriptor, setupFn) => {

‎packages/devtools-kit/src/hook/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function createDevToolsHook(): DevToolsHook {
3333
id: 'vue-devtools-next',
3434
enabled: false,
3535
appRecords: [],
36-
apps: {},
36+
apps: [],
3737
events: new Map(),
3838
on(event, fn) {
3939
if (!this.events.has(event))

‎packages/devtools-kit/src/plugins/component.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { debounce } from 'perfect-debounce'
22
import { VueAppInstance } from '../types'
33
import { DevToolsEvents, apiHooks, setupDevToolsPlugin } from '../api'
4-
import { devtoolsContext, devtoolsState } from '../state'
4+
import { devtoolsAppRecords, devtoolsContext, devtoolsState } from '../state'
55
import { hook } from '../hook'
66
import { getAppRecord, getComponentId, getComponentInstance } from '../core/component/utils'
77
import { getComponentBoundingRect } from '../core/component/state/bounding-rect'
@@ -116,8 +116,11 @@ export function registerComponentDevToolsPlugin(app: VueAppInstance) {
116116
if (component.__VUE_DEVTOOLS_UID__ == null)
117117
component.__VUE_DEVTOOLS_UID__ = id
118118

119-
if (!appRecord?.instanceMap.has(id))
119+
if (!appRecord?.instanceMap.has(id)) {
120120
appRecord?.instanceMap.set(id, component)
121+
// force sync appRecord instanceMap
122+
devtoolsAppRecords.active.instanceMap = appRecord!.instanceMap
123+
}
121124
}
122125

123126
if (!appRecord)
@@ -147,8 +150,11 @@ export function registerComponentDevToolsPlugin(app: VueAppInstance) {
147150
if (component.__VUE_DEVTOOLS_UID__ == null)
148151
component.__VUE_DEVTOOLS_UID__ = id
149152

150-
if (!appRecord?.instanceMap.has(id))
153+
if (!appRecord?.instanceMap.has(id)) {
154+
// force sync appRecord instanceMap
151155
appRecord?.instanceMap.set(id, component)
156+
devtoolsAppRecords.active.instanceMap = appRecord!.instanceMap
157+
}
152158
}
153159

154160
if (!appRecord)
@@ -177,7 +183,10 @@ export function registerComponentDevToolsPlugin(app: VueAppInstance) {
177183
uid,
178184
instance: component,
179185
}) as string
186+
180187
appRecord?.instanceMap.delete(id)
188+
// force sync appRecord instanceMap
189+
devtoolsAppRecords.active.instanceMap = appRecord.instanceMap
181190

182191
debounceSendInspectorTree()
183192
})

‎packages/shared/src/env.ts

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ export const target = (typeof globalThis !== 'undefined'
1212
export const isInChromePanel = typeof target.chrome !== 'undefined' && !!target.chrome.devtools
1313
export const isInIframe = isBrowser && target.self !== target.top
1414
export const isInElectron = typeof navigator !== 'undefined' && navigator.userAgent.toLowerCase().includes('electron')
15+
// @ts-expect-error skip type check
16+
export const isNuxtApp = typeof window !== 'undefined' && !!window.__NUXT__

0 commit comments

Comments
 (0)
Please sign in to comment.