import type { Graph , MixedOutput , Module , SerializerOptions } from "metro" ; import * as semver from "semver" ; export type MetroPlugin < T = MixedOutput > = ( entryPoint : string , preModules : ReadonlyArray < Module < T > > , graph : Graph < T > , options : SerializerOptions < T > ) => void ; export type CustomSerializerResult = string | { code : string ; map : string ; } ; export type CustomSerializer = ( entryPoint : string , preModules : ReadonlyArray < Module > , graph : Graph , options : SerializerOptions ) => Promise < CustomSerializerResult > | CustomSerializerResult ; see https export function MetroSerializer ( plugins ) { const baseJSBundle = require ( "metro/src/DeltaBundler/Serializers/baseJSBundle" ) ; const bundleToString = require ( "metro/src/lib/bundleToString" ) ; const { version } = require ( "metro/package.json" ) ; const shouldReturnPromise = semver . satisfies ( version , ">=0.60.0" ) ; return ( entryPoint , preModules , graph , options ) : string | Promise < string > => { plugins . forEach ( ( plugin ) => plugin ( entryPoint , preModules , graph , options ) ) ; const bundle = baseJSBundle ( entryPoint , preModules , graph , options ) ; const bundleCode = bundleToString ( bundle ) . code ; return shouldReturnPromise ? Promise . resolve ( bundleCode ) : bundleCode ; } ; }