Skip to content

Commit a7bd79b

Browse files
committedOct 21, 2023
Refactor output to immediately export default
1 parent 99fff06 commit a7bd79b

File tree

4 files changed

+42
-48
lines changed

4 files changed

+42
-48
lines changed
 

‎packages/mdx/lib/plugin/recma-document.js

+31-25
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@ export function recmaDocument(options) {
241241
child.expression.type === 'JSXFragment')
242242
) {
243243
content = true
244-
replacement.push(...createMdxContent(child.expression, Boolean(layout)))
244+
replacement.push(
245+
...createMdxContent(child.expression, outputFormat, Boolean(layout))
246+
)
245247
} else {
246248
// This catch-all branch is because plugins might add other things.
247249
// Normally, we only have import/export/jsx, but just add whatever’s
@@ -252,7 +254,9 @@ export function recmaDocument(options) {
252254

253255
// If there was no JSX content at all, add an empty function.
254256
if (!content) {
255-
replacement.push(...createMdxContent(undefined, Boolean(layout)))
257+
replacement.push(
258+
...createMdxContent(undefined, outputFormat, Boolean(layout))
259+
)
256260
}
257261

258262
exportedIdentifiers.push(['MDXContent', 'default'])
@@ -305,11 +309,6 @@ export function recmaDocument(options) {
305309
]
306310
}
307311
})
308-
} else {
309-
replacement.push({
310-
type: 'ExportDefaultDeclaration',
311-
declaration: {type: 'Identifier', name: 'MDXContent'}
312-
})
313312
}
314313

315314
tree.body = replacement
@@ -497,14 +496,16 @@ export function recmaDocument(options) {
497496
}
498497

499498
/**
500-
* @param {Readonly<Expression> | undefined} [content]
499+
* @param {Readonly<Expression> | undefined} content
501500
* Content.
501+
* @param {'function-body' | 'program'} outputFormat
502+
* Output format.
502503
* @param {boolean | undefined} [hasInternalLayout=false]
503504
* Whether there’s an internal layout (default: `false`).
504-
* @returns {Array<FunctionDeclaration>}
505+
* @returns {Array<ExportDefaultDeclaration | FunctionDeclaration>}
505506
* Functions.
506507
*/
507-
function createMdxContent(content, hasInternalLayout) {
508+
function createMdxContent(content, outputFormat, hasInternalLayout) {
508509
/** @type {JSXElement} */
509510
const element = {
510511
type: 'JSXElement',
@@ -597,6 +598,23 @@ export function recmaDocument(options) {
597598
}
598599
})
599600

601+
/** @type {FunctionDeclaration} */
602+
const declaration = {
603+
type: 'FunctionDeclaration',
604+
id: {type: 'Identifier', name: 'MDXContent'},
605+
params: [
606+
{
607+
type: 'AssignmentPattern',
608+
left: {type: 'Identifier', name: 'props'},
609+
right: {type: 'ObjectExpression', properties: []}
610+
}
611+
],
612+
body: {
613+
type: 'BlockStatement',
614+
body: [{type: 'ReturnStatement', argument: result}]
615+
}
616+
}
617+
600618
return [
601619
{
602620
type: 'FunctionDeclaration',
@@ -615,21 +633,9 @@ export function recmaDocument(options) {
615633
]
616634
}
617635
},
618-
{
619-
type: 'FunctionDeclaration',
620-
id: {type: 'Identifier', name: 'MDXContent'},
621-
params: [
622-
{
623-
type: 'AssignmentPattern',
624-
left: {type: 'Identifier', name: 'props'},
625-
right: {type: 'ObjectExpression', properties: []}
626-
}
627-
],
628-
body: {
629-
type: 'BlockStatement',
630-
body: [{type: 'ReturnStatement', argument: result}]
631-
}
632-
}
636+
outputFormat === 'program'
637+
? {type: 'ExportDefaultDeclaration', declaration}
638+
: declaration
633639
]
634640
}
635641
}

‎packages/mdx/readme.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,7 @@ Configuration for `createProcessor` (TypeScript type).
520520
import {Fragment as _Fragment, jsx as _jsx} from 'react/jsx-runtime'
521521
export {number} from 'https://a.full/data.js'
522522
function _createMdxContent(props) { /**/ }
523-
function MDXContent(props = {}) { /**/ }
524-
export default MDXContent
523+
export default function MDXContent(props = {}) { /**/ }
525524
```
526525

527526
</details>

‎packages/mdx/test/compile.js

+8-17
Original file line numberDiff line numberDiff line change
@@ -1058,11 +1058,10 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
10581058
' };',
10591059
' return <_components.p><_components.em>{"a"}</_components.em></_components.p>;',
10601060
'}',
1061-
'function MDXContent(props = {}) {',
1061+
'export default function MDXContent(props = {}) {',
10621062
' const {wrapper: MDXLayout} = props.components || ({});',
10631063
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',
10641064
'}',
1065-
'export default MDXContent;',
10661065
''
10671066
].join('\n')
10681067
)
@@ -1076,11 +1075,10 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
10761075
'function _createMdxContent(props) {',
10771076
' return <a {...b} c d="1" e={1} />;',
10781077
'}',
1079-
'function MDXContent(props = {}) {',
1078+
'export default function MDXContent(props = {}) {',
10801079
' const {wrapper: MDXLayout} = props.components || ({});',
10811080
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',
10821081
'}',
1083-
'export default MDXContent;',
10841082
''
10851083
].join('\n')
10861084
)
@@ -1099,11 +1097,10 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
10991097
' if (!c.d) _missingMdxReference("c.d", true);',
11001098
' return <><><a:b /><c.d /></></>;',
11011099
'}',
1102-
'function MDXContent(props = {}) {',
1100+
'export default function MDXContent(props = {}) {',
11031101
' const {wrapper: MDXLayout} = props.components || ({});',
11041102
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',
11051103
'}',
1106-
'export default MDXContent;',
11071104
'function _missingMdxReference(id, component) {',
11081105
' throw new Error("Expected " + (component ? "component" : "object") + " `" + id + "` to be defined: you likely forgot to import, pass, or provide it.");',
11091106
'}',
@@ -1122,11 +1119,10 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
11221119
'function _createMdxContent(props) {',
11231120
' return <><>{"a "}{}{" b"}</></>;',
11241121
'}',
1125-
'function MDXContent(props = {}) {',
1122+
'export default function MDXContent(props = {}) {',
11261123
' const {wrapper: MDXLayout} = props.components || ({});',
11271124
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',
11281125
'}',
1129-
'export default MDXContent;',
11301126
''
11311127
].join('\n')
11321128
)
@@ -1146,11 +1142,10 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
11461142
' }, _component0 = _components["a-b"];',
11471143
' return <>{<_component0></_component0>}</>;',
11481144
'}',
1149-
'function MDXContent(props = {}) {',
1145+
'export default function MDXContent(props = {}) {',
11501146
' const {wrapper: MDXLayout} = props.components || ({});',
11511147
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',
11521148
'}',
1153-
'export default MDXContent;',
11541149
''
11551150
].join('\n')
11561151
)
@@ -1169,11 +1164,10 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
11691164
' };',
11701165
' return <_components.p>{"Hello "}{props.name}</_components.p>;',
11711166
'}',
1172-
'function MDXContent(props = {}) {',
1167+
'export default function MDXContent(props = {}) {',
11731168
' const {wrapper: MDXLayout} = props.components || ({});',
11741169
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',
11751170
'}',
1176-
'export default MDXContent;',
11771171
''
11781172
].join('\n')
11791173
)
@@ -1201,10 +1195,9 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
12011195
' };',
12021196
' return <_components.p>{"a"}</_components.p>;',
12031197
'}',
1204-
'function MDXContent(props = {}) {',
1198+
'export default function MDXContent(props = {}) {',
12051199
' return <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout>;',
12061200
'}',
1207-
'export default MDXContent;',
12081201
''
12091202
].join('\n')
12101203
)
@@ -1232,15 +1225,13 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
12321225
' };',
12331226
' return <_components.p>{"a"}</_components.p>;',
12341227
'}',
1235-
'function MDXContent(props = {}) {',
1228+
'export default function MDXContent(props = {}) {',
12361229
' const {wrapper: MDXLayout} = {',
12371230
' ..._provideComponents(),',
12381231
' ...props.components',
12391232
' };',
12401233
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',
1241-
12421234
'}',
1243-
'export default MDXContent;',
12441235
''
12451236
].join('\n')
12461237
)

‎packages/mdx/test/syntax.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ test('@mdx-js/mdx: syntax: MDX (JSX)', async function (t) {
656656
' })',
657657
' });',
658658
'}',
659-
'function MDXContent(props = {}) {',
659+
'export default function MDXContent(props = {}) {',
660660
' const {wrapper: MDXLayout} = props.components || ({});',
661661
' return MDXLayout ? _jsx(MDXLayout, {',
662662
' ...props,',
@@ -665,7 +665,6 @@ test('@mdx-js/mdx: syntax: MDX (JSX)', async function (t) {
665665
' })',
666666
' }) : _createMdxContent(props);',
667667
'}',
668-
'export default MDXContent;',
669668
''
670669
].join('\n')
671670
)
@@ -880,7 +879,7 @@ test('@mdx-js/mdx: syntax: MDX (ESM)', async function (t) {
880879
' columnNumber: 1',
881880
' }, this);',
882881
'}',
883-
'function MDXContent(props = {}) {',
882+
'export default function MDXContent(props = {}) {',
884883
' const {wrapper: MDXLayout} = props.components || ({});',
885884
' return MDXLayout ? _jsxDEV(MDXLayout, {',
886885
' ...props,',
@@ -893,7 +892,6 @@ test('@mdx-js/mdx: syntax: MDX (ESM)', async function (t) {
893892
' fileName: "path/to/file.js"',
894893
' }, this) : _createMdxContent(props);',
895894
'}',
896-
'export default MDXContent;',
897895
'function _missingMdxReference(id, component, place) {',
898896
' throw new Error("Expected " + (component ? "component" : "object") + " `" + id + "` to be defined: you likely forgot to import, pass, or provide it." + (place ? "\\nIt’s referenced in your code at `" + place + "` in `path/to/file.js`" : ""));',
899897
'}',

0 commit comments

Comments
 (0)
Please sign in to comment.