Skip to navigation
2-3 minutes read

@mdx-js/loader

webpack loader for MDX.

Contents

What is this?

This package is a webpack loader to support MDX.

When should I use this?

This integration is useful if you’re using webpack (or another tool that uses webpack, such as Next.js).

This integration can be combined with the Babel loader to support nonstandard JSX runtimes (such as Vue) or compile modern JavaScript features to ones your users support.

If you want to evaluate MDX code then the lower-level compiler (@mdx-js/mdx) can be used manually.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

Shell
npm install @mdx-js/loader@next

yarn:

Shell
yarn add @mdx-js/loader@next

Use

Add something along these lines to your webpack.config.js:

JavaScript
module.exports = {
  module: {
    // …
    rules: [
      // …
      {
        test: /\.mdx?$/,
        use: [
          {
            loader: '@mdx-js/loader',
            /** @type {import('@mdx-js/loader').Options} */
            options: {}
          }
        ]
      }
    ]
  }
}

See also ¶ Create React App (CRA), ¶ Next.js, and ¶ Vue CLI, if you’re using webpack through them, for more info.

API

This package exports a webpack plugin as the default export.

Source maps are supported based on how you configure webpack. You do not need to pass options.SourceMapGenerator.

options

options are the same as compile from @mdx-js/mdx.

Note: Babel

If you use modern JavaScript features you might want to use Babel through babel-loader to compile to code that works:

JavaScript
// …
use: [
  // Note that Webpack runs right-to-left: `@mdx-js/loader` is used first, then
  // `babel-loader`.
  {loader: 'babel-loader', options: {}},
  {
    loader: '@mdx-js/loader',
    /** @type {import('@mdx-js/loader').Options} */
    options: {},
  },
];
// …
Note: webpack-cli

webpack-cli doesn’t support loaders in ESM directly or even indirectly. Because @mdx-js/mdx itself is ESM, this means the @mdx-js/loader loader (even though it’s CJS) doesn’t work with webpack-cli (it does work when using the webpack API). To use this loader with webpack-cli, set the DISABLE_V8_COMPILE_CACHE=1 environment variable. See this issue for details.

Shell
DISABLE_V8_COMPILE_CACHE=1 webpack

Types

This package is fully typed with TypeScript. See § Types on our website for information.

An Options type is exported, which represents acceptable configuration.

Security

See § Security on our website for information.

Contribute

See § Contribute on our website for ways to get started. See § Support for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Compositor and Vercel