Skip to content

Commit

Permalink
Remove unneeded pragma comment after transform
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Oct 24, 2023
1 parent 765310c commit f48d038
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 16 deletions.
1 change: 0 additions & 1 deletion docs/docs/using-mdx.mdx
Expand Up @@ -62,7 +62,6 @@ Some observations:
The *actual* output is:

```tsx path="output-actual.js"
/* @jsxRuntime automatic @jsxImportSource react */
import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from 'react/jsx-runtime'

export function Thing() {
Expand Down
6 changes: 5 additions & 1 deletion packages/mdx/lib/plugin/recma-document.js
Expand Up @@ -103,7 +103,11 @@ export function recmaDocument(options) {
if (!tree.comments) tree.comments = []

if (pragmas.length > 0) {
tree.comments.unshift({type: 'Block', value: pragmas.join(' ')})
tree.comments.unshift({
type: 'Block',
value: pragmas.join(' '),
data: {_mdxIsPragmaComment: true}
})
}

if (jsxRuntime === 'classic' && pragmaImportSource) {
Expand Down
11 changes: 11 additions & 0 deletions packages/mdx/lib/plugin/recma-jsx-build.js
Expand Up @@ -43,6 +43,17 @@ export function recmaJsxBuild(options) {
return function (tree, file) {
buildJsx(tree, {development, filePath: file.history[0]})

// Remove the pragma comment that we injected ourselves as it is no longer
// needed.
if (
tree.comments &&
tree.comments[0].type === 'Block' &&
tree.comments[0].data &&
tree.comments[0].data._mdxIsPragmaComment
) {
tree.comments.shift()
}

// When compiling to a function body, replace the import that was just
// generated, and get `jsx`, `jsxs`, and `Fragment` from `arguments[0]`
// instead.
Expand Down
19 changes: 19 additions & 0 deletions packages/mdx/lib/types.d.ts
Expand Up @@ -10,6 +10,16 @@ interface EsastData extends UnistData {
_mdxExplicitJsx?: boolean | null | undefined
}

interface EsastCommentData extends EsastData {
/**
* Whether a node (only used on comments) was generated by us to include the
* JSX pragmas, so that when we compile JSX away, we can remove it.
*
* Registered by `@mdx-js/mdx/lib/types.d.ts`.
*/
_mdxIsPragmaComment?: boolean | null | undefined
}

// Register data on `estree`.
declare module 'estree' {
interface BaseNode {
Expand All @@ -20,6 +30,15 @@ declare module 'estree' {
*/
data?: EsastData | undefined
}

interface Comment {
/**
* Extra unist data passed added by `recma-document`.
*
* Registered by `@mdx-js/mdx/lib/types.d.ts`.
*/
data?: EsastCommentData | undefined
}
}

// Register data on `mdast`.
Expand Down
12 changes: 2 additions & 10 deletions packages/mdx/readme.md
Expand Up @@ -104,7 +104,6 @@ console.log(String(compiled))
Yields roughly:

```tsx
/* @jsxRuntime automatic @jsxImportSource react */
import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from 'react/jsx-runtime'

export function Thing() {
Expand Down Expand Up @@ -653,8 +652,8 @@ Configuration for `createProcessor` (TypeScript type).
…yields this difference:

```diff
/* @jsxRuntime automatic @jsxImportSource react */
-import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from 'react/jsx-runtime'
+/* @jsxRuntime automatic @jsxImportSource react */

export function Thing() {
- return _jsx(_Fragment, {children: 'World'})
Expand Down Expand Up @@ -701,9 +700,7 @@ Configuration for `createProcessor` (TypeScript type).
…yields this difference:

```diff
-/* @jsxRuntime automatic @jsxImportSource react */
-import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from 'react/jsx-runtime'
+/* @jsxRuntime automatic @jsxImportSource preact */
+import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from 'preact/jsx-runtime'
```

Expand All @@ -729,9 +726,7 @@ Configuration for `createProcessor` (TypeScript type).
…yields this difference:

```diff
-/* @jsxRuntime automatic @jsxImportSource react */
-import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from 'react/jsx-runtime'
+/* @jsxRuntime classic @jsx React.createElement @jsxFrag React.Fragment */
+import React from 'react'

export function Thing() {
Expand Down Expand Up @@ -767,14 +762,14 @@ Configuration for `createProcessor` (TypeScript type).
…yields:

```tsx
/* @jsxRuntime automatic @jsxImportSource react */
import {jsx as _jsx, jsxs as _jsxs} from 'react/jsx-runtime'
export const no = 3.14
function _createMdxContent(props) { /**/ }
export default function MDXContent(props = {}) { /**/ }
```

```tsx
'use strict'
const {Fragment: _Fragment, jsx: _jsx} = arguments[0]
const no = 3.14
function _createMdxContent(props) { /**/ }
Expand Down Expand Up @@ -826,9 +821,7 @@ Configuration for `createProcessor` (TypeScript type).
…yields this difference:

```diff
-/* @jsxRuntime classic @jsx React.createElement @jsxFrag React.Fragment */
-import React from 'react'
+/* @jsxRuntime classic @jsx preact.createElement @jsxFrag preact.Fragment */
+import preact from 'preact/compat'

export function Thing() {
Expand Down Expand Up @@ -879,7 +872,6 @@ Configuration for `createProcessor` (TypeScript type).
…yields this difference:

```diff
/* @jsxRuntime automatic @jsxImportSource react */
import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from 'react/jsx-runtime'
+import {useMDXComponents as _provideComponents} from '@mdx-js/react'

Expand Down
3 changes: 0 additions & 3 deletions packages/mdx/test/syntax.js
Expand Up @@ -641,7 +641,6 @@ test('@mdx-js/mdx: syntax: MDX (JSX)', async function (t) {
</table>`)
),
[
'/*@jsxRuntime automatic @jsxImportSource react*/',
'import {jsx as _jsx, jsxs as _jsxs} from "react/jsx-runtime";',
'function _createMdxContent(props) {',
' return _jsx("table", {',
Expand Down Expand Up @@ -868,7 +867,6 @@ test('@mdx-js/mdx: syntax: MDX (ESM)', async function (t) {
)
),
[
'/*@jsxRuntime automatic @jsxImportSource react*/',
'import {jsxDEV as _jsxDEV} from "react/jsx-dev-runtime";',
'function _createMdxContent(props) {',
' const {X} = props.components || ({});',
Expand Down Expand Up @@ -906,7 +904,6 @@ test('@mdx-js/mdx: syntax: MDX (ESM)', async function (t) {
assert.equal(
String(await compile('# hi', {outputFormat: 'function-body'})),
[
'/*@jsxRuntime automatic @jsxImportSource react*/',
'"use strict";',
'const {jsx: _jsx} = arguments[0];',
'function _createMdxContent(props) {',
Expand Down
2 changes: 1 addition & 1 deletion packages/rollup/test/index.js
Expand Up @@ -41,7 +41,7 @@ test('@mdx-js/rollup', async function (t) {
// Source map.
assert.equal(
chunk.map?.mappings,
';;;AAAO,SAAA,OAAA,GAAA;;AAA8B,IAAA,QAAA,EAAA,QAAA;;;;;;;;;AAEnC,IAAA,QAAA,EAAA,CAAA,SAAA,EAAAA,GAAA,CAAA,OAAA,EAAA,EAAA,CAAA,CAAA;;;;;;;;;;;;;;;'
';;AAAO,SAAA,OAAA,GAAA;;AAA8B,IAAA,QAAA,EAAA,QAAA;;;;;;;;;AAEnC,IAAA,QAAA,EAAA,CAAA,SAAA,EAAAA,GAAA,CAAA,OAAA,EAAA,EAAA,CAAA,CAAA;;;;;;;;;;;;;;;'
)

await fs.writeFile(jsUrl, chunk.code)
Expand Down

0 comments on commit f48d038

Please sign in to comment.