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

feat(devtools): register the devtools plugin and allow query inspection #3

Merged
merged 2 commits into from
Nov 23, 2022
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground"
},
"dependencies": {
"@nuxt/kit": "^3.0.0"
"@nuxt/kit": "^3.0.0",
"@vue/devtools-api": "^6.4.5"
},
"devDependencies": {
"@nuxt/module-builder": "^0.2.1",
Expand Down
19 changes: 18 additions & 1 deletion playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
<template>
<div>
Nuxt module playground!

<button @click="fooRefresh()">
refresh foo
</button>
<button @click="barRefresh()">
refresh bar
</button>
</div>
</template>

<script setup>
<script setup lang="ts">
import { useFetch } from '#app'

const { data: fooData, refresh: fooRefresh } = await useFetch('/api/foo', {
key: 'foo'
})

const { data: barData, refresh: barRefresh } = await useFetch('/api/bar', {
key: 'bar',
immediate: false
})
</script>
11 changes: 4 additions & 7 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { defineNuxtConfig } from 'nuxt/config'
import MyModule from '..'
import NuxtQuery from '..'

export default defineNuxtConfig({
modules: [
MyModule
],
myModule: {
addPlugin: true
modules: [NuxtQuery],
nuxtQuery: {
addDevtools: true
}
})
5 changes: 5 additions & 0 deletions playground/server/api/bar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default defineEventHandler(() => {
return {
bar: Math.random()
}
})
5 changes: 5 additions & 0 deletions playground/server/api/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default defineEventHandler(() => {
return {
foo: Math.random()
}
})
3 changes: 2 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import { fileURLToPath } from 'url'
import { defineNuxtModule, addPlugin, createResolver } from '@nuxt/kit'
import { defineNuxtModule, addPlugin, createResolver, useLogger } from '@nuxt/kit'

export interface ModuleOptions {
addPlugin: boolean
addDevtools: boolean
}

const PACKAGE_NAME = 'nuxt-query'

export default defineNuxtModule<ModuleOptions>({
meta: {
name: 'nuxt-query',
name: PACKAGE_NAME,
configKey: 'nuxtQuery'
},
defaults: {
addPlugin: true
addDevtools: true
},
setup (options, nuxt) {
if (options.addPlugin) {
const logger = useLogger(PACKAGE_NAME)

if (options.addDevtools) {
const { resolve } = createResolver(import.meta.url)
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
nuxt.options.build.transpile.push(runtimeDir)
addPlugin(resolve(runtimeDir, 'plugin'))

addPlugin(resolve(runtimeDir, 'plugins', 'devtools'))
logger.info('Installed nuxt-query devtools.')
}
}
})
5 changes: 5 additions & 0 deletions src/runtime/composables/useQueryCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { useNuxtApp } from '#imports'

export const useQueryCache = () => {
return useNuxtApp().payload.data
}
65 changes: 65 additions & 0 deletions src/runtime/devtools/devtools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { setupDevtoolsPlugin } from '@vue/devtools-api'
import { watch } from '#imports'

const inspectorId = 'nuxt-query-cache'

export const setupDevtools = (app: any, data: Record<string, any>) => {
setupDevtoolsPlugin({
id: 'nuxt-query',
label: 'Nuxt Query',
packageName: 'nuxt-query',
app
}, (api) => {
api.addInspector({
id: inspectorId,
label: 'Nuxt Query Cache',
icon: 'cached'
})

api.on.getInspectorTree((payload, context) => {
if (payload.inspectorId === inspectorId) {
const children: {
id: string
label: string
}[] = []

Object.entries(data).forEach((value) => {
children.push({
id: value[0],
label: value[0]
})
})

payload.rootNodes = [
{
id: 'root',
label: 'Query Cache',
children
}
]
}
})

api.on.getInspectorState((payload) => {
if (payload.inspectorId === inspectorId) {
Object.entries(data).forEach((value) => {
if (payload.nodeId === value[0]) {
payload.state = {
Value: value[1]
}
}
})
}
})

watch(data, () => {
// Update tree
api.sendInspectorTree(inspectorId)

// Update state
api.sendInspectorState(inspectorId)
}, {
deep: true
})
})
}
14 changes: 14 additions & 0 deletions src/runtime/devtools/installer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useQueryCache } from '../composables/useQueryCache'
import { setupDevtools } from './devtools'

export const pluginInstaller = {
install (app: any) {
const data = useQueryCache()

let devtools: ReturnType<typeof setupDevtools>

if (process.env.NODE_ENV !== 'production') {
devtools = setupDevtools(app, data)
}
}
}
5 changes: 0 additions & 5 deletions src/runtime/plugin.ts

This file was deleted.

8 changes: 8 additions & 0 deletions src/runtime/plugins/devtools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineNuxtPlugin } from '#app'
import { pluginInstaller } from '../devtools/installer'

export default defineNuxtPlugin((nuxtApp) => {
if (process.client) {
nuxtApp.vueApp.use(pluginInstaller)
}
})