@mdx-js/loader vs @mdx-js/mdx vs @mdx-js/react
MDX (Markdown for JSX) Libraries
@mdx-js/loader@mdx-js/mdx@mdx-js/react

MDX (Markdown for JSX) Libraries

MDX (Markdown for JSX) is a powerful format that allows you to write JSX (React components) directly within Markdown files. This enables a seamless blend of static content and dynamic components, making it ideal for documentation, blogs, and interactive content. The MDX ecosystem provides tools to parse, compile, and render these mixed-content files, offering developers flexibility and creativity in how they present information. The three main packages in the MDX ecosystem are @mdx-js/loader, @mdx-js/mdx, and @mdx-js/react, each serving a unique purpose in the workflow of handling MDX files.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
@mdx-js/loader019,40415.1 kB267 months agoMIT
@mdx-js/mdx019,404164 kB267 months agoMIT
@mdx-js/react019,40414.4 kB267 months agoMIT

Feature Comparison: @mdx-js/loader vs @mdx-js/mdx vs @mdx-js/react

Purpose

  • @mdx-js/loader:

    The @mdx-js/loader package is designed to integrate MDX processing into Webpack workflows. It transforms MDX files into React components during the build process, allowing developers to import and use MDX content directly in their applications.

  • @mdx-js/mdx:

    The @mdx-js/mdx package serves as a command-line tool for compiling MDX files. It provides utilities for converting MDX content into various formats, making it suitable for static site generation and other use cases where MDX needs to be processed outside of a React environment.

  • @mdx-js/react:

    The @mdx-js/react package focuses on rendering MDX content within React applications. It provides the necessary components and context to handle both Markdown and JSX elements, enabling seamless integration of interactive content within React components.

Integration

  • @mdx-js/loader:

    @mdx-js/loader integrates with Webpack, allowing for seamless processing of MDX files as part of the build pipeline. This integration is crucial for projects that rely on Webpack for asset management and bundling.

  • @mdx-js/mdx:

    @mdx-js/mdx operates as a standalone tool, independent of any specific build system. This makes it versatile for use in various environments, including static site generators and command-line workflows.

  • @mdx-js/react:

    @mdx-js/react is designed for integration within React applications. It works alongside other React components and libraries, making it easy to incorporate MDX content into existing React projects.

Rendering

  • @mdx-js/loader:

    @mdx-js/loader handles the rendering of MDX content indirectly by transforming it into React components. The actual rendering is performed by the React application using the components generated by the loader.

  • @mdx-js/mdx:

    @mdx-js/mdx does not handle rendering directly. Instead, it compiles MDX content into a format that can be rendered by other tools or frameworks, such as React, Vue, or static HTML.

  • @mdx-js/react:

    @mdx-js/react provides the rendering capabilities for MDX content within React applications. It handles both Markdown and JSX elements, allowing for dynamic and interactive rendering of MDX files.

Example Code

  • @mdx-js/loader:

    Example of using @mdx-js/loader in a Webpack configuration:

    module.exports = {
      module: {
        rules: [
          {
            test: /\.mdx$/,
            use: '@mdx-js/loader',
          },
        ],
      },
    };
    
  • @mdx-js/mdx:

    Example of using @mdx-js/mdx from the command line:

    npx @mdx-js/mdx compile input.mdx -o output.html
    
  • @mdx-js/react:

    Example of rendering MDX content with @mdx-js/react:

    import { MDXProvider } from '@mdx-js/react';
    import MyMdxContent from './content.mdx';
    
    const App = () => (
      <MDXProvider>
        <MyMdxContent />
      </MDXProvider>
    );
    

How to Choose: @mdx-js/loader vs @mdx-js/mdx vs @mdx-js/react

  • @mdx-js/loader:

    Choose @mdx-js/loader if you are working on a project that requires Webpack integration for MDX files. This package is essential for transforming MDX content into React components during the build process, making it ideal for static site generators and applications that leverage Webpack.

  • @mdx-js/mdx:

    Select @mdx-js/mdx if you need a command-line tool for compiling MDX files into HTML or other formats. This package is useful for projects that require a standalone solution for processing MDX content without integrating it directly into a React application.

  • @mdx-js/react:

    Opt for @mdx-js/react if you are building a React application and want to render MDX content seamlessly. This package provides the necessary components and context to integrate MDX files into your React app, allowing for dynamic rendering of both Markdown and JSX.

README for @mdx-js/loader

@mdx-js/loader

Build Coverage Downloads Sponsors Backers Chat

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 or Rspack).

This integration can be combined with the Babel loader to 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. In Node.js (version 16+), install with npm:

npm install @mdx-js/loader

Use

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

/**
 * @import {Options} from '@mdx-js/loader'
 * @import {Configuration} from 'webpack'
 */

/** @type {Configuration} */
const webpackConfig = {
  module: {
    // …
    rules: [
      // …
      {
        test: /\.mdx?$/,
        use: [
          {
            loader: '@mdx-js/loader',
            /** @type {Options} */
            options: {/* jsxImportSource: …, otherOptions… */}
          }
        ]
      }
    ]
  }
}

export default webpackConfig

See also ¶ Next.js and ¶ Vue CLI, if you’re using webpack through them, for more info.

API

This package exports no identifiers. The default export is mdx.

mdx

This package exports a webpack plugin as the default export. Configuration (see Options) are passed separately through webpack.

Options

Configuration (TypeScript type).

Options are the same as CompileOptions from @mdx-js/mdx with the exception that the SourceMapGenerator and development options are supported based on how you configure webpack. You cannot pass them manually.

Examples

Combine with Babel

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

/**
 * @import {Options} from '@mdx-js/loader'
 * @import {Configuration} from 'webpack'
 */

/** @type {Configuration} */
const webpackConfig = {
  module: {
    // …
    rules: [
      // …
      {
        test: /\.mdx?$/,
        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 {Options} */
            options: {}
          }
        ]
      }
    ]
  }
}

export default webpackConfig

Types

This package is fully typed with TypeScript. It exports the additional type Options. See § Types on our website for information.

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, @mdx-js/loader@^3, compatible with Node.js 16.

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