function getReplacements ( state ) { if ( state . opts instanceof Array ) { return state . opts ; } if ( state . opts && state . opts . replacements instanceof Array ) { return state . opts . replacements ; } return [ state . opts ] ; } export function babelPluginTransformImport ( { types : t } ) { const source = ( value , state , pipeImport ) => t . stringLiteral ( pipeImport ( value , state . filename ) ) ; return { visitor : { ImportDeclaration ( path , state ) { const replacements = getReplacements ( state ) ; replacements . forEach ( ( { pipeImport , removeCssImport } ) => { const { value } = path . node . source ; if ( removeCssImport ) { const cssRegex = '^(.+?)\\.(css|sass|scss|less)$' ; const cssPattern = new RegExp ( `^(${ cssRegex }|${ cssRegex }/.*)$` ) ; if ( cssPattern . test ( value ) ) { path . remove ( ) ; } } else { path . node . source = source ( value , state , pipeImport ) ; } } ) ; } , ExportDeclaration ( path , state ) { const replacements = getReplacements ( state ) ; replacements . forEach ( ( { pipeImport } ) => { if ( path . node . source ) { const { value } = path . node . source ; path . node . source = source ( value , state , pipeImport ) ; } } ) ; } , CallExpression ( path , state ) { const replacements = getReplacements ( state ) ; replacements . forEach ( ( { pipeImport } ) => { const { node } = path ; if ( node . callee . name === 'require' && node . arguments && node . arguments . length === 1 && t . isStringLiteral ( node . arguments [ 0 ] ) ) { path . node . arguments = [ source ( node . arguments [ 0 ] . value , state , pipeImport ) ] ; } } ) ; } , } , } ; }