From 80ca24841733502cd395ea53f05c9a181f8d97ff Mon Sep 17 00:00:00 2001 From: bluwy Date: Tue, 1 Aug 2023 23:16:53 +0800 Subject: [PATCH] Refactor MDX components exports handling --- .changeset/loud-candles-admire.md | 5 +++++ .changeset/young-roses-teach.md | 5 +++++ packages/astro/src/core/render/core.ts | 7 +------ packages/integrations/mdx/src/index.ts | 5 ++++- 4 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 .changeset/loud-candles-admire.md create mode 100644 .changeset/young-roses-teach.md diff --git a/.changeset/loud-candles-admire.md b/.changeset/loud-candles-admire.md new file mode 100644 index 000000000000..3fe1f12e7f92 --- /dev/null +++ b/.changeset/loud-candles-admire.md @@ -0,0 +1,5 @@ +--- +'@astrojs/mdx': patch +--- + +Handle `components` exports handling itself diff --git a/.changeset/young-roses-teach.md b/.changeset/young-roses-teach.md new file mode 100644 index 000000000000..ff9c08e56f21 --- /dev/null +++ b/.changeset/young-roses-teach.md @@ -0,0 +1,5 @@ +--- +'astro': major +--- + +Remove MDX special `components` export handling diff --git a/packages/astro/src/core/render/core.ts b/packages/astro/src/core/render/core.ts index d6228fbbefdf..3a8edde5c83d 100644 --- a/packages/astro/src/core/render/core.ts +++ b/packages/astro/src/core/render/core.ts @@ -59,12 +59,7 @@ async function renderPage({ mod, renderContext, env, cookies }: RenderPage) { locals: renderContext.locals ?? {}, }); - // Support `export const components` for `MDX` pages - if (typeof (mod as any).components === 'object') { - Object.assign(renderContext.props, { components: (mod as any).components }); - } - - let response = await runtimeRenderPage( + const response = await runtimeRenderPage( result, Component, renderContext.props, diff --git a/packages/integrations/mdx/src/index.ts b/packages/integrations/mdx/src/index.ts index 4d64a84d77df..ecf2a9a95950 100644 --- a/packages/integrations/mdx/src/index.ts +++ b/packages/integrations/mdx/src/index.ts @@ -171,11 +171,14 @@ export default function mdx(partialMdxOptions: Partial = {}): AstroI code += `\nexport const file = ${JSON.stringify(fileId)};`; } if (!moduleExports.find(({ n }) => n === 'Content')) { + // If have `export const components`, pass that as props to `Content` as fallback + const hasComponents = moduleExports.find(({ n }) => n === 'components'); + // Make `Content` the default export so we can wrap `MDXContent` and pass in `Fragment` code = code.replace('export default MDXContent;', ''); code += `\nexport const Content = (props = {}) => MDXContent({ ...props, - components: { Fragment, ...props.components }, + components: { Fragment${hasComponents ? ', ...components' : ''}, ...props.components }, }); export default Content;`; }