import type { NodePath } from '@babel/traverse' ; import * as t from '@babel/types' ; param name param localName const importSpecifier = ( name , localName ? ) : t . ImportSpecifier => { return t . importSpecifier ( t . identifier ( name ) , t . identifier ( localName || name ) ) ; } ; const COMPILED_RUNTIME_IMPORTS = [ 'ax' , 'ix' , 'CC' , 'CS' ] ; const COMPILED_RUNTIME_MODULE = '@compiled/react/runtime' ; param path export const appendRuntimeImports = ( path ) : void => { const previouslyDeclaredRuntimeDeclaration = path . get ( 'body' ) . find ( ( childPath ) : childPath is NodePath < t . ImportDeclaration > => { return ( t . isImportDeclaration ( childPath . node ) && childPath . node . source . value === COMPILED_RUNTIME_MODULE ) ; } ) ; if ( previouslyDeclaredRuntimeDeclaration ) { /**
         * Get local import name instead of imported name to handle scenario when
         * import specifier is imported as named and normal both.
         *
         * eg. import { CC as CompiledRoot, ax, CC, CS } from '@compiled/react/runtime';
         * In above example `CC` is used both as `CompiledRoot` and `CC`.
         */ const localImportNames = previouslyDeclaredRuntimeDeclaration . get ( 'specifiers' ) . map ( ( specifier ) => specifier . node . local . name ) ; COMPILED_RUNTIME_IMPORTS . forEach ( ( runtimeImportName ) => { if ( ! localImportNames . includes ( runtimeImportName ) ) { previouslyDeclaredRuntimeDeclaration . pushContainer ( 'specifiers' , importSpecifier ( runtimeImportName ) ) ; } } ) ; } else { path . unshiftContainer ( 'body' , t . importDeclaration ( COMPILED_RUNTIME_IMPORTS . map ( ( runtimeImportName ) => importSpecifier ( runtimeImportName ) ) , t . stringLiteral ( COMPILED_RUNTIME_MODULE ) ) ) ; } } ;