Skip to content

Commit

Permalink
fix: resolve plugins for proper sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Jul 15, 2022
1 parent 4bc56fb commit e67c194
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit e67c194

Please sign in to comment.