Skip to content

Commit c8da29b

Browse files
authored
feat(devtools): make vite-plugin-inspect an optional peer dependency (#1055)
1 parent 7b99f91 commit c8da29b

6 files changed

Lines changed: 54 additions & 8 deletions

File tree

‎docs/content/1.guide/1.features.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ Virtual Files tab shows the virtual files generated by Nuxt to support the conve
161161

162162
Inspect expose the [`vite-plugin-inspect`](https://github.com/antfu/vite-plugin-inspect) integration, allowing you to inspect transformation steps of Vite.
163163

164+
`vite-plugin-inspect` is an optional peer dependency, so it is not installed by default. Until you add it to your project, DevTools shows an install launcher in the Inspect slot (the same experience as Vite Plus DevTools) — click it to install the plugin and restart the dev server to activate the view.
165+
164166
<img width="1284" src="https://user-images.githubusercontent.com/11247099/217670818-77f91135-7318-462e-9148-4ad504c82555.png" alt="Nuxt DevTools Inspect tab">
165167

166168
## Settings

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ export interface ModuleOptions {
4343
vueDevTools?: boolean
4444

4545
/**
46-
* Enable vite-plugin-inspect
46+
* Enable the Vite Inspect integration.
47+
*
48+
* `vite-plugin-inspect` is an optional peer dependency. When it isn't
49+
* installed, DevTools shows an install launcher in its place (like Vite Plus
50+
* DevTools); once installed, the real Inspect view is mounted. Set this to
51+
* `false` to disable the integration (and its launcher) entirely.
4752
*
4853
* @default true
4954
*/

‎packages/devtools/package.json‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@
4545
"peerDependencies": {
4646
"nitro": "^3.0.0-0",
4747
"nitropack": "^2.0.0",
48-
"vite": "^8.1.5"
48+
"vite": "^8.1.5",
49+
"vite-plugin-inspect": "catalog:prod"
4950
},
5051
"peerDependenciesMeta": {
5152
"nitro": {
5253
"optional": true
5354
},
5455
"nitropack": {
5556
"optional": true
57+
},
58+
"vite-plugin-inspect": {
59+
"optional": true
5660
}
5761
},
5862
"dependencies": {
@@ -85,7 +89,6 @@
8589
"tinyglobby": "catalog:prod",
8690
"unstorage": "catalog:prod",
8791
"verkit": "catalog:prod",
88-
"vite-plugin-inspect": "catalog:prod",
8992
"vite-plugin-vue-tracer": "catalog:prod",
9093
"ws": "catalog:prod"
9194
},
@@ -136,6 +139,7 @@
136139
"vanilla-jsoneditor": "catalog:frontend",
137140
"vis-data": "catalog:frontend",
138141
"vis-network": "catalog:frontend",
142+
"vite-plugin-inspect": "catalog:prod",
139143
"vue-tsc": "catalog:cli",
140144
"vue-virtual-scroller": "catalog:frontend"
141145
}

‎packages/devtools/src/integrations/analyze-build.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import type { AnalyzeBuildMeta, ModuleOptions } from '../types'
33
import { addVitePlugin } from '@nuxt/kit'
44
import { join } from 'pathe'
55
import { getFolderSize } from '../utils/fs'
6-
import { createVitePluginInspect } from './vite-inspect'
6+
import { createVitePluginInspect, isVitePluginInspectAvailable } from './vite-inspect'
77

88
export async function setup(nuxt: Nuxt, options: ModuleOptions) {
9-
if (options.viteInspect !== false) {
9+
// Only wire the Vite Inspect build report when the optional
10+
// `vite-plugin-inspect` peer dependency is actually installed.
11+
if (options.viteInspect !== false && isVitePluginInspectAvailable(nuxt.options.rootDir)) {
1012
addVitePlugin(
1113
await createVitePluginInspect({
1214
build: true,

‎packages/devtools/src/integrations/vite-inspect.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,49 @@ import type { ViteInspectOptions } from 'vite-plugin-inspect'
33
import type { NuxtDevtoolsServerContext, ServerFunctions } from '../types'
44
import { onDevtoolsReady } from '@nuxt/devtools-kit'
55
import { addVitePlugin, logger } from '@nuxt/kit'
6+
import { createInstallLauncher } from '@vitejs/devtools-kit/node'
7+
import { isPackageExists } from 'local-pkg'
68
import { RPC_NAMESPACE } from '../rpc-namespace'
79

810
const VERSION_QUERY_RE = /\?v=\w+$/
911
const VUE_EXT_RE = /\.vue($|\?v=)/
1012

13+
// `vite-plugin-inspect` registers its own dock under this id/title/icon. The
14+
// install launcher we mount when the package is absent reuses them so the rail
15+
// slot stays stable before and after the user installs it.
16+
const INSPECT_DOCK_ID = 'vite-plugin-inspect'
17+
const INSPECT_DOCK_TITLE = 'Inspect'
18+
const INSPECT_DOCK_ICON = 'ph:magnifying-glass-duotone'
19+
20+
/**
21+
* Whether the optional `vite-plugin-inspect` peer dependency is installed in
22+
* the user's project.
23+
*/
24+
export function isVitePluginInspectAvailable(rootDir: string): boolean {
25+
return isPackageExists('vite-plugin-inspect', { paths: [rootDir] })
26+
}
27+
1128
export async function createVitePluginInspect(options?: ViteInspectOptions): Promise<Plugin> {
1229
return await import('vite-plugin-inspect').then(r => r.default(options))
1330
}
1431

1532
export async function setup(ctx: NuxtDevtoolsServerContext) {
33+
// `vite-plugin-inspect` is an optional peer dependency. When the user hasn't
34+
// installed it, mount a discovery/install launcher in its dock slot (the same
35+
// UX as Vite Plus DevTools' built-in integration launchers) instead of the
36+
// real Inspect view. Clicking it installs the package as a tracked terminal
37+
// session, then asks for a dev-server restart to activate the real plugin.
38+
if (!isVitePluginInspectAvailable(ctx.nuxt.options.rootDir)) {
39+
addVitePlugin(createInstallLauncher({
40+
id: INSPECT_DOCK_ID,
41+
title: INSPECT_DOCK_TITLE,
42+
icon: INSPECT_DOCK_ICON,
43+
label: 'Vite Inspect',
44+
install: ['vite-plugin-inspect'],
45+
}))
46+
return
47+
}
48+
1649
const plugin = await createVitePluginInspect()
1750
addVitePlugin(plugin)
1851

‎pnpm-lock.yaml‎

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

0 commit comments

Comments
 (0)