Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f48d038

Browse files
committed
Remove unneeded pragma comment after transform
1 parent 765310c commit f48d038

File tree

7 files changed

+38
-16
lines changed

7 files changed

+38
-16
lines changed

docs/docs/using-mdx.mdx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ Some observations:
6262
The *actual* output is:
6363

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

6867
export function Thing() {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ export function recmaDocument(options) {
103103
if (!tree.comments) tree.comments = []
104104

105105
if (pragmas.length > 0) {
106-
tree.comments.unshift({type: 'Block', value: pragmas.join(' ')})
106+
tree.comments.unshift({
107+
type: 'Block',
108+
value: pragmas.join(' '),
109+
data: {_mdxIsPragmaComment: true}
110+
})
107111
}
108112

109113
if (jsxRuntime === 'classic' && pragmaImportSource) {

packages/mdx/lib/plugin/recma-jsx-build.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ export function recmaJsxBuild(options) {
4343
return function (tree, file) {
4444
buildJsx(tree, {development, filePath: file.history[0]})
4545

46+
// Remove the pragma comment that we injected ourselves as it is no longer
47+
// needed.
48+
if (
49+
tree.comments &&
50+
tree.comments[0].type === 'Block' &&
51+
tree.comments[0].data &&
52+
tree.comments[0].data._mdxIsPragmaComment
53+
) {
54+
tree.comments.shift()
55+
}
56+
4657
// When compiling to a function body, replace the import that was just
4758
// generated, and get `jsx`, `jsxs`, and `Fragment` from `arguments[0]`
4859
// instead.

packages/mdx/lib/types.d.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ interface EsastData extends UnistData {
1010
_mdxExplicitJsx?: boolean | null | undefined
1111
}
1212

13+
interface EsastCommentData extends EsastData {
14+
/**
15+
* Whether a node (only used on comments) was generated by us to include the
16+
* JSX pragmas, so that when we compile JSX away, we can remove it.
17+
*
18+
* Registered by `@mdx-js/mdx/lib/types.d.ts`.
19+
*/
20+
_mdxIsPragmaComment?: boolean | null | undefined
21+
}
22+
1323
// Register data on `estree`.
1424
declare module 'estree' {
1525
interface BaseNode {
@@ -20,6 +30,15 @@ declare module 'estree' {
2030
*/
2131
data?: EsastData | undefined
2232
}
33+
34+
interface Comment {
35+
/**
36+
* Extra unist data passed added by `recma-document`.
37+
*
38+
* Registered by `@mdx-js/mdx/lib/types.d.ts`.
39+
*/
40+
data?: EsastCommentData | undefined
41+
}
2342
}
2443

2544
// Register data on `mdast`.

packages/mdx/readme.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ console.log(String(compiled))
104104
Yields roughly:
105105

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

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

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

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

703702
```diff
704-
-/* @jsxRuntime automatic @jsxImportSource react */
705703
-import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from 'react/jsx-runtime'
706-
+/* @jsxRuntime automatic @jsxImportSource preact */
707704
+import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from 'preact/jsx-runtime'
708705
```
709706

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

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

737732
export function Thing() {
@@ -767,14 +762,14 @@ Configuration for `createProcessor` (TypeScript type).
767762
…yields:
768763

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

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

828823
```diff
829-
-/* @jsxRuntime classic @jsx React.createElement @jsxFrag React.Fragment */
830824
-import React from 'react'
831-
+/* @jsxRuntime classic @jsx preact.createElement @jsxFrag preact.Fragment */
832825
+import preact from 'preact/compat'
833826

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

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

packages/mdx/test/syntax.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,6 @@ test('@mdx-js/mdx: syntax: MDX (JSX)', async function (t) {
641641
</table>`)
642642
),
643643
[
644-
'/*@jsxRuntime automatic @jsxImportSource react*/',
645644
'import {jsx as _jsx, jsxs as _jsxs} from "react/jsx-runtime";',
646645
'function _createMdxContent(props) {',
647646
' return _jsx("table", {',
@@ -868,7 +867,6 @@ test('@mdx-js/mdx: syntax: MDX (ESM)', async function (t) {
868867
)
869868
),
870869
[
871-
'/*@jsxRuntime automatic @jsxImportSource react*/',
872870
'import {jsxDEV as _jsxDEV} from "react/jsx-dev-runtime";',
873871
'function _createMdxContent(props) {',
874872
' const {X} = props.components || ({});',
@@ -906,7 +904,6 @@ test('@mdx-js/mdx: syntax: MDX (ESM)', async function (t) {
906904
assert.equal(
907905
String(await compile('# hi', {outputFormat: 'function-body'})),
908906
[
909-
'/*@jsxRuntime automatic @jsxImportSource react*/',
910907
'"use strict";',
911908
'const {jsx: _jsx} = arguments[0];',
912909
'function _createMdxContent(props) {',

packages/rollup/test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ test('@mdx-js/rollup', async function (t) {
4141
// Source map.
4242
assert.equal(
4343
chunk.map?.mappings,
44-
';;;AAAO,SAAA,OAAA,GAAA;;AAA8B,IAAA,QAAA,EAAA,QAAA;;;;;;;;;AAEnC,IAAA,QAAA,EAAA,CAAA,SAAA,EAAAA,GAAA,CAAA,OAAA,EAAA,EAAA,CAAA,CAAA;;;;;;;;;;;;;;;'
44+
';;AAAO,SAAA,OAAA,GAAA;;AAA8B,IAAA,QAAA,EAAA,QAAA;;;;;;;;;AAEnC,IAAA,QAAA,EAAA,CAAA,SAAA,EAAAA,GAAA,CAAA,OAAA,EAAA,EAAA,CAAA,CAAA;;;;;;;;;;;;;;;'
4545
)
4646

4747
await fs.writeFile(jsUrl, chunk.code)

0 commit comments

Comments
 (0)