1
1
import { transform } from '@babel/standalone' ;
2
- import { PluginItem , PluginObj } from '@babel/core' ;
2
+ import { PluginItem } from '@babel/core' ;
3
3
import removeImports from 'babel-plugin-transform-remove-imports' ;
4
+ import replaceExportDefault from 'babel-plugin-transform-replace-export-default' ;
4
5
import { Options } from '../' ;
5
6
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
-
25
7
export const getTransformValue = ( str : string , filename : string , opts : Options ) => {
26
8
try {
27
9
const plugins : PluginItem [ ] = [ ...( opts . babelPlugins || [ ] ) ] ;
@@ -31,7 +13,7 @@ export const getTransformValue = (str: string, filename: string, opts: Options)
31
13
const result = transform ( str , {
32
14
filename,
33
15
presets : [ 'env' , 'es2015' , 'react' , 'typescript' ] ,
34
- plugins : [ ...plugins , defaultExportReplace ] ,
16
+ plugins : [ ...plugins , replaceExportDefault ] ,
35
17
} ) ;
36
18
return result . code ;
37
19
} catch ( err ) {
0 commit comments