File tree Expand file tree Collapse file tree 4 files changed +50
-4
lines changed Expand file tree Collapse file tree 4 files changed +50
-4
lines changed Original file line number Diff line number Diff line change @@ -50,3 +50,18 @@ pluginMdx({
50
50
},
51
51
});
52
52
```
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
+ ```
Original file line number Diff line number Diff line change @@ -50,3 +50,18 @@ pluginMdx({
50
50
},
51
51
});
52
52
```
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
+ ```
Original file line number Diff line number Diff line change @@ -7,14 +7,30 @@ export type PluginMdxOptions = {
7
7
* @see https://npmjs.com/package/@mdx-js/loader#api
8
8
*/
9
9
mdxLoaderOptions ?: MdxLoaderOptions ;
10
+ /**
11
+ * @default ['.mdx', '.md']
12
+ */
13
+ extensions ?: string [ ] ;
10
14
} ;
11
15
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
+
12
24
export const pluginMdx = ( options : PluginMdxOptions = { } ) : RsbuildPlugin => ( {
13
25
name : 'rsbuild:mdx' ,
14
26
15
27
setup ( api ) {
16
28
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
+ } ) ;
18
34
19
35
const jsRule = chain . module . rules . get ( CHAIN_ID . RULE . JS ) ;
20
36
const mdxRule = chain . module . rule ( 'mdx' ) ;
@@ -30,7 +46,7 @@ export const pluginMdx = (options: PluginMdxOptions = {}): RsbuildPlugin => ({
30
46
return false ;
31
47
} ) ;
32
48
33
- const MDX_REGEXP = / \. m d x ? $ / ;
49
+ const MDX_REGEXP = createRegExp ( extensions ) ;
34
50
35
51
mdxRule
36
52
. test ( MDX_REGEXP )
Original file line number Diff line number Diff line change 2
2
3
3
exports [` plugin-mdx > should allow to configure mdx loader 1` ] = `
4
4
{
5
- " test" : / \\\\\\ . mdx \\ ? \\ $ / ,
5
+ " test" : / \\\\\\ . \\ ( \\ ? :mdx \\ | md \\ ) \\ $ / i ,
6
6
" use" : [
7
7
{
8
8
" loader" : " builtin:swc-loader" ,
@@ -47,7 +47,7 @@ exports[`plugin-mdx > should allow to configure mdx loader 1`] = `
47
47
48
48
exports [` plugin-mdx > should register mdx loader correctly 1` ] = `
49
49
{
50
- " test" : / \\\\\\ . mdx \\ ? \\ $ / ,
50
+ " test" : / \\\\\\ . \\ ( \\ ? :mdx \\ | md \\ ) \\ $ / i ,
51
51
" use" : [
52
52
{
53
53
" loader" : " builtin:swc-loader" ,
You can’t perform that action at this time.
0 commit comments