Skip to content

Commit 8a4e6a6

Browse files
silvenonkentcdodds
andauthored
feat: replace xdm with MDX v2 (#152)
BREAKING CHANGE: We now use `@mdx-js/esbuild` instead of `xdm` and `xdmOptions` has been renamed to `mdxOptions` Co-authored-by: Kent C. Dodds <[email protected]>
1 parent 61c1a34 commit 8a4e6a6

File tree

5 files changed

+35
-49
lines changed

5 files changed

+35
-49
lines changed

README.md

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ get a bundled version of these files to eval in the browser.
2525
## This solution
2626

2727
This is an async function that will compile and bundle your MDX files and their
28-
dependencies. It uses [esbuild](https://esbuild.github.io/), so it's VERY fast
29-
and supports TypeScript files (for the dependencies of your MDX files). It also
30-
uses [xdm](https://github.com/wooorm/xdm) which is a more modern and powerful
31-
MDX compiler with fewer bugs and more features (and no extra runtime
32-
requirements).
28+
dependencies. It uses [MDX v2](https://mdxjs.com/blog/v2/) and
29+
[esbuild](https://esbuild.github.io/), so it's VERY fast and supports TypeScript
30+
files (for the dependencies of your MDX files).
3331

3432
Your source files could be local, in a remote github repo, in a CMS, or wherever
3533
else and it doesn't matter. All `mdx-bundler` cares about is that you pass it
@@ -154,17 +152,6 @@ Why not?
154152

155153
</details>
156154

157-
<details>
158-
<summary>
159-
<strong>
160-
"Why does this use XDM instead of @mdx-js?"
161-
</strong>
162-
</summary>
163-
164-
It has more features, fewer bugs, and no runtime!
165-
166-
</details>
167-
168155
<details>
169156
<summary>
170157
<strong>
@@ -321,18 +308,18 @@ file source code. You could get these from the filesystem or from a remote
321308
database. If your MDX doesn't reference other files (or only imports things from
322309
`node_modules`), then you can omit this entirely.
323310

324-
#### xdmOptions
311+
#### mdxOptions
325312

326-
This allows you to modify the built-in xdm configuration (passed to the xdm
327-
esbuild plugin). This can be helpful for specifying your own
313+
This allows you to modify the built-in MDX configuration (passed to
314+
`@mdx-js/esbuild`). This can be helpful for specifying your own
328315
remarkPlugins/rehypePlugins.
329316

330-
The function is passed the default xdmOptions and the frontmatter.
317+
The function is passed the default mdxOptions and the frontmatter.
331318

332319
```ts
333320
bundleMDX({
334321
source: mdxSource,
335-
xdmOptions(options, frontmatter) {
322+
mdxOptions(options, frontmatter) {
336323
// this is the recommended way to add custom remark/rehype plugins:
337324
// The syntax might look weird, but it protects you in case we add/remove
338325
// plugins in the future.
@@ -505,7 +492,7 @@ the directory. If one option is set the other must be aswell.
505492
_The Javascript bundle is not written to this directory and is still returned as
506493
a string from `bundleMDX`._
507494

508-
This feature is best used with tweaks to `xdmOptions` and `esbuildOptions`. In
495+
This feature is best used with tweaks to `mdxOptions` and `esbuildOptions`. In
509496
the example below and `.png` files are written to the disk and then served from
510497
`/file/`.
511498

@@ -521,7 +508,7 @@ const {code} = await bundleMDX({
521508
cwd: '/path/to/site/content',
522509
bundleDirectory: '/path/to/site/public/file,
523510
bundlePath: '/file/',
524-
xdmOptions: options => {
511+
mdxOptions: options => {
525512
options.remarkPlugins = [remarkMdxImages]
526513

527514
return options
@@ -553,7 +540,7 @@ const {code} = await bundleMDX({
553540
`bundleMDX` has a single type parameter which is the type of your frontmatter.
554541
It defaults to `{[key: string]: any}` and must be an object. This is then used
555542
to type the returned `frontmatter` and the frontmatter passed to
556-
`esbuildOptions` and `xdmOptions`.
543+
`esbuildOptions` and `mdxOptions`.
557544

558545
```ts
559546
const {frontmatter} = bundleMDX<{title: string}>({source})
@@ -564,7 +551,7 @@ frontmatter.title // has type string
564551
### Component Substitution
565552

566553
MDX Bundler passes on
567-
[XDM's ability to substitute components](https://github.com/wooorm/xdm#mdx-content)
554+
[MDX's ability to substitute components](https://mdxjs.com/docs/using-mdx/#components)
568555
through the `components` prop on the component returned by `getMDXComponent`.
569556

570557
Here's an example that removes _p_ tags from around images.
@@ -653,7 +640,7 @@ import {remarkMdxImages} from 'remark-mdx-images'
653640
const {code} = await bundleMDX({
654641
source: mdxSource,
655642
cwd: '/users/you/site/_content/pages',
656-
xdmOptions: options => {
643+
mdxOptions: options => {
657644
options.remarkPlugins = [...(options.remarkPlugins ?? []), remarkMdxImages]
658645

659646
return options
@@ -684,7 +671,7 @@ folder to be used in image sources.
684671
const {code} = await bundleMDX({
685672
source: mdxSource,
686673
cwd: '/users/you/site/_content/pages',
687-
xdmOptions: options => {
674+
mdxOptions: options => {
688675
options.remarkPlugins = [...(options.remarkPlugins ?? []), remarkMdxImages]
689676

690677
return options

package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
"mdx",
1010
"bundler",
1111
"mdx-bundler",
12-
"esbuild",
13-
"xdm"
12+
"esbuild"
1413
],
1514
"author": "Kent C. Dodds <[email protected]> (https://kentcdodds.com)",
1615
"license": "MIT",
@@ -43,11 +42,11 @@
4342
"@babel/runtime": "^7.16.3",
4443
"@esbuild-plugins/node-resolve": "^0.1.4",
4544
"@fal-works/esbuild-plugin-global-externals": "^2.1.2",
45+
"@mdx-js/esbuild": "^2.0.0",
4646
"gray-matter": "^4.0.3",
4747
"remark-frontmatter": "^4.0.1",
4848
"remark-mdx-frontmatter": "^1.1.1",
49-
"uuid": "^8.3.2",
50-
"xdm": "^3.3.0"
49+
"uuid": "^8.3.2"
5150
},
5251
"peerDependencies": {
5352
"esbuild": "0.11.x || 0.12.x || 0.13.x || 0.14.x"

src/__tests__/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ import Demo from './demo'
217217
import * as React from 'react'
218218
import {left} from './left'
219219
220-
const Demo: React.FC = () => {
220+
const Demo: React.FC = () => {
221221
return <p>{left("TypeScript")}</p>
222222
}
223223
@@ -273,7 +273,7 @@ import {Demo} from './demo'
273273
'./demo.ts': `
274274
import React from 'react'
275275
276-
export const Demo: React.FC = () => {
276+
export const Demo: React.FC = () => {
277277
return <p>Sample</p>
278278
}
279279
`.trim(),
@@ -313,7 +313,7 @@ import {Sample} from './sample-component'
313313
const {code} = await bundleMDX({
314314
source: mdxSource,
315315
cwd: path.join(process.cwd(), 'other'),
316-
xdmOptions: options => {
316+
mdxOptions: options => {
317317
options.remarkPlugins = [remarkMdxImages]
318318

319319
return options
@@ -354,7 +354,7 @@ test('should output assets', async () => {
354354
cwd: path.join(process.cwd(), 'other'),
355355
bundleDirectory: path.join(process.cwd(), 'output'),
356356
bundlePath: '/img/',
357-
xdmOptions: options => {
357+
mdxOptions: options => {
358358
options.remarkPlugins = [remarkMdxImages]
359359

360360
return options
@@ -379,7 +379,7 @@ test('should output assets', async () => {
379379
await bundleMDX({
380380
source: mdxSource,
381381
cwd: path.join(process.cwd(), 'other'),
382-
xdmOptions: options => {
382+
mdxOptions: options => {
383383
options.remarkPlugins = [remarkMdxImages]
384384

385385
return options

src/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function bundleMDX({
2020
file,
2121
source,
2222
files = {},
23-
xdmOptions = options => options,
23+
mdxOptions = options => options,
2424
esbuildOptions = options => options,
2525
globals = {},
2626
cwd = path.join(process.cwd(), `__mdx_bundler_fake_dir__`),
@@ -36,10 +36,10 @@ async function bundleMDX({
3636
}
3737
/* c8 ignore stop */
3838

39-
// xdm is a native ESM, and we're running in a CJS context. This is the
39+
// @mdx-js/esbuild is a native ESM, and we're running in a CJS context. This is the
4040
// only way to import ESM within CJS
41-
const [{default: xdmESBuild}, {default: remarkFrontmatter}] =
42-
await Promise.all([import('xdm/esbuild.js'), import('remark-frontmatter')])
41+
const [{default: mdxESBuild}, {default: remarkFrontmatter}] =
42+
await Promise.all([import('@mdx-js/esbuild'), import('remark-frontmatter')])
4343

4444
let /** @type string */ code,
4545
/** @type string */ entryPath,
@@ -184,8 +184,8 @@ async function bundleMDX({
184184
resolveOptions: {basedir: cwd},
185185
}),
186186
inMemoryPlugin,
187-
xdmESBuild(
188-
xdmOptions(
187+
mdxESBuild(
188+
mdxOptions(
189189
{
190190
remarkPlugins: [
191191
remarkFrontmatter,

src/types.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import type {Plugin, BuildOptions, Loader} from 'esbuild'
88
import type {ModuleInfo} from '@fal-works/esbuild-plugin-global-externals'
9-
import type {CoreProcessorOptions} from 'xdm/lib/compile'
9+
import type {ProcessorOptions} from '@mdx-js/esbuild/lib'
1010
import type {GrayMatterOption, Input, GrayMatterFile} from 'gray-matter'
1111

1212
type ESBuildOptions = BuildOptions
@@ -57,18 +57,18 @@ type BundleMDXOptions<Frontmatter> = {
5757
*/
5858
files?: Record<string, string>
5959
/**
60-
* This allows you to modify the built-in xdm configuration (passed to xdm.compile).
60+
* This allows you to modify the built-in MDX configuration (passed to @mdx-js/mdx compile).
6161
* This can be helpful for specifying your own remarkPlugins/rehypePlugins.
6262
*
6363
* @param vfileCompatible the path and contents of the mdx file being compiled
6464
* @param options the default options which you are expected to modify and return
65-
* @returns the options to be passed to xdm.compile
65+
* @returns the options to be passed to @mdx-js/mdx compile
6666
*
6767
* @example
6868
* ```
6969
* bundleMDX({
7070
* source: mdxString,
71-
* xdmOptions(options) {
71+
* mdxOptions(options) {
7272
* // this is the recommended way to add custom remark/rehype plugins:
7373
* // The syntax might look weird, but it protects you in case we add/remove
7474
* // plugins in the future.
@@ -80,10 +80,10 @@ type BundleMDXOptions<Frontmatter> = {
8080
* })
8181
* ```
8282
*/
83-
xdmOptions?: (
84-
options: CoreProcessorOptions,
83+
mdxOptions?: (
84+
options: ProcessorOptions,
8585
frontmatter: Frontmatter,
86-
) => CoreProcessorOptions
86+
) => ProcessorOptions
8787
/**
8888
* This allows you to modify the built-in esbuild configuration. This can be
8989
* especially helpful for specifying the compilation target.

0 commit comments

Comments
 (0)