Skip to content

Commit a1d95de

Browse files
committed
feat: add removeImports/babelPlugins options.
1 parent 248cbba commit a1d95de

File tree

6 files changed

+84
-14
lines changed

6 files changed

+84
-14
lines changed

core/README-zh.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ npm i markdown-react-code-preview-loader -D
1919

2020
**第 ① 种方法,使用 mdCodeModulesLoader 方法**
2121

22+
`mdCodeModulesLoader` 用于在 webpack 配置添加 `markdown-react-code-preview-loader` 的方法。
23+
2224
```ts
2325
// .kktrc.ts
2426
import scopePluginOptions from '@kkt/scope-plugin-options';
@@ -67,7 +69,26 @@ export default (conf: Configuration, env: 'development' | 'production', options:
6769

6870
### options 参数
6971

70-
- lang: 需要解析代码块的语言,默认: `["jsx","tsx"]`
72+
```ts
73+
import { PluginItem } from '@babel/core';
74+
import { Options as RIOptions } from 'babel-plugin-transform-remove-imports'
75+
export type Options = {
76+
/**
77+
* 需要解析代码块的语言,默认: `["jsx","tsx"]`
78+
*/
79+
lang?: string[];
80+
/**
81+
* 删除过滤 imports 包;
82+
* babel (babel-plugin-transform-remove-imports) 包的 option 设置
83+
* https://github.com/uiwjs/babel-plugin-transform-remove-imports
84+
*/
85+
removeImports?: RIOptions;
86+
/**
87+
* 添加 babel 插件。
88+
*/
89+
babelPlugins?: PluginItem[];
90+
}
91+
```
7192
7293
## 项目中使用
7394

core/README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ After installing the dependency (loader), we need to configure the `loader` into
1919

2020
**① The first method, use the mdCodeModulesLoader method**
2121

22+
`mdCodeModulesLoader` method for adding `markdown-react-code-preview-loader` to webpack config.
23+
2224
```ts
2325
// .kktrc.ts
2426
import scopePluginOptions from '@kkt/scope-plugin-options';
@@ -67,7 +69,25 @@ export default (conf: Configuration, env: 'development' | 'production', options:
6769

6870
### options parameter
6971

70-
- lang: Language to parse code blocks, default: `["jsx","tsx"]`
72+
```ts
73+
import { PluginItem } from '@babel/core';
74+
import { Options as RIOptions } from 'babel-plugin-transform-remove-imports'
75+
export type Options = {
76+
/**
77+
* Language to parse code blocks, default: `["jsx","tsx"]`
78+
*/
79+
lang?: string[];
80+
/**
81+
* Option settings for the babel (babel-plugin-transform-remove-imports) package
82+
* https://github.com/uiwjs/babel-plugin-transform-remove-imports
83+
*/
84+
removeImports?: RIOptions;
85+
/**
86+
* Add babel plugins.
87+
*/
88+
babelPlugins?: PluginItem[];
89+
}
90+
```
7191
7292
## Used in the project
7393

core/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
},
2626
"dependencies": {
2727
"@babel/standalone": "^7.17.11",
28+
"babel-plugin-transform-remove-imports": "^1.7.0",
2829
"remark": "~13.0.0"
2930
},
3031
"devDependencies": {

core/src/index.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { getCodeBlockString } from './utils';
21
import React from 'react';
2+
import { PluginItem } from '@babel/core';
3+
import { Options as RIOptions } from 'babel-plugin-transform-remove-imports';
4+
import { getCodeBlockString } from './utils';
35
export * from './utils';
46

57
export type CodeBlockData = {
@@ -9,9 +11,25 @@ export type CodeBlockData = {
911
languages: Record<string | number, string>;
1012
};
1113

14+
export type Options = {
15+
/**
16+
* Language to parse code blocks, default: `["jsx","tsx"]`
17+
*/
18+
lang?: string[];
19+
/**
20+
* Option settings for the babel (babel-plugin-transform-remove-imports) package
21+
* https://github.com/uiwjs/babel-plugin-transform-remove-imports
22+
*/
23+
removeImports?: RIOptions;
24+
/**
25+
* Add babel plugins.
26+
*/
27+
babelPlugins?: PluginItem[];
28+
};
29+
1230
export default function (source: string) {
13-
const options = this.getOptions();
14-
const result = getCodeBlockString(source, options.lang || ['tsx', 'jsx']);
31+
const options: Options = this.getOptions();
32+
const result = getCodeBlockString(source, options);
1533

1634
return `
1735
${result}

core/src/utils/index.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import { MarkDownTreeType, CodeBlockItemType } from './interface';
55
import { getTransformValue } from './transform';
66
import webpack from 'webpack';
7-
export * from './interface';
87
import remark from 'remark';
8+
export * from './interface';
9+
import { Options } from '../';
910

1011
/** 转换 代码*/
1112
const getProcessor = (scope: string) => {
@@ -32,7 +33,8 @@ const getMeta = (meta: string | null): Record<string, string | boolean> => {
3233
};
3334

3435
/** 获取需要渲染的代码块 **/
35-
const getCodeBlock = (child: MarkDownTreeType['children'], lang: string[] = ['jsx', 'tsx']) => {
36+
const getCodeBlock = (child: MarkDownTreeType['children'], opts: Options = {}) => {
37+
const { lang = ['jsx', 'tsx'] } = opts;
3638
// 获取渲染部分
3739
const codeBlock: Record<string | number, CodeBlockItemType> = {};
3840
try {
@@ -43,7 +45,7 @@ const getCodeBlock = (child: MarkDownTreeType['children'], lang: string[] = ['js
4345
if (metaData.preview) {
4446
let name = typeof metaData.preview === 'string' ? metaData.preview : line;
4547
const funName = `BaseCode${line}`;
46-
const returnCode = getTransformValue(item.value, `${funName}.${lang}`, funName);
48+
const returnCode = getTransformValue(item.value, `${funName}.${lang}`, funName, opts);
4749
codeBlock[line] = {
4850
code: returnCode,
4951
name,
@@ -81,15 +83,15 @@ const createStr = (codeBlock: Record<string | number, CodeBlockItemType>) => {
8183
return indexStr;
8284
};
8385

84-
export const getCodeBlockString = (scope: string, lang: string[] = ['jsx', 'tsx']) => {
86+
export const getCodeBlockString = (scope: string, opts: Options = {}) => {
8587
const children = getProcessor(scope);
86-
const codeBlock = getCodeBlock(children, lang);
88+
const codeBlock = getCodeBlock(children, opts);
8789
const result = createStr(codeBlock);
8890
return result;
8991
};
9092

9193
/**
92-
* 配置好markdown的loader
94+
* 用于修改 webpack 配置 loader 的方法
9395
* @param {webpack.Configuration} config webpack配置
9496
* @param {string[]} lang 解析语言
9597
* @returns {webpack.Configuration}

core/src/utils/transform.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
import { transform } from '@babel/standalone';
2+
import { PluginItem } from '@babel/core';
3+
import { Options } from '../';
24

3-
export function babelTransform(input: string, filename: string) {
5+
export function babelTransform(input: string, filename: string, opts: Options = {}) {
6+
const plugins: PluginItem[] = [...(opts.babelPlugins || [])];
7+
if (opts.removeImports) {
8+
plugins.push(['babel-plugin-transform-remove-imports', opts.removeImports]);
9+
}
410
return transform(input, {
511
filename,
612
presets: ['env', 'es2015', 'react', 'typescript'],
13+
plugins: [...plugins],
714
});
815
}
916

10-
export const getTransformValue = (str: string, filename: string, funName: string) => {
17+
export const getTransformValue = (str: string, filename: string, funName: string, opts: Options) => {
1118
try {
1219
const isReact = /import\x20+React(\x20+|[\x20+,]+({[a-zA-Z0-9,\s]+}|{})\x20+)from\x20+('|")react('|")/.test(str);
1320
// 先判断 是否引入 react
1421
const tran = isReact ? str : `import React from "react"\n ${str}`;
1522
/** 先把默认导出 export default 进行替换 **/
1623
const newCode = `${tran.replace(/export\x20+default/, 'const _default = ')}\n`;
17-
const code = `${babelTransform(newCode, `${filename}`).code}\n return _react["default"].createElement(_default)`;
24+
const tranCode = babelTransform(newCode, `${filename}`, opts).code;
25+
const code = `${tranCode}\n return _react["default"].createElement(_default)`;
1826
return `function ${funName}(){\n${code}\n};`;
1927
} catch (err) {
2028
console.warn(err);

0 commit comments

Comments
 (0)