Skip to content

Commit 681378a

Browse files
committed
feat: add expiermental support for Vite DevTools
1 parent 8cd29b7 commit 681378a

File tree

7 files changed

+99
-14
lines changed

7 files changed

+99
-14
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@types/which": "catalog:types",
3636
"@types/ws": "catalog:types",
3737
"@unocss/eslint-config": "catalog:buildtools",
38+
"@vitejs/devtools-kit": "catalog:types",
3839
"bumpp": "catalog:cli",
3940
"conventional-changelog-cli": "catalog:cli",
4041
"eslint": "catalog:cli",

packages/devtools-kit/src/_types/options.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ export interface ModuleOptions {
4343
*/
4444
viteInspect?: boolean
4545

46+
/**
47+
* Enable Vite DevTools integration
48+
*
49+
* @experimental
50+
* @default false
51+
*/
52+
viteDevTools?: boolean
53+
4654
/**
4755
* Disable dev time authorization check.
4856
*

packages/devtools/client/plugins/global.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { NuxtDevtoolsHostClient } from '@nuxt/devtools-kit/types'
12
import { defineNuxtPlugin, useRouter } from '#imports'
23
import { triggerRef } from 'vue'
34
import { useClient } from '../composables/client'
@@ -16,27 +17,47 @@ export default defineNuxtPlugin(() => {
1617
rpc.openInEditor(path)
1718
}
1819

19-
Object.defineProperty(window, '__NUXT_DEVTOOLS_VIEW__', {
20-
value: {
21-
setClient(_client) {
22-
if (client.value === _client)
23-
return
20+
function setupClient(_client: NuxtDevtoolsHostClient) {
21+
if (client.value === _client)
22+
return
23+
24+
client.value = _client
25+
26+
_client.hooks.hook('host:update:reactivity', onUpdateReactivity)
27+
_client.hooks.hook('host:inspector:click', onInspectorClick)
28+
_client.hooks.hook('host:action:reload', () => location.reload())
29+
_client.hooks.hook('host:action:navigate', (path: string) => router.push(path))
2430

25-
client.value = _client
31+
// eslint-disable-next-line no-console
32+
console.log('[nuxt-devtools] Client connected', _client)
33+
}
2634

27-
_client.hooks.hook('host:update:reactivity', onUpdateReactivity)
28-
_client.hooks.hook('host:inspector:click', onInspectorClick)
29-
_client.hooks.hook('host:action:reload', () => location.reload())
30-
_client.hooks.hook('host:action:navigate', (path: string) => router.push(path))
35+
function connectParent() {
36+
try {
37+
if (window.parent === window)
38+
return
39+
if (window.parent?.__NUXT_DEVTOOLS_HOST__) {
40+
setupClient(window.parent.__NUXT_DEVTOOLS_HOST__)
41+
}
42+
}
43+
catch (error) {
44+
console.error('[nuxt-devtools] Failed to connect parent', error)
45+
}
46+
}
3147

32-
// eslint-disable-next-line no-console
33-
console.log('[nuxt-devtools] Client connected', _client)
48+
Object.defineProperty(window, '__NUXT_DEVTOOLS_VIEW__', {
49+
value: {
50+
setClient(_client) {
51+
setupClient(_client)
3452
},
3553
} as typeof window['__NUXT_DEVTOOLS_VIEW__'],
3654
enumerable: false,
3755
configurable: true,
3856
})
3957

58+
connectParent()
59+
setTimeout(() => connectParent(), 1000)
60+
4061
router.afterEach(() => {
4162
const path = router.currentRoute.value?.path
4263
if (!path || path.includes('__'))

packages/devtools/src/module-main.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type {} from '@vitejs/devtools-kit'
12
import type { ServerResponse } from 'node:http'
23
import type { Nuxt } from 'nuxt/schema'
34
import type { ViteDevServer } from 'vite'
@@ -34,7 +35,7 @@ export async function enableModule(options: ModuleOptions, nuxt: Nuxt) {
3435
}
3536

3637
// Determine if user aware devtools, by checking the presentation in the config
37-
const enabledExplicitly = (nuxt.options.devtools === true as unknown)
38+
const enabledExplicitly = (nuxt.options.devtools === true)
3839
|| (nuxt.options.devtools && nuxt.options.devtools.enabled)
3940
|| !!nuxt.options.modules.find(m => m === '@nuxt/devtools' || m === '@nuxt/devtools-edge' || m === '@nuxt/devtools-nightly')
4041

@@ -58,6 +59,26 @@ export async function enableModule(options: ModuleOptions, nuxt: Nuxt) {
5859
mode: 'server',
5960
})
6061

62+
if (options.viteDevTools) {
63+
// eslint-disable-next-line no-console
64+
console.log('[nuxt-devtools] Enabling experimental Vite DevTools integration')
65+
addVitePlugin({
66+
name: 'nuxt:devtools',
67+
devtools: {
68+
setup(ctx) {
69+
ctx.docks.register({
70+
id: 'nuxt:devtools',
71+
type: 'iframe',
72+
icon: 'https://nuxt.com/assets/design-kit/icon-green.svg',
73+
title: 'Nuxt DevTools',
74+
url: '/__nuxt_devtools__/client/',
75+
})
76+
},
77+
},
78+
})
79+
// TODO: disable builtin devtools panel for vite devtools
80+
}
81+
6182
// Mainly for the injected runtime plugin to access the settings
6283
// Usage `import settings from '#build/devtools/settings'`
6384
addTemplate({

packages/devtools/src/runtime/plugins/view/client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ export async function setupDevToolsClient({
120120
try {
121121
iframe?.contentWindow?.__NUXT_DEVTOOLS_VIEW__?.setClient(client)
122122
}
123-
catch {
123+
catch (e) {
124124
// cross-origin
125+
console.error('[nuxt-devtools] Failed to connect view', e)
125126
}
126127
return client
127128
}

pnpm-lock.yaml

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ catalogs:
142142
'@types/which': ^3.0.4
143143
'@types/ws': ^8.18.1
144144
'@unhead/schema': ^2.0.19
145+
'@vitejs/devtools-kit': 0.0.0-alpha.6
145146
unimport: ^5.5.0
146147
onlyBuiltDependencies:
147148
- '@parcel/watcher'

0 commit comments

Comments
 (0)