Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(devtools-kit): dynamically register setupFn of plugins #247

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading