import { parse } from 'semver' ; import { version as reactNativeVersionString } from 'react-native/package.json' ; if ( ! reactNativeVersionString ) { throw new Error ( 'Cannot detect React Native version. Module react-native package.json missing version' ) ; } export type TransformOptions = { src : string ; filename : string ; options : { customTransformOptions : { [ key ] : any ; } ; dev : boolean ; hot : boolean ; inlinePlatform : boolean ; minify : boolean ; platform : string ; experimentalImportSupport : boolean ; type : string ; inlineRequires : boolean ; enableBabelRCLookup : boolean ; enableBabelRuntime : boolean ; projectRoot : string ; publicPath : string ; } ; } ; export type Transformer = { transform ( options ) : void ; } ; export type LegacyTransformer = { transform ( src , filename , options ) : void ; } ; let transformer ; const reactNativeMinorVersion = parse ( reactNativeVersionString ) ?. minor ; if ( ! reactNativeMinorVersion ) { throw new Error ( `Failed to parse minor version from React Native version '${ reactNativeVersionString }'` ) ; } if ( reactNativeMinorVersion >= 59 ) { transformer = require ( 'metro-react-native-babel-transformer' ) as Transformer ; } else if ( reactNativeMinorVersion >= 56 ) { transformer = require ( 'metro/src/reactNativeTransformer' ) as Transformer ; } else if ( reactNativeMinorVersion >= 52 ) { transformer = require ( 'metro/src/transformer' ) as Transformer ; } else if ( reactNativeMinorVersion >= 47 ) { transformer = require ( 'metro-bundler/src/transformer' ) as Transformer ; } else if ( reactNativeMinorVersion === 46 ) { transformer = require ( 'metro-bundler/build/transformer' ) as Transformer ; } else { const legacyUpstreamTransformer = require ( 'react-native/packager/transformer' ) as LegacyTransformer ; transformer = { transform ( { src , filename , options } ) { return legacyUpstreamTransformer . transform ( src , filename , options ) ; } , } ; } export const upstreamTransformer = transformer ;