|
| 1 | +import type { PluginWithDevTools } from '@vitejs/devtools-kit' |
| 2 | +import type { NuxtDevtoolsServerContext } from '../types' |
| 3 | +import { addPluginTemplate, addVitePlugin } from '@nuxt/kit' |
| 4 | +import { vueDevtools } from 'vite-plugin-vue-devtools' |
| 5 | +import { skipInSSR } from '../server-rpc/skip-in-ssr' |
| 6 | + |
| 7 | +export function setup({ nuxt }: NuxtDevtoolsServerContext) { |
| 8 | + if (!nuxt.options.dev || nuxt.options.test) |
| 9 | + return |
| 10 | + |
| 11 | + // Added isomorphically — `{ server: false }` would strip the `devtools` |
| 12 | + // property (see the note in `module-main.ts`) — so guard the dock |
| 13 | + // registration against Nuxt's SSR Vite instance, which would otherwise |
| 14 | + // register a duplicate `vue-devtools` dock. Same pattern as the module's |
| 15 | + // own dock registration. |
| 16 | + // `vueDevtools()` always produces plain plugin objects; its declared |
| 17 | + // `PluginOption[]` return type is wider than that. |
| 18 | + const plugins = (vueDevtools() as PluginWithDevTools[]).map((plugin) => { |
| 19 | + const { devtools } = plugin |
| 20 | + if (!devtools?.setup) |
| 21 | + return plugin |
| 22 | + return { |
| 23 | + ...plugin, |
| 24 | + devtools: { |
| 25 | + ...devtools, |
| 26 | + setup: (ctx: Parameters<typeof devtools.setup>[0]) => |
| 27 | + skipInSSR(ctx) ? undefined : devtools.setup(ctx), |
| 28 | + }, |
| 29 | + } |
| 30 | + }) |
| 31 | + addVitePlugin(plugins) |
| 32 | + |
| 33 | + // Nuxt has no Vite HTML entry for the plugin's own injection to hook into; |
| 34 | + // load the Vue DevTools client hook through an early Nuxt client plugin |
| 35 | + // instead. |
| 36 | + addPluginTemplate({ |
| 37 | + filename: 'devtools/vue-devtools-client.mjs', |
| 38 | + name: 'vue-devtools-client', |
| 39 | + mode: 'client', |
| 40 | + order: -1_000, |
| 41 | + getContents: () => [ |
| 42 | + `import 'virtual:vue-devtools-client'`, |
| 43 | + ``, |
| 44 | + `export default () => {}`, |
| 45 | + ``, |
| 46 | + ].join('\n'), |
| 47 | + }) |
| 48 | +} |
0 commit comments