Skip to content

Commit

Permalink
Support JSX development runtime (fixes #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradlc committed Dec 17, 2022
1 parent 20f5e89 commit 1595aee
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 9 deletions.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ export const mdxAnnotations = {
return (tree) => {
estreeVisit(tree, (node) => {
if (node.type !== 'CallExpression') return
if (node.callee.name !== '_jsxs' && node.callee.name !== '_jsx') return
if (
node.callee.name !== '_jsxs' &&
node.callee.name !== '_jsx' &&
node.callee.name !== '_jsxDEV'
)
return

let propsNode = node.arguments[1]
if (propsNode?.type !== 'ObjectExpression') return
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"test": "node test.js"
},
"devDependencies": {
"@mdx-js/mdx": "^2.1.5",
"@mdx-js/mdx": "^2.2.1",
"remark-gfm": "^3.0.1",
"uvu": "^0.5.6"
},
Expand Down
36 changes: 36 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,42 @@ export default MDXContent;`
)
})

test('dev runtime', async () => {
let compiled = await compile("# Hello {{ foo: 'bar' }}", { development: true })

assert.equal(
compiled,
`/*@jsxRuntime automatic @jsxImportSource react*/
import {jsxDEV as _jsxDEV} from "react/jsx-dev-runtime";
function _createMdxContent(props) {
const _components = Object.assign({
h1: "h1"
}, props.components);
return _jsxDEV(_components.h1, {
children: "Hello",
...{
foo: 'bar'
}
}, undefined, false, {
fileName: "<source.js>",
lineNumber: 1,
columnNumber: 1
}, this);
}
function MDXContent(props = {}) {
const {wrapper: MDXLayout} = props.components || ({});
return MDXLayout ? _jsxDEV(MDXLayout, Object.assign({}, props, {
children: _jsxDEV(_createMdxContent, props, undefined, false, {
fileName: "<source.js>"
}, this)
}), undefined, false, {
fileName: "<source.js>"
}, this) : _createMdxContent(props);
}
export default MDXContent;`
)
})

test('gfm', async () => {
let compiled = await compile(
[
Expand Down

0 comments on commit 1595aee

Please sign in to comment.