From e67c194d3a8e11070487ed325947e7c59e8d69cd Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Wed, 13 Jul 2022 12:09:59 -0400 Subject: [PATCH] fix: resolve plugins for proper sorting --- packages/astro/src/core/create-vite.ts | 34 ++++++++++++-------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 5ac0db3c3a711..04add3d326f2c 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -126,30 +126,26 @@ export async function createVite( let result = commonConfig; result = vite.mergeConfig(result, astroConfig.vite || {}); result = vite.mergeConfig(result, commandConfig); - sortPlugins(result); - return result; -} + // Resolve plugin config to remove nulls, undefined's, nested arrays, promises, + // and anything else Vite supports in the future! + const { plugins: resolvedPlugins } = await vite.resolveConfig(result, mode === 'dev' ? 'serve' : 'build'); + result.plugins = sortPlugins(resolvedPlugins); -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')) + return result; } -function sortPlugins(result: ViteConfigWithSSR) { - const plugins = result.plugins?.filter(isVitePlugin) ?? [] +function sortPlugins(plugins: readonly vite.Plugin[]): vite.Plugin[] { + const result = [...plugins]; // HACK: move mdxPlugin to top because it needs to run before internal JSX plugin const mdxPluginIndex = - plugins.findIndex((plugin) => getPluginName(plugin) === '@mdx-js/rollup') ?? -1; - if (mdxPluginIndex === -1) return; + plugins.findIndex((plugin) => plugin.name === '@mdx-js/rollup') ?? -1; + if (mdxPluginIndex === -1) return result; const jsxPluginIndex = - plugins.findIndex((plugin) => getPluginName(plugin) === 'astro:jsx') ?? -1; - const mdxPlugin = result.plugins?.[mdxPluginIndex]; - result.plugins?.splice(mdxPluginIndex, 1); - result.plugins?.splice(jsxPluginIndex, 0, mdxPlugin); + plugins.findIndex((plugin) => plugin.name === 'astro:jsx') ?? -1; + + const mdxPlugin = plugins[mdxPluginIndex]; + result.splice(mdxPluginIndex, 1); + result.splice(jsxPluginIndex, 0, mdxPlugin); + return result; }