Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new transformer for MDX v2 #8625

Open
wants to merge 6 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/core/integration-tests/test/mdx-2-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "@parcel/config-default",
"optimizers": {
"*.mdx": ["@parcel/transformer-mdx-2"]
}
}
30 changes: 30 additions & 0 deletions packages/core/integration-tests/test/mdx-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// @flow

const assert = require('assert');
const path = require('path');
const {bundle, run} = require('@parcel/test-utils');

describe('mdx', function () {
it('should support bundling MDX', async function () {
let b = await bundle(path.join(__dirname, '/integration/mdx/index.mdx'), {
config: require.resolve('./mdx-2-config.json'),
});

let output = await run(b);
assert.equal(typeof output.default, 'function');
assert(output.default.isMDXComponent);
});

it('should support bundling MDX with React 17', async function () {
let b = await bundle(
path.join(__dirname, '/integration/mdx-react-17/index.mdx'),
{
config: require.resolve('./mdx-2-config.json'),
},
);

let output = await run(b);
assert.equal(typeof output.default, 'function');
assert(output.default.isMDXComponent);
});
});
29 changes: 29 additions & 0 deletions packages/transformers/mdx-2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@parcel/transformer-mdx-2",
"version": "2.8.0",
"license": "MIT",
"publishConfig": {
"access": "public"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/parcel"
},
"repository": {
"type": "git",
"url": "https://github.com/parcel-bundler/parcel.git"
},
"main": "lib/MDXTransformer.js",
"source": "src/MDXTransformer.js",
"engines": {
"node": ">= 12.0.0",
"parcel": "2.0.0 - 2.7.0"
},
"dependencies": {
"@mdx-js/mdx": "^2.1.5",
"@parcel/plugin": "2.8.0"
},
"peerDependencies": {
"react": ">=17.0.0"
}
}
41 changes: 41 additions & 0 deletions packages/transformers/mdx-2/src/MDXTransformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const {Transformer} = require('@parcel/plugin');

module.exports = new Transformer({
async loadConfig({config}) {
// config.getConfig only works with CJS
let configFile = await config.getConfig(['.mdxrc.js', '.mdxrc.cjs'], {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To follow https://github.com/mdx-js/mdx/blob/v1/packages/parcel-plugin-mdx/src/MDXAsset.js#L13, I could add mdx.config.js and mdx.config.cjs.

Or just use those 2 instead of .mdxrc.js (as usually the rc files are JSON files, not JS files)

packageKey: 'mdx',
});
if (!configFile) {
return () => {};
}

config.invalidateOnFileChange(configFile.filePath);
// .mdxrc can either exports an object (MDX config), or a function that returns the config
if (typeof configFile.contents === 'function') {
return configFile.contents;
}
return () => configFile.contents;
},

async transform({asset, config}) {
let code = await asset.getCode();

const {compile} = await import('@mdx-js/mdx');

// @mdx-js/mdx is a ESM package, so we need to use `import()` to make it work in CJS envs
let compiled = await compile(code, {
...(await config()),

// Those don't have to be configurable
useDynamicImport: false,
format: 'mdx',
outputFormat: 'program',
});

asset.setCode(compiled.toString('utf-8'));
asset.type = 'js';

return [asset];
},
});
Loading