Skip to content

Commit

Permalink
Refactor output to immediately export default
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 21, 2023
1 parent 99fff06 commit a7bd79b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 48 deletions.
56 changes: 31 additions & 25 deletions packages/mdx/lib/plugin/recma-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ export function recmaDocument(options) {
child.expression.type === 'JSXFragment')
) {
content = true
replacement.push(...createMdxContent(child.expression, Boolean(layout)))
replacement.push(
...createMdxContent(child.expression, outputFormat, Boolean(layout))
)
} else {
// This catch-all branch is because plugins might add other things.
// Normally, we only have import/export/jsx, but just add whatever’s
Expand All @@ -252,7 +254,9 @@ export function recmaDocument(options) {

// If there was no JSX content at all, add an empty function.
if (!content) {
replacement.push(...createMdxContent(undefined, Boolean(layout)))
replacement.push(
...createMdxContent(undefined, outputFormat, Boolean(layout))
)
}

exportedIdentifiers.push(['MDXContent', 'default'])
Expand Down Expand Up @@ -305,11 +309,6 @@ export function recmaDocument(options) {
]
}
})
} else {
replacement.push({
type: 'ExportDefaultDeclaration',
declaration: {type: 'Identifier', name: 'MDXContent'}
})
}

tree.body = replacement
Expand Down Expand Up @@ -497,14 +496,16 @@ export function recmaDocument(options) {
}

/**
* @param {Readonly<Expression> | undefined} [content]
* @param {Readonly<Expression> | undefined} content
* Content.
* @param {'function-body' | 'program'} outputFormat
* Output format.
* @param {boolean | undefined} [hasInternalLayout=false]
* Whether there’s an internal layout (default: `false`).
* @returns {Array<FunctionDeclaration>}
* @returns {Array<ExportDefaultDeclaration | FunctionDeclaration>}
* Functions.
*/
function createMdxContent(content, hasInternalLayout) {
function createMdxContent(content, outputFormat, hasInternalLayout) {
/** @type {JSXElement} */
const element = {
type: 'JSXElement',
Expand Down Expand Up @@ -597,6 +598,23 @@ export function recmaDocument(options) {
}
})

/** @type {FunctionDeclaration} */
const declaration = {
type: 'FunctionDeclaration',
id: {type: 'Identifier', name: 'MDXContent'},
params: [
{
type: 'AssignmentPattern',
left: {type: 'Identifier', name: 'props'},
right: {type: 'ObjectExpression', properties: []}
}
],
body: {
type: 'BlockStatement',
body: [{type: 'ReturnStatement', argument: result}]
}
}

return [
{
type: 'FunctionDeclaration',
Expand All @@ -615,21 +633,9 @@ export function recmaDocument(options) {
]
}
},
{
type: 'FunctionDeclaration',
id: {type: 'Identifier', name: 'MDXContent'},
params: [
{
type: 'AssignmentPattern',
left: {type: 'Identifier', name: 'props'},
right: {type: 'ObjectExpression', properties: []}
}
],
body: {
type: 'BlockStatement',
body: [{type: 'ReturnStatement', argument: result}]
}
}
outputFormat === 'program'
? {type: 'ExportDefaultDeclaration', declaration}
: declaration
]
}
}
3 changes: 1 addition & 2 deletions packages/mdx/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,7 @@ Configuration for `createProcessor` (TypeScript type).
import {Fragment as _Fragment, jsx as _jsx} from 'react/jsx-runtime'
export {number} from 'https://a.full/data.js'
function _createMdxContent(props) { /**/ }
function MDXContent(props = {}) { /**/ }
export default MDXContent
export default function MDXContent(props = {}) { /**/ }
```

</details>
Expand Down
25 changes: 8 additions & 17 deletions packages/mdx/test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1058,11 +1058,10 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
' };',
' return <_components.p><_components.em>{"a"}</_components.em></_components.p>;',
'}',
'function MDXContent(props = {}) {',
'export default function MDXContent(props = {}) {',
' const {wrapper: MDXLayout} = props.components || ({});',
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',
'}',
'export default MDXContent;',
''
].join('\n')
)
Expand All @@ -1076,11 +1075,10 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
'function _createMdxContent(props) {',
' return <a {...b} c d="1" e={1} />;',
'}',
'function MDXContent(props = {}) {',
'export default function MDXContent(props = {}) {',
' const {wrapper: MDXLayout} = props.components || ({});',
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',
'}',
'export default MDXContent;',
''
].join('\n')
)
Expand All @@ -1099,11 +1097,10 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
' if (!c.d) _missingMdxReference("c.d", true);',
' return <><><a:b /><c.d /></></>;',
'}',
'function MDXContent(props = {}) {',
'export default function MDXContent(props = {}) {',
' const {wrapper: MDXLayout} = props.components || ({});',
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',
'}',
'export default MDXContent;',
'function _missingMdxReference(id, component) {',
' throw new Error("Expected " + (component ? "component" : "object") + " `" + id + "` to be defined: you likely forgot to import, pass, or provide it.");',
'}',
Expand All @@ -1122,11 +1119,10 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
'function _createMdxContent(props) {',
' return <><>{"a "}{}{" b"}</></>;',
'}',
'function MDXContent(props = {}) {',
'export default function MDXContent(props = {}) {',
' const {wrapper: MDXLayout} = props.components || ({});',
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',
'}',
'export default MDXContent;',
''
].join('\n')
)
Expand All @@ -1146,11 +1142,10 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
' }, _component0 = _components["a-b"];',
' return <>{<_component0></_component0>}</>;',
'}',
'function MDXContent(props = {}) {',
'export default function MDXContent(props = {}) {',
' const {wrapper: MDXLayout} = props.components || ({});',
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',
'}',
'export default MDXContent;',
''
].join('\n')
)
Expand All @@ -1169,11 +1164,10 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
' };',
' return <_components.p>{"Hello "}{props.name}</_components.p>;',
'}',
'function MDXContent(props = {}) {',
'export default function MDXContent(props = {}) {',
' const {wrapper: MDXLayout} = props.components || ({});',
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',
'}',
'export default MDXContent;',
''
].join('\n')
)
Expand Down Expand Up @@ -1201,10 +1195,9 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
' };',
' return <_components.p>{"a"}</_components.p>;',
'}',
'function MDXContent(props = {}) {',
'export default function MDXContent(props = {}) {',
' return <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout>;',
'}',
'export default MDXContent;',
''
].join('\n')
)
Expand Down Expand Up @@ -1232,15 +1225,13 @@ test('@mdx-js/mdx: compile (JSX)', async function (t) {
' };',
' return <_components.p>{"a"}</_components.p>;',
'}',
'function MDXContent(props = {}) {',
'export default function MDXContent(props = {}) {',
' const {wrapper: MDXLayout} = {',
' ..._provideComponents(),',
' ...props.components',
' };',
' return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);',

'}',
'export default MDXContent;',
''
].join('\n')
)
Expand Down
6 changes: 2 additions & 4 deletions packages/mdx/test/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ test('@mdx-js/mdx: syntax: MDX (JSX)', async function (t) {
' })',
' });',
'}',
'function MDXContent(props = {}) {',
'export default function MDXContent(props = {}) {',
' const {wrapper: MDXLayout} = props.components || ({});',
' return MDXLayout ? _jsx(MDXLayout, {',
' ...props,',
Expand All @@ -665,7 +665,6 @@ test('@mdx-js/mdx: syntax: MDX (JSX)', async function (t) {
' })',
' }) : _createMdxContent(props);',
'}',
'export default MDXContent;',
''
].join('\n')
)
Expand Down Expand Up @@ -880,7 +879,7 @@ test('@mdx-js/mdx: syntax: MDX (ESM)', async function (t) {
' columnNumber: 1',
' }, this);',
'}',
'function MDXContent(props = {}) {',
'export default function MDXContent(props = {}) {',
' const {wrapper: MDXLayout} = props.components || ({});',
' return MDXLayout ? _jsxDEV(MDXLayout, {',
' ...props,',
Expand All @@ -893,7 +892,6 @@ test('@mdx-js/mdx: syntax: MDX (ESM)', async function (t) {
' fileName: "path/to/file.js"',
' }, this) : _createMdxContent(props);',
'}',
'export default MDXContent;',
'function _missingMdxReference(id, component, place) {',
' 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`" : ""));',
'}',
Expand Down

0 comments on commit a7bd79b

Please sign in to comment.