/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */ import Server from 'metro/src/Server' ; const outputBundle = require ( 'metro/src/shared/output/bundle' ) ; import path from 'path' ; import chalk from 'chalk' ; import { CommandLineArgs } from './bundleCommandLineArgs' ; import type { Config } from '@react-native-community/cli-types' ; import saveAssets from './saveAssets' ; import { default as loadMetroConfig , MetroConfig , } from '../../tools/loadMetroConfig' ; import { logger } from '@react-native-community/cli-tools' ; interface RequestOptions { entryFile : string ; sourceMapUrl : string | undefined ; dev : boolean ; minify : boolean ; platform : string | undefined ; unstable_transformProfile : string | undefined ; } export interface AssetData { __packager_asset : boolean ; fileSystemLocation : string ; hash : string ; height : number | null ; httpServerLocation : string ; name : string ; scales : number [ ] ; type : string ; width : number | null ; files : string [ ] ; } async function buildBundle ( args , ctx , output = outputBundle ) { const config = await loadMetroConfig ( ctx , { maxWorkers : args . maxWorkers , resetCache : args . resetCache , config : args . config , } ) ; return buildBundleWithConfig ( args , config , output ) ; } /**
 * Create a bundle using a pre-loaded Metro config. The config can be
 * re-used for several bundling calls if multiple platforms are being
 * bundled.
 */ export async function buildBundleWithConfig ( args , config , output = outputBundle ) { if ( config . resolver . platforms . indexOf ( args . platform ) === - 1 ) { logger . error ( `Invalid platform ${ args . platform ? `"${ chalk . bold ( args . platform ) }" ` : '' }selected.` ) ; logger . 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' ; let sourceMapUrl = args . sourcemapOutput ; if ( sourceMapUrl && ! args . sourcemapUseAbsolutePath ) { sourceMapUrl = path . basename ( sourceMapUrl ) ; } const requestOpts = { entryFile : args . entryFile , sourceMapUrl , 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 output . build ( server , requestOpts ) ; await output . save ( bundle , args , logger . info ) ; const outputAssets = await server . getAssets ( { ... Server . DEFAULT_BUNDLE_OPTIONS , ... requestOpts , bundleType : 'todo' , } ) ; return await saveAssets ( outputAssets , args . platform , args . assetsDest ) ; } finally { server . end ( ) ; } } export default buildBundle ;