Parcel transformer for MDX files that compiles MDX to React components
npx @tessl/cli install tessl/npm-parcel--transformer-mdx@2.13.0Parcel transformer plugin for MDX files that compiles MDX to React components. This transformer enables seamless processing of MDX (Markdown with JSX) files in Parcel applications by compiling them to JavaScript modules that can be imported and used as React components.
Note: This is a Parcel plugin, not a standalone library. It integrates with Parcel's build system to transform .mdx files during bundling.
npm install @parcel/transformer-mdxThis is a Parcel transformer plugin that is automatically registered when installed. It requires no direct imports in application code.
The transformer is included in Parcel's default configuration for .mdx files. For custom configurations, add to .parcelrc:
{
"extends": "@parcel/config-default",
"transformers": {
"*.mdx": ["@parcel/transformer-mdx"]
}
}Once the transformer is installed and configured, applications can import MDX files as React components. The transformer automatically processes .mdx files during the Parcel build process.
Application usage after transformation:
// Import an MDX file as a React component
import MyDocument from './content/my-document.mdx';
// Use it in your React application
function App() {
return (
<div>
<h1>My App</h1>
<MyDocument />
</div>
);
}Example MDX file (my-document.mdx):
# Hello from MDX
This is a paragraph with **bold text**.
import Button from './Button';
<Button>Click me!</Button>
## Code Example
```javascript
console.log('Hello world');## Architecture
The transformer integrates with Parcel's plugin system:
- **Plugin Type**: Transformer plugin using `new Transformer()` constructor
- **Framework**: Built on `@parcel/plugin` framework
- **Processing**: Uses `@mdx-js/mdx` to compile MDX to JavaScript with React imports
- **Output**: JavaScript modules with classic JSX runtime configuration
## Capabilities
### MDX Transformation
Transforms MDX files into JavaScript modules containing React components.
```javascript { .api }
/**
* Parcel Transformer instance for MDX files
* Automatically processes .mdx files when registered with Parcel
*/
export default Transformer;
/**
* Transformer implementation that processes MDX files
* Takes an asset parameter with destructured syntax
*/
interface TransformerImplementation {
transform({asset}: {asset: Asset}): Promise<Array<Asset>>;
}
/**
* Asset object representing the file being processed by Parcel
*/
interface Asset {
/** Get the source code content of the asset */
getCode(): Promise<string>;
/** Set the transformed code content */
setCode(code: string): void;
/** The asset type - changed from 'mdx' to 'js' after transformation */
type: string;
}Transformation Process:
.mdx files containing Markdown with JSX@mdx-js/mdx to compile MDX to JavaScript/* @jsxRuntime classic */)/* @jsx mdx */)import React from 'react')import { mdx } from '@mdx-js/react')Generated Output Format:
/* @jsxRuntime classic */
/* @jsx mdx */
import React from 'react';
import { mdx } from '@mdx-js/react'
// [compiled MDX content as JSX]/**
* Core dependencies required by the transformer
*/
interface Dependencies {
/** MDX compiler - converts MDX to JavaScript */
"@mdx-js/mdx": "^1.6.22";
/** Parcel plugin base class */
"@parcel/plugin": "2.13.3";
}
/**
* Peer dependencies required at runtime
*/
interface PeerDependencies {
/** MDX React runtime for component rendering */
"@mdx-js/react": "^1.6.22";
}The transformer uses default MDX compilation settings with no custom configuration options. It automatically:
.mdx files@mdx-js/mdx v1.6.22@mdx-js/react for runtime supportAutomatically handles files with the .mdx extension when registered with Parcel.
The transformer may throw errors during compilation if:
Errors are handled by Parcel's error reporting system and will prevent the build from completing successfully.
The transformer is included in Parcel's default configuration at line 36 of @parcel/config-default. It automatically processes .mdx files without additional configuration.
For custom configurations, add to .parcelrc:
{
"extends": "@parcel/config-default",
"transformers": {
"*.mdx": ["@parcel/transformer-mdx"]
}
}npm install @parcel/transformer-mdxnpm install @mdx-js/react.mdx files in your project