Skip to content

Commit 2e85275

Browse files
authored
feat(plugin-mdx): add extensions option (#1560)
1 parent e5d5df3 commit 2e85275

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

packages/document/docs/en/plugins/list/plugin-mdx.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,18 @@ pluginMdx({
5050
},
5151
});
5252
```
53+
54+
### extensions
55+
56+
Specify the file extensions to be compiled with MDX loader, including .md files and .mdx files by default.
57+
58+
- **Type:** `string[]`
59+
- **Default:** `['.mdx', '.md']`
60+
61+
For example, to only compile .mdx files, you can set it as:
62+
63+
```ts
64+
pluginMdx({
65+
extensions: ['.mdx'],
66+
});
67+
```

packages/document/docs/zh/plugins/list/plugin-mdx.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,18 @@ pluginMdx({
5050
},
5151
});
5252
```
53+
54+
### extensions
55+
56+
指定使用 MDX loader 编译的文件后缀,默认包括 .md 文件和 .mdx 文件。
57+
58+
- **类型:** `string[]`
59+
- **默认值:** `['.mdx', '.md']`
60+
61+
比如仅编译 .mdx 文件,可以设置为:
62+
63+
```ts
64+
pluginMdx({
65+
extensions: ['.mdx'],
66+
});
67+
```

packages/plugin-mdx/src/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,30 @@ export type PluginMdxOptions = {
77
* @see https://npmjs.com/package/@mdx-js/loader#api
88
*/
99
mdxLoaderOptions?: MdxLoaderOptions;
10+
/**
11+
* @default ['.mdx', '.md']
12+
*/
13+
extensions?: string[];
1014
};
1115

16+
function createRegExp(exts: string[]): RegExp {
17+
const matcher = exts.map((ext) => ext.slice(1)).join('|');
18+
return new RegExp(
19+
exts.length === 1 ? `\\.${matcher}$` : `\\.(?:${matcher})$`,
20+
'i',
21+
);
22+
}
23+
1224
export const pluginMdx = (options: PluginMdxOptions = {}): RsbuildPlugin => ({
1325
name: 'rsbuild:mdx',
1426

1527
setup(api) {
1628
api.modifyBundlerChain((chain, { CHAIN_ID }) => {
17-
chain.resolve.extensions.add('.mdx');
29+
const { extensions = ['.mdx', '.md'] } = options;
30+
31+
extensions.forEach((ext) => {
32+
chain.resolve.extensions.add(ext);
33+
});
1834

1935
const jsRule = chain.module.rules.get(CHAIN_ID.RULE.JS);
2036
const mdxRule = chain.module.rule('mdx');
@@ -30,7 +46,7 @@ export const pluginMdx = (options: PluginMdxOptions = {}): RsbuildPlugin => ({
3046
return false;
3147
});
3248

33-
const MDX_REGEXP = /\.mdx?$/;
49+
const MDX_REGEXP = createRegExp(extensions);
3450

3551
mdxRule
3652
.test(MDX_REGEXP)

packages/plugin-mdx/tests/__snapshots__/index.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`plugin-mdx > should allow to configure mdx loader 1`] = `
44
{
5-
"test": /\\\\\\.mdx\\?\\$/,
5+
"test": /\\\\\\.\\(\\?:mdx\\|md\\)\\$/i,
66
"use": [
77
{
88
"loader": "builtin:swc-loader",
@@ -47,7 +47,7 @@ exports[`plugin-mdx > should allow to configure mdx loader 1`] = `
4747

4848
exports[`plugin-mdx > should register mdx loader correctly 1`] = `
4949
{
50-
"test": /\\\\\\.mdx\\?\\$/,
50+
"test": /\\\\\\.\\(\\?:mdx\\|md\\)\\$/i,
5151
"use": [
5252
{
5353
"loader": "builtin:swc-loader",

0 commit comments

Comments
 (0)