From 84af8ed9d1e6401c6ebc9c60fe8cddb44d5044b0 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Mon, 31 Jul 2023 21:51:37 +0800 Subject: [PATCH] Move MDX plugin re-ordering hack to MDX integration (#7872) --- .changeset/famous-queens-itch.md | 5 +++++ .changeset/rude-ears-play.md | 5 +++++ .changeset/unlucky-hotels-try.md | 5 +++++ packages/astro/src/core/create-vite.ts | 25 ------------------------- packages/integrations/mdx/package.json | 3 +++ packages/integrations/mdx/src/index.ts | 15 +++++++++++++++ 6 files changed, 33 insertions(+), 25 deletions(-) create mode 100644 .changeset/famous-queens-itch.md create mode 100644 .changeset/rude-ears-play.md create mode 100644 .changeset/unlucky-hotels-try.md diff --git a/.changeset/famous-queens-itch.md b/.changeset/famous-queens-itch.md new file mode 100644 index 000000000..e3538cdd6 --- /dev/null +++ b/.changeset/famous-queens-itch.md @@ -0,0 +1,5 @@ +--- +'@astrojs/mdx': patch +--- + +Re-orders the MDX plugin to run before Astro's JSX plugin diff --git a/.changeset/rude-ears-play.md b/.changeset/rude-ears-play.md new file mode 100644 index 000000000..660cfcb34 --- /dev/null +++ b/.changeset/rude-ears-play.md @@ -0,0 +1,5 @@ +--- +'astro': major +--- + +Remove MDX plugin re-ordering hack diff --git a/.changeset/unlucky-hotels-try.md b/.changeset/unlucky-hotels-try.md new file mode 100644 index 000000000..359a239e2 --- /dev/null +++ b/.changeset/unlucky-hotels-try.md @@ -0,0 +1,5 @@ +--- +'@astrojs/mdx': minor +--- + +Add `astro` as peer dependency diff --git a/packages/astro/src/core/create-vite.ts b/packages/astro/src/core/create-vite.ts index 0fb64ef69..efb78d137 100644 --- a/packages/astro/src/core/create-vite.ts +++ b/packages/astro/src/core/create-vite.ts @@ -234,37 +234,12 @@ export async function createVite( result = vite.mergeConfig(result, settings.config.vite || {}); } result = vite.mergeConfig(result, commandConfig); - if (result.plugins) { - sortPlugins(result.plugins); - } result.customLogger = vite.createLogger(result.logLevel ?? 'warn'); return result; } -function isVitePlugin(plugin: vite.PluginOption): plugin is vite.Plugin { - return Boolean(plugin?.hasOwnProperty('name')); -} - -function findPluginIndexByName(pluginOptions: vite.PluginOption[], name: string): number { - return pluginOptions.findIndex(function (pluginOption) { - // Use isVitePlugin to ignore nulls, booleans, promises, and arrays - // CAUTION: could be a problem if a plugin we're searching for becomes async! - return isVitePlugin(pluginOption) && pluginOption.name === name; - }); -} - -function sortPlugins(pluginOptions: vite.PluginOption[]) { - // HACK: move mdxPlugin to top because it needs to run before internal JSX plugin - const mdxPluginIndex = findPluginIndexByName(pluginOptions, '@mdx-js/rollup'); - if (mdxPluginIndex === -1) return; - const jsxPluginIndex = findPluginIndexByName(pluginOptions, 'astro:jsx'); - const mdxPlugin = pluginOptions[mdxPluginIndex]; - pluginOptions.splice(mdxPluginIndex, 1); - pluginOptions.splice(jsxPluginIndex, 0, mdxPlugin); -} - const COMMON_DEPENDENCIES_NOT_ASTRO = [ 'autoprefixer', 'react', diff --git a/packages/integrations/mdx/package.json b/packages/integrations/mdx/package.json index a10249d53..72e51d029 100644 --- a/packages/integrations/mdx/package.json +++ b/packages/integrations/mdx/package.json @@ -53,6 +53,9 @@ "unist-util-visit": "^4.1.2", "vfile": "^5.3.7" }, + "peerDependencies": { + "astro": "workspace:^2.9.6" + }, "devDependencies": { "@types/chai": "^4.3.5", "@types/estree": "^1.0.1", diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts index 1d0320213..4d64a84d7 100644 --- a/packages/integrations/mdx/src/index.ts +++ b/packages/integrations/mdx/src/index.ts @@ -95,6 +95,21 @@ export default function mdx(partialMdxOptions: Partial = {}): AstroI enforce: 'pre', configResolved(resolved) { importMetaEnv = { ...importMetaEnv, ...resolved.env }; + + // HACK: move ourselves before Astro's JSX plugin to transform things in the right order + const jsxPluginIndex = resolved.plugins.findIndex((p) => p.name === 'astro:jsx'); + if (jsxPluginIndex !== -1) { + const myPluginIndex = resolved.plugins.findIndex( + (p) => p.name === '@mdx-js/rollup' + ); + if (myPluginIndex !== -1) { + const myPlugin = resolved.plugins[myPluginIndex]; + // @ts-ignore-error ignore readonly annotation + resolved.plugins.splice(myPluginIndex, 1); + // @ts-ignore-error ignore readonly annotation + resolved.plugins.splice(jsxPluginIndex, 0, myPlugin); + } + } }, // Override transform to alter code before MDX compilation // ex. inject layouts