import * as t from '@babel/types' ; const mountStatement = `
  export function mount(ModuleComponent, targetNode, props) {
    ReactDOM.render(React.createElement(ModuleComponent, Object.assign({}, props)), targetNode);
  }
  ` ; const unmountStatement = `
  export function unmount(targetNode) {
    ReactDOM.unmountComponentAtNode(targetNode);
  }
` ; export default ( api , { entries } ) => { const checkEntryPoint = ( filename ) => { return entries . some ( entry => filename . includes ( entry ) ) ; } ; return { visitor : { Program ( nodePath , state ) { if ( checkEntryPoint ( state . filename ) ) { const node = nodePath . node ; const { body } = node ; let reactdomStatement = false ; let mountExportStatement = false ; let unmountExportStatement = false ; body . forEach ( ( item ) => { if ( t . isImportDeclaration ( item ) ) { if ( t . isStringLiteral ( item . source , { value : 'react-dom' } ) ) { reactdomStatement = true ; const reactDomDefaultExport = item . specifiers . some ( value => { return t . isImportDefaultSpecifier ( value ) && t . isIdentifier ( value . local , { name : 'ReactDOM' } ) ; } ) ; if ( ! reactDomDefaultExport ) { item . specifiers . push ( t . importDefaultSpecifier ( t . identifier ( 'ReactDOM' ) ) ) ; } } } if ( t . isExportNamedDeclaration ( item ) ) { const isFuntionDec = ( name ) => t . isFunctionDeclaration ( item . declaration ) && t . isIdentifier ( item . declaration . id , { name } ) ; const isVariableDec = ( name ) => t . isVariableDeclaration ( item . declaration ) && t . isIdentifier ( item . declaration . declarations [ 0 ] , { name } ) ; if ( isFuntionDec ( 'mount' ) || isVariableDec ( 'mount' ) ) { mountExportStatement = true ; } if ( isFuntionDec ( 'unmount' ) || isVariableDec ( 'unmount' ) ) { unmountExportStatement = true ; } } } ) ; if ( ! reactdomStatement ) { body . push ( t . importDeclaration ( [ t . importDefaultSpecifier ( t . identifier ( 'ReactDOM' ) ) , ] , t . stringLiteral ( 'react-dom' ) ) ) ; } if ( ! mountExportStatement || ! unmountExportStatement ) { if ( ! mountExportStatement && ! unmountExportStatement ) { nodePath . pushContainer ( 'body' , api . template ( `${ mountStatement } \n ${ unmountStatement }` ) ( ) ) ; } else if ( ! mountExportStatement ) { nodePath . pushContainer ( 'body' , api . template ( mountStatement ) ( ) ) ; } else { nodePath . pushContainer ( 'body' , api . template ( unmountStatement ) ( ) ) ; } } } } } } ; } ;