Skip to content

Commit 32669cd

Browse files
bluwyematipico
authored andcommitted
Refactor MDX components exports handling (#7904)
1 parent c0de7a7 commit 32669cd

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

.changeset/loud-candles-admire.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/mdx': patch
3+
---
4+
5+
Handle `components` exports handling itself

.changeset/young-roses-teach.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': major
3+
---
4+
5+
Remove MDX special `components` export handling

packages/astro/src/core/render/core.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,7 @@ async function renderPage({ mod, renderContext, env, cookies }: RenderPage) {
5959
locals: renderContext.locals ?? {},
6060
});
6161

62-
// Support `export const components` for `MDX` pages
63-
if (typeof (mod as any).components === 'object') {
64-
Object.assign(renderContext.props, { components: (mod as any).components });
65-
}
66-
67-
let response = await runtimeRenderPage(
62+
const response = await runtimeRenderPage(
6863
result,
6964
Component,
7065
renderContext.props,

packages/integrations/mdx/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,14 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
171171
code += `\nexport const file = ${JSON.stringify(fileId)};`;
172172
}
173173
if (!moduleExports.find(({ n }) => n === 'Content')) {
174+
// If have `export const components`, pass that as props to `Content` as fallback
175+
const hasComponents = moduleExports.find(({ n }) => n === 'components');
176+
174177
// Make `Content` the default export so we can wrap `MDXContent` and pass in `Fragment`
175178
code = code.replace('export default MDXContent;', '');
176179
code += `\nexport const Content = (props = {}) => MDXContent({
177180
...props,
178-
components: { Fragment, ...props.components },
181+
components: { Fragment${hasComponents ? ', ...components' : ''}, ...props.components },
179182
});
180183
export default Content;`;
181184
}

0 commit comments

Comments
 (0)