mdx-bundler 🦤
Compile and bundle your MDX files and their dependencies. FAST.

The problem
You have a string of MDX and various TS/JS files that it uses and you want to
get a bundled version of these files to eval in the browser.
This solution
This is an async function that will compile and bundle your MDX files and their
dependencies. It uses esbuild, so it's VERY fast
and supports TypeScript files (for the dependencies of your MDX files). It also
uses xdm which is a more modern and powerful
MDX compiler with fewer bugs and more features (and no extra runtime
requirements).
Your source files could be local, in a remote github repo, in a CMS, or wherever
else and it doesn't matter. All mdx-bundler
cares about is that you pass it
all the files and source code necessary and it will take care of bundling
everything for you.
Table of Contents
Installation
This module is distributed via npm which is bundled with node and
should be installed as one of your project's dependencies
:
npm install --save mdx-bundler
Usage
import {bundleMDX} from 'mdx-bundler'
const mdxSource = `
---
title: Example Post
published: 2021-02-13
description: This is some description
---
# Wahoo
import Demo from './demo'
Here's a **neat** demo:
<Demo />
`.trim()
const result = await bundleMDX(mdxSource, {
files: {
'./demo.tsx': `
import * as React from 'react'
function Demo() {
return <div>Neat demo!</div>
}
export default Demo
`,
},
})
const {code, frontmatter} = result
From there, you send the code
to your client, and then:
import * as React from 'react'
import {MDXProvider} from '@mdx-js/react'
import {getMDXComponent} from 'mdx-bundler/client'
function Post({code, frontmatter}) {
const Component = React.useMemo(() => getMDXComponent(code), [code])
return (
<MDXProvider>
<header>
<h1>{frontmatter.title}</h1>
<p>{frontmatter.description}</p>
</header>
<main>
<Component />
</main>
</MDXProvider>
)
}
Ultimately, this gets rendered (basically):
<header>
<h1>This is the title</h1>
<p>This is some description</p>
</header>
<main>
<div>
<h1>Wahoo</h1>
<p>Here's a <strong>neat</strong> demo:</p>
<div>Neat demo!</div>
</div>
</main>
Options
files
The files
config is an object of all the files you're bundling. The key is the
path to the file (relative to the MDX source) and the value is the string of the
file source code. You could get these from the filesystem or from a remote
database. If your MDX doesn't reference other files (or only imports things from
node_modules
), then you can omit this entirely.
xdmOptions
This allows you to modify the built-in xdm configuration (passed to
xdm.compile). This can be helpful for specifying your own
remarkPlugins/rehypePlugins.
bundleMDX(mdxString, {
xdmOptions(input, options) {
options.remarkPlugins = [...(options.remarkPlugins ?? []), myRemarkPlugin]
options.rehypePlugins = [...(options.rehypePlugins ?? []), myRehypePlugin]
return options
},
})
esbuildOptions
You can customize any of esbuild options with the option esbuildOptions
. This
takes a function which is passed the default esbuild options and expects an
options object to be returned.
bundleMDX(mdxSource, {
esbuildOptions(options) {
options.minify = false
options.target = [
'es2020',
'chrome58',
'firefox57',
'safari11',
'edge16',
'node12',
]
return options
},
})
More information on the available options can be found in the
esbuild documentation.
It's recommended to use this feature to configure the target
to your desired
output, otherwise, esbuild defaults to esnext
which is to say that it doesn't
compile any standardized features so it's possible users of older browsers will
experience errors.
globals
This tells esbuild that a given module is externally available. For example, if
your MDX file uses the d3 library and you're already using the d3 library in
your app then you'll end up shipping d3
to the user twice (once for your app
and once for this MDX component). This is wasteful and you'd be better off just
telling esbuild to not bundle d3
and you can pass it to the component
yourself when you call getMDXComponent
.
Here's an example:
import {bundleMDX} from 'mdx-bundler'
const mdxSource = `
# This is the title
import leftPad from 'left-pad'
<div>{leftPad("Neat demo!", 12, '!')}</div>
`.trim()
const result = await bundleMDX(mdxSource, {
globals: {'left-pad': 'myLeftPad'},
})
import * as React from 'react'
import {MDXProvider} from '@mdx-js/react'
import leftPad from 'left-pad'
import {getMDXComponent} from 'mdx-bundler/client'
function MDXPage({code}: {code: string}) {
const Component = React.useMemo(
() => getMDXComponent(result.code, {myLeftPad: leftPad}),
[result.code, leftPad],
)
return (
<MDXProvider>
<main>
<Component />
</main>
</MDXProvider>
)
}
Inspiration
As I was rewriting kentcdodds.com to
remix, I decided I wanted to keep my blog posts as MDX, but
I didn't want to have to compile them all at build time or be required to
redeploy every time I fix a typo. So I made this which allows my server to
compile on demand.
Other Solutions
There's next-mdx-remote but it's
more of an mdx-compiler than a bundler (can't bundle your mdx for dependencies).
Also it's focused on Next.js whereas this is meta-framework agnostic.
Issues
Looking to contribute? Look for the Good First Issue
label.
🐛 Bugs
Please file an issue for bugs, missing documentation, or unexpected behavior.
See Bugs
💡 Feature Requests
Please file an issue to suggest new features. Vote on feature requests by adding
a 👍. This helps maintainers prioritize what to work on.
See Feature Requests
Contributors ✨
Thanks goes to these people (emoji key):
This project follows the all-contributors specification.
Contributions of any kind welcome!
LICENSE
MIT