1#!/bin/bash 2# Copyright (c) Meta Platforms, Inc. and affiliates. 3# 4# This source code is licensed under the MIT license found in the 5# LICENSE file in the root directory of this source tree. 6 7# Bundle React Native app's code and image assets. 8# This script is supposed to be invoked as part of Xcode build process 9# and relies on environment variables (including PWD) set by Xcode 10 11# Print commands before executing them (useful for troubleshooting) 12set -x 13DEST=$CONFIGURATION_BUILD_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH 14 15# Enables iOS devices to get the IP address of the machine running Metro 16if [[ "$CONFIGURATION" = *Debug* && ! "$PLATFORM_NAME" == *simulator ]]; then 17 for num in 0 1 2 3 4 5 6 7 8; do 18 IP=$(ipconfig getifaddr en${num}) 19 if [ ! -z "$IP" ]; then 20 break 21 fi 22 done 23 if [ -z "$IP" ]; then 24 IP=$(ifconfig | grep 'inet ' | grep -v ' 127.' | grep -v ' 169.254.' |cut -d\ -f2 | awk 'NR==1{print $1}') 25 fi 26 27 echo "$IP" > "$DEST/ip.txt" 28fi 29 30if [[ "$SKIP_BUNDLING" ]]; then 31 echo "SKIP_BUNDLING enabled; skipping." 32 exit 0; 33fi 34 35case "$CONFIGURATION" in 36 *Debug*) 37 if [[ "$PLATFORM_NAME" == *simulator ]]; then 38 if [[ "$FORCE_BUNDLING" ]]; then 39 echo "FORCE_BUNDLING enabled; continuing to bundle." 40 else 41 echo "Skipping bundling in Debug for the Simulator (since the packager bundles for you). Use the FORCE_BUNDLING flag to change this behavior." 42 exit 0; 43 fi 44 else 45 echo "Bundling for physical device. Use the SKIP_BUNDLING flag to change this behavior." 46 fi 47 48 DEV=true 49 ;; 50 "") 51 echo "$0 must be invoked by Xcode" 52 exit 1 53 ;; 54 *) 55 DEV=false 56 ;; 57esac 58 59# Path to react-native folder inside node_modules 60REACT_NATIVE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" 61# The project should be located next to where react-native is installed 62# in node_modules. 63PROJECT_ROOT=${PROJECT_ROOT:-"$REACT_NATIVE_DIR/../.."} 64 65cd "$PROJECT_ROOT" || exit 66 67# Define entry file 68if [[ "$ENTRY_FILE" ]]; then 69 # Use ENTRY_FILE defined by user 70 : 71elif [[ -s "index.ios.js" ]]; then 72 ENTRY_FILE=${1:-index.ios.js} 73else 74 ENTRY_FILE=${1:-index.js} 75fi 76 77# check and assign NODE_BINARY env 78# shellcheck source=/dev/null 79source "$REACT_NATIVE_DIR/scripts/node-binary.sh" 80 81HERMES_ENGINE_PATH="$PODS_ROOT/hermes-engine" 82[ -z "$HERMES_CLI_PATH" ] && HERMES_CLI_PATH="$HERMES_ENGINE_PATH/destroot/bin/hermesc" 83 84# Hermes is enabled in new projects by default, so we cannot assume that USE_HERMES=1 is set as an envvar. 85# If hermes-engine is found in Pods, we can assume Hermes has not been disabled. 86# If hermesc is not available and USE_HERMES is either unset or true, show error. 87if [[ -f "$HERMES_ENGINE_PATH" && ! -f "$HERMES_CLI_PATH" ]]; then 88 echo "error: Hermes is enabled but the hermesc binary could not be found at ${HERMES_CLI_PATH}." \ 89 "Perhaps you need to run 'bundle exec pod install' or otherwise " \ 90 "point the HERMES_CLI_PATH variable to your custom location." >&2 91 exit 2 92fi 93 94[ -z "$NODE_ARGS" ] && export NODE_ARGS="" 95 96[ -z "$CLI_PATH" ] && export CLI_PATH="$REACT_NATIVE_DIR/cli.js" 97 98[ -z "$BUNDLE_COMMAND" ] && BUNDLE_COMMAND="bundle" 99 100[ -z "$COMPOSE_SOURCEMAP_PATH" ] && COMPOSE_SOURCEMAP_PATH="$REACT_NATIVE_DIR/scripts/compose-source-maps.js" 101 102if [[ -z "$BUNDLE_CONFIG" ]]; then 103 CONFIG_ARG="" 104else 105 CONFIG_ARG="--config $BUNDLE_CONFIG" 106fi 107 108BUNDLE_FILE="$CONFIGURATION_BUILD_DIR/main.jsbundle" 109 110EXTRA_ARGS= 111 112case "$PLATFORM_NAME" in 113 "macosx") 114 BUNDLE_PLATFORM="macos" 115 ;; 116 *) 117 BUNDLE_PLATFORM="ios" 118 ;; 119esac 120 121if [ "${IS_MACCATALYST}" = "YES" ]; then 122 BUNDLE_PLATFORM="ios" 123fi 124 125EMIT_SOURCEMAP= 126if [[ ! -z "$SOURCEMAP_FILE" ]]; then 127 EMIT_SOURCEMAP=true 128fi 129 130PACKAGER_SOURCEMAP_FILE= 131if [[ $EMIT_SOURCEMAP == true ]]; then 132 if [[ $USE_HERMES == true ]]; then 133 PACKAGER_SOURCEMAP_FILE="$CONFIGURATION_BUILD_DIR/$(basename $SOURCEMAP_FILE)" 134 else 135 PACKAGER_SOURCEMAP_FILE="$SOURCEMAP_FILE" 136 fi 137 EXTRA_ARGS="$EXTRA_ARGS --sourcemap-output $PACKAGER_SOURCEMAP_FILE" 138fi 139 140# Hermes doesn't require JS minification. 141if [[ $USE_HERMES == true && $DEV == false ]]; then 142 EXTRA_ARGS="$EXTRA_ARGS --minify false" 143fi 144 145"$NODE_BINARY" $NODE_ARGS "$CLI_PATH" $BUNDLE_COMMAND \ 146 $CONFIG_ARG \ 147 --entry-file "$ENTRY_FILE" \ 148 --platform "$BUNDLE_PLATFORM" \ 149 --dev $DEV \ 150 --reset-cache \ 151 --bundle-output "$BUNDLE_FILE" \ 152 --assets-dest "$DEST" \ 153 $EXTRA_ARGS \ 154 $EXTRA_PACKAGER_ARGS 155 156if [[ $USE_HERMES != true ]]; then 157 cp "$BUNDLE_FILE" "$DEST/" 158 BUNDLE_FILE="$DEST/main.jsbundle" 159else 160 EXTRA_COMPILER_ARGS= 161 if [[ $DEV == true ]]; then 162 EXTRA_COMPILER_ARGS=-Og 163 else 164 EXTRA_COMPILER_ARGS=-O 165 fi 166 if [[ $EMIT_SOURCEMAP == true ]]; then 167 EXTRA_COMPILER_ARGS="$EXTRA_COMPILER_ARGS -output-source-map" 168 fi 169 "$HERMES_CLI_PATH" -emit-binary $EXTRA_COMPILER_ARGS -out "$DEST/main.jsbundle" "$BUNDLE_FILE" 170 if [[ $EMIT_SOURCEMAP == true ]]; then 171 HBC_SOURCEMAP_FILE="$BUNDLE_FILE.map" 172 "$NODE_BINARY" "$COMPOSE_SOURCEMAP_PATH" "$PACKAGER_SOURCEMAP_FILE" "$HBC_SOURCEMAP_FILE" -o "$SOURCEMAP_FILE" 173 fi 174 BUNDLE_FILE="$DEST/main.jsbundle" 175fi 176 177if [[ $DEV != true && ! -f "$BUNDLE_FILE" ]]; then 178 echo "error: File $BUNDLE_FILE does not exist. This must be a bug with React Native, please report it here: https://github.com/facebook/react-native/issues" >&2 179 exit 2 180fi 181