Skip to content

Commit 61932f1

Browse files
committed
fix: Fix loader example error issue.
1 parent 0f96f3c commit 61932f1

File tree

3 files changed

+46
-42
lines changed

3 files changed

+46
-42
lines changed

core/src/index.ts

+14-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import { PluginItem } from '@babel/core';
33
import { Options as RIOptions } from 'babel-plugin-transform-remove-imports';
44
import { getProcessor, getCodeBlock } from './utils';
5+
import { LoaderDefinitionFunction } from 'webpack';
56
export * from './utils';
67

78
export type CodeBlockItem = {
@@ -41,18 +42,25 @@ export type Options = {
4142
babelPlugins?: PluginItem[];
4243
};
4344

44-
export default function (source: string) {
45+
const codePreviewLoader: LoaderDefinitionFunction = function (source) {
4546
const options: Options = this.getOptions();
4647

47-
const codeBlock = getCodeBlock(getProcessor(source), options);
4848
let components = '';
49-
Object.keys(codeBlock).forEach((key) => {
50-
components += `${key}: (function() { ${codeBlock[key].code} })(),`;
51-
});
49+
let codeBlock = {} as CodeBlockData['data'];
50+
try {
51+
codeBlock = getCodeBlock(getProcessor(source), options, this.resourcePath);
52+
Object.keys(codeBlock).forEach((key) => {
53+
components += `${key}: (function() { ${codeBlock[key].code} })(),`;
54+
});
55+
} catch (error) {
56+
this.emitError(error);
57+
}
5258

5359
return `\nexport default {
5460
components: { ${components} },
5561
data: ${JSON.stringify(codeBlock, null, 2)},
5662
source: ${JSON.stringify(source)}
5763
}`;
58-
}
64+
};
65+
66+
export default codePreviewLoader;

core/src/utils/index.ts

+23-23
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,34 @@ export const getMetaId = (meta: string = '') => {
6363
export const isMeta = (meta: string = '') => meta && meta.includes('mdx:preview');
6464

6565
/** 获取需要渲染的代码块 **/
66-
export const getCodeBlock = (child: MarkdownParseData['children'], opts: Options = {}): CodeBlockData['data'] => {
66+
export function getCodeBlock(
67+
child: MarkdownParseData['children'],
68+
opts: Options = {},
69+
resourcePath?: string,
70+
): CodeBlockData['data'] {
6771
const { lang = ['jsx', 'tsx'] } = opts;
6872
// 获取渲染部分
6973
const codeBlock: Record<string | number, CodeBlockItem> = {};
70-
try {
71-
child.forEach((item) => {
72-
if (item && item.type === 'code' && lang.includes(item.lang)) {
73-
const line = item.position.start.line;
74-
const metaId = getMetaId(item.meta);
75-
if (isMeta(item.meta)) {
76-
let name = metaId || line;
77-
const funName = `${FUNNAME_PREFIX}${name}`;
78-
const returnCode = getTransformValue(item.value, `${funName}.${lang}`, opts);
79-
codeBlock[name] = {
80-
name,
81-
meta: getURLParameters(item.meta),
82-
code: returnCode,
83-
language: item.lang,
84-
value: item.value,
85-
};
86-
}
74+
child.forEach((item) => {
75+
if (item && item.type === 'code' && lang.includes(item.lang)) {
76+
const line = item.position.start.line;
77+
const metaId = getMetaId(item.meta);
78+
if (isMeta(item.meta)) {
79+
let name = metaId || line;
80+
const funName = `${resourcePath}.${FUNNAME_PREFIX}${name}`;
81+
const returnCode = getTransformValue(item.value, `${funName}.${item.lang}`, opts);
82+
codeBlock[name] = {
83+
name,
84+
meta: getURLParameters(item.meta),
85+
code: returnCode,
86+
language: item.lang,
87+
value: item.value,
88+
};
8789
}
88-
});
89-
} catch (err) {
90-
console.warn(err);
91-
}
90+
}
91+
});
9292
return codeBlock;
93-
};
93+
}
9494

9595
/**
9696
* `mdCodeModulesLoader` method for adding `markdown-react-code-preview-loader` to webpack config.

core/src/utils/transform.ts

+9-13
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@ import replaceExportDefault from 'babel-plugin-transform-replace-export-default'
55
import { Options } from '../';
66

77
export const getTransformValue = (str: string, filename: string, opts: Options) => {
8-
try {
9-
const plugins: PluginItem[] = [...(opts.babelPlugins || [])];
10-
if (opts.removeImports) {
11-
plugins.push([removeImports, opts.removeImports]);
12-
}
13-
const result = transform(str, {
14-
filename,
15-
presets: ['env', 'es2015', 'react', 'typescript'],
16-
plugins: [...plugins, replaceExportDefault],
17-
});
18-
return result.code;
19-
} catch (err) {
20-
console.warn(err);
8+
const plugins: PluginItem[] = [...(opts.babelPlugins || [])];
9+
if (opts.removeImports) {
10+
plugins.push([removeImports, opts.removeImports]);
2111
}
12+
const result = transform(str, {
13+
filename,
14+
presets: ['env', 'es2015', 'react', 'typescript'],
15+
plugins: [...plugins, replaceExportDefault],
16+
});
17+
return result.code;
2218
};

0 commit comments

Comments
 (0)