Skip to content

Commit 9ad1a3c

Browse files
authored
fix(kit): prevent duplicate registration of devtool plugin (#457)
1 parent 1ad5e9f commit 9ad1a3c

File tree

7 files changed

+217
-16
lines changed

7 files changed

+217
-16
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import { onLegacyDevToolsPluginApiAvailable } from '../compat'
1919
import { DevToolsHooks } from '../types'
2020
import { createAppRecord, removeAppRecordId } from './app'
21-
import { callDevToolsPluginSetupFn, createComponentsDevToolsPlugin, registerDevToolsPlugin, setupDevToolsPlugin } from './plugin'
21+
import { callDevToolsPluginSetupFn, createComponentsDevToolsPlugin, registerDevToolsPlugin, removeRegisteredPluginApp, setupDevToolsPlugin } from './plugin'
2222
import { normalizeRouterInfo } from './router'
2323

2424
export function initDevTools() {
@@ -74,10 +74,9 @@ export function initDevTools() {
7474
setActiveAppRecord(normalizedAppRecord)
7575
setActiveAppRecordId(normalizedAppRecord.id)
7676
normalizeRouterInfo(normalizedAppRecord, activeAppRecord)
77+
registerDevToolsPlugin(normalizedAppRecord.app)
7778
}
78-
7979
setupDevToolsPlugin(...createComponentsDevToolsPlugin(normalizedAppRecord.app))
80-
registerDevToolsPlugin(normalizedAppRecord.app)
8180

8281
updateDevToolsState({
8382
connected: true,
@@ -104,6 +103,7 @@ export function initDevTools() {
104103
devtoolsContext.hooks.callHook(DevToolsMessagingHookKeys.SEND_ACTIVE_APP_UNMOUNTED_TO_CLIENT)
105104
}
106105
target.__VUE_DEVTOOLS_GLOBAL_HOOK__.apps.splice(target.__VUE_DEVTOOLS_GLOBAL_HOOK__.apps.indexOf(app), 1)
106+
removeRegisteredPluginApp(app)
107107
})
108108

109109
subscribeDevToolsHook()

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1+
import { target } from '@vue/devtools-shared'
12
import { App, PluginDescriptor, PluginSetupFunction } from '../../types'
23
import { hook } from '../../hook'
34
import { devtoolsContext, devtoolsPluginBuffer } from '../../ctx'
45
import { DevToolsPluginAPI } from '../../api'
56

67
export * from './components'
78

9+
target.__VUE_DEVTOOLS_KIT__REGISTERED_PLUGIN_APPS__ ??= new Set<App>()
10+
811
export function setupDevToolsPlugin(pluginDescriptor: PluginDescriptor, setupFn: PluginSetupFunction) {
912
return hook.setupDevToolsPlugin(pluginDescriptor, setupFn)
1013
}
1114

1215
export function callDevToolsPluginSetupFn(plugin: [PluginDescriptor, PluginSetupFunction], app: App) {
1316
const [pluginDescriptor, setupFn] = plugin
14-
// @TODO: need check
15-
// if (pluginDescriptor.app !== app)
16-
// return
17+
if (pluginDescriptor.app !== app)
18+
return
1719

1820
const api = new DevToolsPluginAPI({
1921
plugin: {
@@ -32,7 +34,17 @@ export function callDevToolsPluginSetupFn(plugin: [PluginDescriptor, PluginSetup
3234

3335
setupFn(api)
3436
}
37+
38+
export function removeRegisteredPluginApp(app: App) {
39+
target.__VUE_DEVTOOLS_KIT__REGISTERED_PLUGIN_APPS__.delete(app)
40+
}
41+
3542
export function registerDevToolsPlugin(app: App) {
43+
if (target.__VUE_DEVTOOLS_KIT__REGISTERED_PLUGIN_APPS__.has(app))
44+
return
45+
46+
target.__VUE_DEVTOOLS_KIT__REGISTERED_PLUGIN_APPS__.add(app)
47+
3648
devtoolsPluginBuffer.forEach((plugin) => {
3749
callDevToolsPluginSetupFn(plugin, app)
3850
})

packages/devtools-kit/src/ctx/api.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getRootElementsFromComponentInstance } from '../core/component/tree/el'
88
import { openInEditor } from '../core/open-in-editor'
99
import { normalizeRouterInfo } from '../core/router'
1010
import { getComponentInspector } from '../core/component-inspector'
11+
import { registerDevToolsPlugin } from '../core/plugin'
1112
import type { DevToolsContextHooks, DevToolsMessagingHooks, DevToolsV6PluginAPIHookPayloads } from './hook'
1213
import { DevToolsContextHookKeys, DevToolsV6PluginAPIHookKeys } from './hook'
1314
import { activeAppRecord, devtoolsAppRecords, setActiveAppRecord, setActiveAppRecordId } from './state'
@@ -108,6 +109,7 @@ export function createDevToolsApi(hooks: Hookable<DevToolsContextHooks & DevTool
108109
setActiveAppRecord(appRecord)
109110
normalizeRouterInfo(appRecord, activeAppRecord)
110111
callInspectorUpdatedHook()
112+
registerDevToolsPlugin(appRecord.app)
111113
}
112114
},
113115
// inspect dom

packages/playground/basic/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"dependencies": {
1010
"@tanstack/vue-query": "^5.41.0",
1111
"@vueuse/core": "^10.10.0",
12+
"element-plus": "^2.7.5",
1213
"pinia": "^2.1.7",
1314
"unplugin-auto-import": "^0.17.6",
1415
"vee-validate": "^4.13.1",

packages/playground/basic/src/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { createRouter, createWebHistory } from 'vue-router'
44
import { VueQueryPlugin } from '@tanstack/vue-query'
55
import { addCustomCommand } from '@vue/devtools-api'
66

7+
import ElementPlus from 'element-plus'
78
import store from './stores/vuexStore'
89

910
import App from './App.vue'
10-
11+
import 'element-plus/dist/index.css'
1112
import Home from './pages/Home.vue'
1213
import Hey from './pages/Hey.vue'
1314
import VueQuery from './pages/VueQuery.vue'
@@ -16,8 +17,8 @@ import './style.css'
1617
import 'uno.css'
1718

1819
const pinia = createPinia()
19-
2020
const app = createApp(App)
21+
app.use(ElementPlus)
2122

2223
// devtools.connect()
2324

packages/playground/basic/vite.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import commonjs from '@rollup/plugin-commonjs'
55
import Unocss from 'unocss/vite'
66
import AutoImport from 'unplugin-auto-import/vite'
77
import inspect from 'vite-plugin-inspect'
8-
8+
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
99
// https://vitejs.dev/config/
1010
export default defineConfig({
1111
plugins: [
@@ -21,6 +21,7 @@ export default defineConfig({
2121
'vue-router',
2222
'@vueuse/core',
2323
],
24+
resolvers: [ElementPlusResolver()],
2425
}),
2526
inspect(),
2627
],

0 commit comments

Comments
 (0)