Skip to content

Commit

Permalink
fix(devtools-kit): dynamically register setupFn of plugins (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
Azurewarth0920 authored Feb 26, 2024
1 parent 90285de commit d6b92f4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
48 changes: 26 additions & 22 deletions packages/devtools-kit/src/api/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,35 @@ export function setupDevToolsPlugin(pluginDescriptor: PluginDescriptor, setupFn:
return hook.setupDevToolsPlugin(pluginDescriptor, setupFn)
}

export function registerPlugin(app: App<any>, api: DevToolsPluginApi) {
const plugins = devtoolsState.pluginBuffer.filter(([plugin]) => plugin.app === app)
plugins.forEach(async ([plugin, setupFn]) => {
if (plugin.packageName === 'vue-query') {
/**
* Skip it for now because plugin api doesn't support vue-query devtools plugin:
* https://github.com/TanStack/query/blob/main/packages/vue-query/src/devtools/devtools.ts
* @TODO: Need to discuss if we should be full compatible with the old devtools plugin api.
*/
return
}
export function setupExternalPlugin(plugin: [PluginDescriptor, PluginSetupFunction], app: App<any>, api: DevToolsPluginApi) {
const [pluginDescriptor, setupFn] = plugin
if (pluginDescriptor.app !== app)
return

if (pluginDescriptor.packageName === 'vue-query') {
/**
* Skip it for now because plugin api doesn't support vue-query devtools plugin:
* https://github.com/TanStack/query/blob/main/packages/vue-query/src/devtools/devtools.ts
* @TODO: Need to discuss if we should be full compatible with the old devtools plugin api.
*/
return
}

// edge case for router plugin
if (plugin.packageName === 'vue-router') {
const id = getRouterDevToolsId(`${plugin.id}`)
if (plugin.app === app) {
devtoolsAppRecords.value = devtoolsAppRecords.value.map(item => ({
...item,
routerId: id,
}))
}
// edge case for router plugin
if (pluginDescriptor.packageName === 'vue-router') {
const id = getRouterDevToolsId(`${pluginDescriptor.id}`)
if (pluginDescriptor.app === app) {
devtoolsAppRecords.value = devtoolsAppRecords.value.map(item => ({
...item,
routerId: id,
}))
}
setupFn(api)
})
}
setupFn(api)
}

export function registerPlugin(app: App<any>, api: DevToolsPluginApi) {
devtoolsState.pluginBuffer.forEach(plugin => setupExternalPlugin(plugin, app, api))
devtoolsAppRecords.value = devtoolsAppRecords.value.map((record) => {
const globalProperties = record.app?.config?.globalProperties
if (!globalProperties)
Expand Down
10 changes: 8 additions & 2 deletions packages/devtools-kit/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { target } from '@vue/devtools-shared'
import { createDevToolsHook, devtoolsHooks, hook, subscribeDevToolsHook } from '../hook'
import { DevToolsHooks } from '../types'
import { devtoolsAppRecords, devtoolsState, getDevToolsEnv } from '../state'
import { DevToolsEvents, DevToolsPluginApi, apiHooks, collectDevToolsPlugin } from '../api'
import { DevToolsEvents, DevToolsPluginApi, apiHooks, collectDevToolsPlugin, setupExternalPlugin } from '../api'
import { createAppRecord, setActiveAppRecord } from './app-record'

export function initDevTools() {
Expand All @@ -22,7 +22,13 @@ export function initDevTools() {
target.__VUE_DEVTOOLS_GLOBAL_HOOK__ = createDevToolsHook()

// setup old devtools plugin (compatible with pinia, router, etc)
hook.on.setupDevtoolsPlugin(collectDevToolsPlugin)
hook.on.setupDevtoolsPlugin((pluginDescriptor, setupFn) => {
collectDevToolsPlugin(pluginDescriptor, setupFn)
const { app, api } = devtoolsAppRecords.active || {}
if (!app || !api)
return
setupExternalPlugin([pluginDescriptor, setupFn], app, api)
})

// create app record
hook.on.vueAppInit(async (app, version) => {
Expand Down
6 changes: 6 additions & 0 deletions packages/playground/basic/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
/hello
</span>
</RouterLink>
|
<RouterLink to="/hey/123123">
<span>
/hey/123123
</span>
</RouterLink>
</div>
</div>
</template>
4 changes: 4 additions & 0 deletions packages/playground/basic/src/pages/Hey.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<script setup lang="ts">
import { useCounterStore } from '../stores'
const route = useRoute()
const counterStore = useCounterStore()
</script>

<template>
<div class="container">
Hey: {{ route.params.id }}
Counter: {{ counterStore.count }}
</div>
</template>

Expand Down

0 comments on commit d6b92f4

Please sign in to comment.