import { API , FileInfo , ImportDefaultSpecifier , ImportNamespaceSpecifier , ImportSpecifier , JSCodeshift , Options , } from 'jscodeshift' ; import { bundleExportMap , sourceMap } from './maps' ; type ImportReplacements = { [ source ] : { default ? : ImportDefaultSpecifier ; namedImports ? : ImportSpecifier [ ] ; namespace ? : ImportNamespaceSpecifier ; } ; } ; param specifier param source param newImports const addMappedDefaultSpecifier = ( specifier , source , newImports , j ) => { const specifierName = specifier . local ?. name ; if ( ! specifierName ) { return ; } if ( specifierName === 'type' && source === '@workday/canvas-kit-labs-react-core' ) { addMappedSpecifier ( j . importSpecifier ( j . identifier ( specifierName ) ) , source , newImports ) ; return ; } const mappedSource = bundleExportMap [ specifierName ] ; newImports [ mappedSource ] = { ... newImports [ mappedSource ] , default : specifier , } ; } ; param specifier param source param newImports const addMappedNamespaceSpecifier = ( specifier , source , newImports ) => { const specifierName = specifier . local ?. name ; if ( ! specifierName ) { return ; } if ( source === '@workday/canvas-kit-react' ) { console . error ( `Failed to split namespace import \`import * as ${ specifierName } from ${ source }\`` ) ; return ; } const mappedSource = sourceMap [ source ] ; newImports [ mappedSource ] = { ... newImports [ mappedSource ] , namespace : specifier , } ; } ; param specifier param source param newImports const addMappedSpecifier = ( specifier , source , newImports ) => { const specifierName = specifier . imported . name ; const newSource = source === '@workday/canvas-kit-react' ? bundleExportMap [ specifierName ] : sourceMap [ source ] ; if ( ! newImports [ newSource ] || ! newImports [ newSource ] . namedImports ) { newImports [ newSource ] = { ... newImports [ newSource ] , namedImports : [ ] , } ; } newImports [ newSource ] . namedImports ?. push ( specifier ) ; } ; export default function transformer ( file , api , options ) { const j = api . jscodeshift ; return j ( file . source ) . find ( j . ImportDeclaration ) . forEach ( _import => { if ( typeof _import . value . source . value !== 'string' || ( _import . value . source . value !== '@workday/canvas-kit-react' && ! Object . keys ( sourceMap ) . includes ( _import . value . source . value ) ) ) { return ; } const source = _import . value . source . value ; const newImports = { } ; _import . value . specifiers ?. forEach ( specifier => { switch ( specifier . type ) { case 'ImportDefaultSpecifier' : addMappedDefaultSpecifier ( specifier , source , newImports , j ) ; break ; case 'ImportNamespaceSpecifier' : addMappedNamespaceSpecifier ( specifier , source , newImports ) ; break ; case 'ImportSpecifier' : default : addMappedSpecifier ( specifier , source , newImports ) ; break ; } } ) ; if ( ! Object . keys ( newImports ) . length ) { return ; } j ( _import ) . replaceWith ( Object . keys ( newImports ) . map ( source => { const newImportSet = [ ] ; const { default : defaultImport , namespace , namedImports } = newImports [ source ] ; if ( defaultImport ) { newImportSet . push ( defaultImport ) ; } if ( namespace ) { newImportSet . push ( namespace ) ; } if ( namedImports ?. length ) { namedImports ?. forEach ( specifier => { newImportSet . push ( specifier ) ; } ) ; } return j . importDeclaration ( newImportSet , j . stringLiteral ( source ) , 'value' ) ; } ) ) ; } ) . toSource ( ) ; }