From 09c8c373c8c9162f0cec7a5a72b53026a362ad7b Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 18 Apr 2023 17:01:10 +0100 Subject: [PATCH] fix(nuxt-vitest): inject vue plugins if missing (#130) --- src/nuxt-vitest/config.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/nuxt-vitest/config.ts b/src/nuxt-vitest/config.ts index 1d06be93a..95cfe7e7e 100644 --- a/src/nuxt-vitest/config.ts +++ b/src/nuxt-vitest/config.ts @@ -1,6 +1,8 @@ -import type { Nuxt } from '@nuxt/schema' +import type { Nuxt, ViteConfig } from '@nuxt/schema' import type { InlineConfig as VitestConfig } from 'vitest' import { InlineConfig, mergeConfig, defineConfig } from 'vite' +import vuePlugin from '@vitejs/plugin-vue' +import viteJsxPlugin from '@vitejs/plugin-vue-jsx' interface GetVitestConfigOptions { nuxt: Nuxt @@ -46,11 +48,25 @@ async function startNuxtAndGetViteConfig(rootDir = process.cwd()) { return promise } +const vuePlugins = { + 'vite:vue': [vuePlugin, 'vue'], + 'vite:vue-jsx': [viteJsxPlugin, 'vueJsx'], +} as const + export async function getVitestConfigFromNuxt( options?: GetVitestConfigOptions ): Promise { if (!options) options = await startNuxtAndGetViteConfig() - options.viteConfig.plugins = options.viteConfig.plugins?.filter(p => (p as any)?.name !== 'nuxt:import-protection') + options.viteConfig.plugins = options.viteConfig.plugins || [] + options.viteConfig.plugins = options.viteConfig.plugins.filter(p => (p as any)?.name !== 'nuxt:import-protection') + + for (const name in vuePlugins) { + if (!options.viteConfig.plugins?.some(p => (p as any)?.name === name)) { + const [plugin, key] = vuePlugins[name as keyof typeof vuePlugins] + // @ts-expect-error mismatching component options + options.viteConfig.plugins.push(plugin((options.viteConfig as ViteConfig)[key])) + } + } return { ...options.viteConfig,