Skip to content

Commit 60ccfaa

Browse files
committed
refactor: replace export default issue.
1 parent dfc6e96 commit 60ccfaa

File tree

2 files changed

+4
-21
lines changed

2 files changed

+4
-21
lines changed

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-replace-export-default": "^1.0.3",
2829
"babel-plugin-transform-remove-imports": "^1.7.0",
2930
"remark": "~13.0.0"
3031
},

core/src/utils/transform.ts

+3-21
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,9 @@
11
import { transform } from '@babel/standalone';
2-
import { PluginItem, PluginObj } from '@babel/core';
2+
import { PluginItem } from '@babel/core';
33
import removeImports from 'babel-plugin-transform-remove-imports';
4+
import replaceExportDefault from 'babel-plugin-transform-replace-export-default';
45
import { Options } from '../';
56

6-
export function defaultExportReplace(): PluginObj {
7-
return {
8-
name: 'transform-replace-export-default-to-return',
9-
visitor: {
10-
ExportDefaultDeclaration(path, opts) {
11-
const declaration = path.node.declaration;
12-
if (declaration.type === 'ClassDeclaration' || declaration.type === 'FunctionDeclaration') {
13-
declaration.id.name = `return ${declaration.id.name}`;
14-
} else if (declaration.type === 'Identifier') {
15-
declaration.name = `return ${declaration.name}`;
16-
}
17-
if (declaration) {
18-
path.replaceWith(declaration);
19-
}
20-
},
21-
},
22-
};
23-
}
24-
257
export const getTransformValue = (str: string, filename: string, opts: Options) => {
268
try {
279
const plugins: PluginItem[] = [...(opts.babelPlugins || [])];
@@ -31,7 +13,7 @@ export const getTransformValue = (str: string, filename: string, opts: Options)
3113
const result = transform(str, {
3214
filename,
3315
presets: ['env', 'es2015', 'react', 'typescript'],
34-
plugins: [...plugins, defaultExportReplace],
16+
plugins: [...plugins, replaceExportDefault],
3517
});
3618
return result.code;
3719
} catch (err) {

0 commit comments

Comments
 (0)