Skip to content

Commit 77158cd

Browse files
committedOct 18, 2024··
Refactor to externalize recma packages
Closes GH-2030.
1 parent 6ccc003 commit 77158cd

8 files changed

+224
-193
lines changed
 

‎package-lock.json

+198-87
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/mdx/lib/core.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/**
22
* @import {Program} from 'estree-jsx'
3-
* @import {ElementAttributeNameCase, StylePropertyNameCase} from 'hast-util-to-estree'
43
* @import {Root} from 'mdast'
4+
* @import {Options as RehypeRecmaOptions} from 'rehype-recma'
55
* @import {Options as RemarkRehypeOptions} from 'remark-rehype'
66
* @import {SourceMapGenerator} from 'source-map'
77
* @import {PluggableList, Processor} from 'unified'
8-
* @import {Node} from 'unist'
98
*/
109

1110
/**
@@ -24,7 +23,7 @@
2423
* when using the webpack loader (`@mdx-js/loader`) or the Rollup integration
2524
* (`@mdx-js/rollup`) through Vite, this is automatically inferred from how
2625
* you configure those tools.
27-
* @property {ElementAttributeNameCase | null | undefined} [elementAttributeNameCase='react']
26+
* @property {RehypeRecmaOptions['elementAttributeNameCase']} [elementAttributeNameCase='react']
2827
* Casing to use for attribute names (default: `'react'`);
2928
* HTML casing is for example `class`, `stroke-linecap`, `xml:lang`;
3029
* React casing is for example `className`, `strokeLinecap`, `xmlLang`;
@@ -112,7 +111,7 @@
112111
* nodes (see `nodeTypes`) are passed through;
113112
* In particular, you might want to pass configuration for footnotes if your
114113
* content is not in English.
115-
* @property {StylePropertyNameCase | null | undefined} [stylePropertyNameCase='dom']
114+
* @property {RehypeRecmaOptions['stylePropertyNameCase']} [stylePropertyNameCase='dom']
116115
* Casing to use for property names in `style` objects (default: `'dom'`);
117116
* CSS casing is for example `background-color` and `-webkit-line-clamp`;
118117
* DOM casing is for example `backgroundColor` and `WebkitLineClamp`;
@@ -125,15 +124,17 @@
125124
*/
126125

127126
import {unreachable} from 'devlop'
127+
import recmaBuildJsx from 'recma-build-jsx'
128+
import recmaJsx from 'recma-jsx'
129+
import recmaStringify from 'recma-stringify'
130+
import rehypeRecma from 'rehype-recma'
128131
import remarkMdx from 'remark-mdx'
129132
import remarkParse from 'remark-parse'
130133
import remarkRehype from 'remark-rehype'
131134
import {unified} from 'unified'
135+
import {recmaBuildJsxTransform} from './plugin/recma-build-jsx-transform.js'
132136
import {recmaDocument} from './plugin/recma-document.js'
133-
import {recmaJsxBuild} from './plugin/recma-jsx-build.js'
134137
import {recmaJsxRewrite} from './plugin/recma-jsx-rewrite.js'
135-
import {recmaStringify} from './plugin/recma-stringify.js'
136-
import {rehypeRecma} from './plugin/rehype-recma.js'
137138
import {rehypeRemoveRaw} from './plugin/rehype-remove-raw.js'
138139
import {remarkMarkAndUnravel} from './plugin/remark-mark-and-unravel.js'
139140
import {nodeTypes} from './node-types.js'
@@ -225,12 +226,13 @@ export function createProcessor(options) {
225226
.use(recmaJsxRewrite, settings)
226227

227228
if (!settings.jsx) {
228-
pipeline.use(recmaJsxBuild, settings)
229+
pipeline.use(recmaBuildJsx, settings).use(recmaBuildJsxTransform, settings)
229230
}
230231

231-
// @ts-expect-error: `Program` is close enough to a `Node`,
232-
// but type inference has trouble with it and bridges.
233-
pipeline.use(recmaStringify, settings).use(settings.recmaPlugins || [])
232+
pipeline
233+
.use(recmaJsx)
234+
.use(recmaStringify, settings)
235+
.use(settings.recmaPlugins || [])
234236

235237
// @ts-expect-error: TS doesn’t get the plugins we added with if-statements.
236238
return pipeline

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

+6-17
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,37 @@
11
/**
22
* @import {Program} from 'estree-jsx'
3-
* @import {Options as BuildJsxOptions} from 'estree-util-build-jsx'
4-
* @import {VFile} from 'vfile'
53
*/
64

75
/**
8-
* @typedef ExtraOptions
9-
* Configuration for internal plugin `recma-jsx-build`.
6+
* @typedef Options
7+
* Configuration for internal plugin `recma-build-jsx-transform`.
108
* @property {'function-body' | 'program' | null | undefined} [outputFormat='program']
119
* Whether to keep the import of the automatic runtime or get it from
1210
* `arguments[0]` instead (default: `'program'`).
13-
*
14-
* @typedef {BuildJsxOptions & ExtraOptions} Options
15-
* Options.
1611
*/
1712

18-
import {buildJsx} from 'estree-util-build-jsx'
1913
import {specifiersToDeclarations} from '../util/estree-util-specifiers-to-declarations.js'
2014
import {toIdOrMemberExpression} from '../util/estree-util-to-id-or-member-expression.js'
2115

2216
/**
23-
* A plugin to build JSX into function calls.
24-
* `estree-util-build-jsx` does all the work for us!
17+
* Plugin to change the tree after compiling JSX away.
2518
*
2619
* @param {Readonly<Options> | null | undefined} [options]
2720
* Configuration (optional).
2821
* @returns
2922
* Transform.
3023
*/
31-
export function recmaJsxBuild(options) {
24+
export function recmaBuildJsxTransform(options) {
3225
/* c8 ignore next -- always given in `@mdx-js/mdx` */
33-
const {development, outputFormat} = options || {}
26+
const {outputFormat} = options || {}
3427

3528
/**
3629
* @param {Program} tree
3730
* Tree.
38-
* @param {VFile} file
39-
* File.
4031
* @returns {undefined}
4132
* Nothing.
4233
*/
43-
return function (tree, file) {
44-
buildJsx(tree, {development, filePath: file.history[0]})
45-
34+
return function (tree) {
4635
// Remove the pragma comment that we injected ourselves as it is no longer
4736
// needed.
4837
if (tree.comments) {

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

-44
This file was deleted.

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

-28
This file was deleted.

‎packages/mdx/package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@
4949
"@types/mdx": "^2.0.0",
5050
"collapse-white-space": "^2.0.0",
5151
"devlop": "^1.0.0",
52-
"estree-util-build-jsx": "^3.0.0",
5352
"estree-util-is-identifier-name": "^3.0.0",
5453
"estree-util-scope": "^1.0.0",
55-
"estree-util-to-js": "^2.0.0",
5654
"estree-walker": "^3.0.0",
57-
"hast-util-to-estree": "^3.0.0",
5855
"hast-util-to-jsx-runtime": "^2.0.0",
5956
"markdown-extensions": "^2.0.0",
57+
"recma-build-jsx": "^1.0.0",
58+
"recma-jsx": "^1.0.0",
59+
"recma-stringify": "^1.0.0",
60+
"rehype-recma": "^1.0.0",
6061
"remark-mdx": "^3.0.0",
6162
"remark-parse": "^11.0.0",
6263
"remark-rehype": "^11.0.0",

‎packages/mdx/readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ probably.
11341134
Then we go to JavaScript: [esast][] (JS; an
11351135
AST which is compatible with estree but looks a bit more like other unist ASTs).
11361136
This transformation is done by
1137-
[`syntax-tree/hast-util-to-estree`][hast-util-to-estree].
1137+
[`rehype-recma`][rehype-recma].
11381138
This is a new ecosystem that does not have utilities or plugins yet.
11391139
But it’s where `@mdx-js/mdx` does its thing: where it adds imports/exports,
11401140
where it compiles JSX away into `_jsx()` calls, and where it does the other cool
@@ -1239,7 +1239,7 @@ abide by its terms.
12391239

12401240
[hast]: https://github.com/syntax-tree/hast
12411241

1242-
[hast-util-to-estree]: https://github.com/syntax-tree/hast-util-to-estree
1242+
[rehype-recma]: https://github.com/mdx-js/recma/tree/main/packages/rehype-recma
12431243

12441244
[rehype-highlight]: https://github.com/rehypejs/rehype-highlight
12451245

‎packages/mdx/test/compile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ test('@mdx-js/mdx: compile', async function (t) {
10671067
const exception = /** @type {Error} */ (error)
10681068
const match = /at Component \(([^)]+)\)/.exec(String(exception.stack))
10691069
const actual = match?.[1].split(/\\|\//g).join('/') || ''
1070-
return (base.pathname + 'unknown.mdx:2:3').endsWith(actual)
1070+
return (base.pathname + 'unknown.js:2:3').endsWith(actual)
10711071
}
10721072
)
10731073

0 commit comments

Comments
 (0)
Please sign in to comment.