Skip to content

Commit

Permalink
fix: only add _componentX to _createMdxContent
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Aug 15, 2022
1 parent 55414b4 commit a9d93b2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
36 changes: 19 additions & 17 deletions packages/mdx/lib/plugin/recma-jsx-rewrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,23 +358,25 @@ export function recmaJsxRewrite(options = {}) {
componentsInit = {type: 'Identifier', name: '_components'}
}

for (const [id, componentName] of idToInvalidComponentName) {
// For JSX IDs that can’t be represented as JavaScript IDs (as in,
// those with dashes, such as `custom-element`), generate a
// separate variable that is a valid JS ID (such as `_component0`),
// and takes it from components:
// `const _component0 = _components['custom-element']`
declarations.push({
type: 'VariableDeclarator',
id: {type: 'Identifier', name: componentName},
init: {
type: 'MemberExpression',
object: {type: 'Identifier', name: '_components'},
property: {type: 'Literal', value: id},
computed: true,
optional: false
}
})
if (isNamedFunction(scope.node, '_createMdxContent')) {
for (const [id, componentName] of idToInvalidComponentName) {
// For JSX IDs that can’t be represented as JavaScript IDs (as in,
// those with dashes, such as `custom-element`), generate a
// separate variable that is a valid JS ID (such as `_component0`),
// and takes it from components:
// `const _component0 = _components['custom-element']`
declarations.push({
type: 'VariableDeclarator',
id: {type: 'Identifier', name: componentName},
init: {
type: 'MemberExpression',
object: {type: 'Identifier', name: '_components'},
property: {type: 'Literal', value: id},
computed: true,
optional: false
}
})
}
}

if (componentsPattern) {
Expand Down
2 changes: 1 addition & 1 deletion packages/mdx/test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ test('jsx', async () => {
' return <>{<_component0></_component0>}</>;',
'}',
'function MDXContent(props = {}) {',
' const _component0 = _components["a-b"], {wrapper: MDXLayout} = props.components || ({});',
' const {wrapper: MDXLayout} = props.components || ({});',
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',
'}',
'export default MDXContent;',
Expand Down

0 comments on commit a9d93b2

Please sign in to comment.