From 58a22585e71be2bee72d1b2c27c54c18ea5a9789 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Tue, 12 Jul 2022 12:44:31 -0400 Subject: [PATCH] fix: add type check for plugin.name --- packages/astro/src/core/create-vite.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 848077ffd..ed504bc4b 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -136,19 +136,24 @@ export async function createVite( return result; } -function getPluginName(plugin: vite.PluginOption) { +function getPluginName(plugin: vite.Plugin) { if (plugin && typeof plugin === 'object' && !Array.isArray(plugin)) { return plugin.name; } } +function isVitePlugin(plugin: vite.PluginOption): plugin is vite.Plugin { + return Boolean(plugin?.hasOwnProperty('name')) +} + function sortPlugins(result: ViteConfigWithSSR) { + const plugins = result.plugins?.filter(isVitePlugin) ?? [] // HACK: move mdxPlugin to top because it needs to run before internal JSX plugin const mdxPluginIndex = - result.plugins?.findIndex((plugin) => getPluginName(plugin) === '@mdx-js/rollup') ?? -1; + plugins.findIndex((plugin) => getPluginName(plugin) === '@mdx-js/rollup') ?? -1; if (mdxPluginIndex === -1) return; const jsxPluginIndex = - result.plugins?.findIndex((plugin) => getPluginName(plugin) === 'astro:jsx') ?? -1; + plugins.findIndex((plugin) => getPluginName(plugin) === 'astro:jsx') ?? -1; const mdxPlugin = result.plugins?.[mdxPluginIndex]; result.plugins?.splice(mdxPluginIndex, 1); result.plugins?.splice(jsxPluginIndex, 0, mdxPlugin);