import chalk from "chalk" ; import type { AssetData , OutputOptions } from "metro" ; import type { TransformProfile } from "metro-babel-transformer" ; import type { ConfigT } from "metro-config" ; import Server from "metro/src/Server" ; import Bundle from "metro/src/shared/output/bundle" ; import { saveAssets } from "./asset" ; export type BundleArgs = { assetsDest ? : string ; entryFile : string ; minify ? : boolean ; platform : string ; dev : boolean ; bundleOutput : string ; bundleEncoding ? : OutputOptions [ "bundleEncoding" ] ; sourcemapOutput ? : string ; sourcemapSourcesRoot ? : string ; unstableTransformProfile ? : TransformProfile ; } ; type RequestOptions = { entryFile : string ; sourceMapUrl ? : string ; dev : boolean ; minify : boolean ; platform : string ; unstable_transformProfile ? : TransformProfile ; } ; export async function bundle ( args , config ) { if ( config . resolver . platforms . indexOf ( args . platform ) === - 1 ) { console . error ( `Invalid platform ${ args . platform ? `"${ chalk . bold ( args . platform ) }" ` : "" }selected.` ) ; console . info ( `Available platforms are: ${ config . resolver . platforms . map ( ( x ) => `"${ chalk . bold ( x ) }"` ) . join ( ", " ) }. If you are trying to bundle for an out-of-tree platform, it may not be installed.` ) ; throw new Error ( "Bundling failed" ) ; } process . env . NODE_ENV = args . dev ? "development" : "production" ; const requestOpts = { entryFile : args . entryFile , sourceMapUrl : args . sourcemapOutput , dev : args . dev , minify : args . minify !== undefined ? args . minify : ! args . dev , platform : args . platform , unstable_transformProfile : args . unstableTransformProfile } ; const server = new Server ( config ) ; try { const bundle = await Bundle . build ( server , requestOpts ) ; await Bundle . save ( bundle , args , console . info ) ; const outputAssets = await server . getAssets ( { ... Server . DEFAULT_BUNDLE_OPTIONS , ... requestOpts , bundleType : "todo" } ) ; return await saveAssets ( outputAssets , args . platform , args . assetsDest ) ; } finally { server . end ( ) ; } }