import * as parser from '@babel/parser' ; import * as generator from '@babel/generator' ; import traverse from '@babel/traverse' ; import get from 'lodash/get' ; const fromSpecifiers = ( specifiers = [ ] ) => Array . isArray ( specifiers ) && specifiers . length > 0 ? specifiers . map ( specifier => get ( specifier , 'local.name' ) ) : [ ] ; const traverseOnImports = ( fn ) => ( node ) => { try { const ast = parser . parse ( node . value , { sourceType : 'module' } ) ; let populated = [ ] ; traverse ( ast as any , { enter ( path ) { if ( path . isImportDeclaration ( ) ) { populated = populated . concat ( fn ( path ) ) ; return ; } } , } ) ; return populated ; } catch ( err ) { return [ ] ; } } ; export const getFullImports = traverseOnImports ( ( path ) => [ get ( generator . default ( path . node ) , 'code' ) , ] ) ; export const getImportsVariables = traverseOnImports ( ( path ) => fromSpecifiers ( path . node . specifiers ) ) ;