-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Ayc0
wants to merge
6
commits into
parcel-bundler:v2
Choose a base branch
from
Ayc0:mdx-2
base: v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f9b2bfc
Add transformer for MDX 2
Ayc0 99c0e39
Add integration test for MDX 2
Ayc0 36464cd
Change defaults for MDX to use the v2
Ayc0 1428312
Revert "Change defaults for MDX to use the v2"
Ayc0 39802eb
[mdx-2][test] use mdx 2 transformer
Ayc0 4ec91ba
Patch mdx-2 loader
Ayc0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "@parcel/config-default", | ||
"optimizers": { | ||
"*.mdx": ["@parcel/transformer-mdx-2"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], { | ||
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]; | ||
}, | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
andmdx.config.cjs
.Or just use those 2 instead of
.mdxrc.js
(as usually the rc files are JSON files, not JS files)