Skip to content

Commit a9d93b2

Browse files
committed
fix: only add _componentX to _createMdxContent
1 parent 55414b4 commit a9d93b2

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

packages/mdx/lib/plugin/recma-jsx-rewrite.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -358,23 +358,25 @@ export function recmaJsxRewrite(options = {}) {
358358
componentsInit = {type: 'Identifier', name: '_components'}
359359
}
360360

361-
for (const [id, componentName] of idToInvalidComponentName) {
362-
// For JSX IDs that can’t be represented as JavaScript IDs (as in,
363-
// those with dashes, such as `custom-element`), generate a
364-
// separate variable that is a valid JS ID (such as `_component0`),
365-
// and takes it from components:
366-
// `const _component0 = _components['custom-element']`
367-
declarations.push({
368-
type: 'VariableDeclarator',
369-
id: {type: 'Identifier', name: componentName},
370-
init: {
371-
type: 'MemberExpression',
372-
object: {type: 'Identifier', name: '_components'},
373-
property: {type: 'Literal', value: id},
374-
computed: true,
375-
optional: false
376-
}
377-
})
361+
if (isNamedFunction(scope.node, '_createMdxContent')) {
362+
for (const [id, componentName] of idToInvalidComponentName) {
363+
// For JSX IDs that can’t be represented as JavaScript IDs (as in,
364+
// those with dashes, such as `custom-element`), generate a
365+
// separate variable that is a valid JS ID (such as `_component0`),
366+
// and takes it from components:
367+
// `const _component0 = _components['custom-element']`
368+
declarations.push({
369+
type: 'VariableDeclarator',
370+
id: {type: 'Identifier', name: componentName},
371+
init: {
372+
type: 'MemberExpression',
373+
object: {type: 'Identifier', name: '_components'},
374+
property: {type: 'Literal', value: id},
375+
computed: true,
376+
optional: false
377+
}
378+
})
379+
}
378380
}
379381

380382
if (componentsPattern) {

packages/mdx/test/compile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ test('jsx', async () => {
864864
' return <>{<_component0></_component0>}</>;',
865865
'}',
866866
'function MDXContent(props = {}) {',
867-
' const _component0 = _components["a-b"], {wrapper: MDXLayout} = props.components || ({});',
867+
' const {wrapper: MDXLayout} = props.components || ({});',
868868
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',
869869
'}',
870870
'export default MDXContent;',

0 commit comments

Comments
 (0)