Skip to content

Commit

Permalink
[fix] Preset paths & document of Custom Configuration (fix #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery committed Aug 8, 2024
1 parent 1e69a82 commit 7af2d91
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
8 changes: 7 additions & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Unofficial [MDX][1] 3 [transformer plugin][2] for [Parcel][3] 2

[![NPM](https://nodei.co/npm/parcel-transformer-mdx.png?downloads=true&downloadRank=true&stars=true)][6]

## Supported features
## Preset features

1. [GitHub flavored Markdown (GFM)](https://github.com/remarkjs/remark-gfm)
2. [Frontmatter](https://github.com/remarkjs/remark-frontmatter)
Expand Down Expand Up @@ -95,6 +95,10 @@ https://www.youtube.com/watch?v=VEoMT8pAxMA

- WebCell example: https://github.com/EasyWebApp/BootCell-document

## Custom Configuration

Create a `mdx.config.js` file in the root folder of your project, and export your [`CompileOptions` object][7] as what [the internal preset][8] does.

## Inspiration

https://github.com/parcel-bundler/parcel/pull/7922
Expand All @@ -105,3 +109,5 @@ https://github.com/parcel-bundler/parcel/pull/7922
[4]: https://libraries.io/npm/parcel-transformer-mdx
[5]: https://github.com/EasyWebApp/Parcel-transformer-MDX/actions/workflows/main.yml
[6]: https://nodei.co/npm/parcel-transformer-mdx/
[7]: https://mdxjs.com/packages/mdx/#compileoptions
[8]: source/preset.js
18 changes: 7 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { Transformer } from '@parcel/plugin';
import { compile } from '@mdx-js/mdx';

import MDXPreset from './source/preset';
import { BabelConfigFiles, MDXConfigFiles } from './source/utility';
import MDXPreset from './source/preset.js';
import { BabelConfigFiles, MDXConfigFiles } from './source/utility.js';

export { MDXPreset };
export * from './source/utility';
export * from './source/utility.js';

export default new Transformer({
/**
* @returns {Promise<import('@mdx-js/mdx').CompileOptions>}
*/
async loadConfig({ config }) {
const [
{ contents: TSConfig },
{ contents: BabelConfig },
{ contents: MDXConfig }
] = await Promise.all([
const [TSConfig, BabelConfig, MDXConfig] = await Promise.all([
config.getConfig(['tsconfig.json', 'jsconfig.json']),
config.getConfig(BabelConfigFiles, { packageKey: 'babel' }),
config.getConfig(MDXConfigFiles, { packageKey: 'mdx' })
Expand All @@ -25,12 +21,12 @@ export default new Transformer({
* @type {import('types-tsconfig').TSConfigJSON['compilerOptions']}
*/
const { jsx, jsxImportSource, jsxFactory, jsxFragmentFactory } =
TSConfig?.compilerOptions || {};
TSConfig?.contents.compilerOptions || {};
/**
* @see {@link https://babeljs.io/docs/babel-preset-react#with-a-configuration-file-recommended}
*/
const [_, ReactPreset = {}] =
BabelConfig?.presets?.find(
BabelConfig?.contents.presets?.find(
preset =>
preset instanceof Array &&
preset[0] === '@babel/preset-react'
Expand All @@ -46,7 +42,7 @@ export default new Transformer({
pragma: pragma || jsxFactory,
pragmaFrag: pragmaFrag || jsxFragmentFactory,
...MDXPreset,
...MDXConfig
...MDXConfig?.contents
};
},
async transform({ asset, config }) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "parcel-transformer-mdx",
"version": "0.4.0",
"version": "0.4.2",
"license": "LGPL-3.0-or-later",
"author": "shiy2008@gmail.com",
"description": "Unofficial MDX 3 transformer plugin for Parcel 2",
Expand Down

0 comments on commit 7af2d91

Please sign in to comment.