Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Run workers on iOS device using offline release bundles #21

Open
therealgilles opened this issue Nov 17, 2016 · 0 comments
Open

Run workers on iOS device using offline release bundles #21

therealgilles opened this issue Nov 17, 2016 · 0 comments

Comments

@therealgilles
Copy link

therealgilles commented Nov 17, 2016

Here is what I had to do to make workers work on iOS using offline release bundles.

  1. Fix node_modules/react-native-workers/ios/Workers/WorkerManager.m as follows:
  //NSURL *workerURL = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:name fallbackResource:nil];
  NSURL *workerURL = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:name fallbackResource:name];
  1. Create a react-native-workers.sh script (see below) inside the ios folder and add it to Build Phases > Bundle React Native code and images:
export NODE_BINARY=node
./react-native-workers.sh
../node_modules/react-native/packager/react-native-xcode.sh

--- ios/react-native-workers.sh ---
Change App/Workers and App/Workers/*Worker.js to match the location and filename pattern of your workers.

#!/bin/bash
# Copyright (c) 2015-present, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.

# Bundle React Native app's code and image assets.
# This script is supposed to be invoked as part of Xcode build process
# and relies on environment variables (including PWD) set by Xcode

case "$CONFIGURATION" in
  Debug)
    # Speed up build times by skipping the creation of the offline package for debug
    # builds on the simulator since the packager is supposed to be running anyways.
    if [[ "$PLATFORM_NAME" == *simulator ]]; then
      echo "Skipping bundling for Simulator platform"
      exit 0;
    fi

    DEV=true
    ;;
  "")
    echo "$0 must be invoked by Xcode"
    exit 1
    ;;
  *)
    DEV=false
    ;;
esac

# Path to react-native folder inside node_modules
REACT_NATIVE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../node_modules/react-native" && pwd)"

# Xcode project file for React Native apps is located in ios/ subfolder
cd ..

# Define NVM_DIR and source the nvm.sh setup script
[ -z "$NVM_DIR" ] && export NVM_DIR="$HOME/.nvm"

if [[ -s "$HOME/.nvm/nvm.sh" ]]; then
  . "$HOME/.nvm/nvm.sh"
elif [[ -x "$(command -v brew)" && -s "$(brew --prefix nvm)/nvm.sh" ]]; then
  . "$(brew --prefix nvm)/nvm.sh"
fi

# Set up the nodenv node version manager if present
if [[ -x "$HOME/.nodenv/bin/nodenv" ]]; then
  eval "$("$HOME/.nodenv/bin/nodenv" init -)"
fi

[ -z "$NODE_BINARY" ] && export NODE_BINARY="node"

nodejs_not_found()
{
  echo "error: Can't find '$NODE_BINARY' binary to build React Native bundle" >&2
  echo "If you have non-standard nodejs installation, select your project in Xcode," >&2
  echo "find 'Build Phases' - 'Bundle React Native code and images'" >&2
  echo "and change NODE_BINARY to absolute path to your node executable" >&2
  echo "(you can find it by invoking 'which node' in the terminal)" >&2
  exit 2
}

type $NODE_BINARY >/dev/null 2>&1 || nodejs_not_found

# Print commands before executing them (useful for troubleshooting)
set -x

# change App/Workers to match the location of your workers
DEST=$CONFIGURATION_BUILD_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH/App/Workers
mkdir -p $DEST

# Get list of entry files (change App/Workers/*Worker.js to match the location and filename pattern of your workers)
for ENTRY_FILE in ${1:-App/Workers/*Worker.js}
do

  ENTRY_FILE_BASENAME=$(basename $ENTRY_FILE | cut -d. -f1)
  BUNDLE_FILE="$DEST/$ENTRY_FILE_BASENAME.jsbundle"

  $NODE_BINARY "$REACT_NATIVE_DIR/local-cli/cli.js" bundle \
    --entry-file "$ENTRY_FILE" \
    --platform ios \
    --dev $DEV \
    --reset-cache \
    --bundle-output "$BUNDLE_FILE" \
    --assets-dest "$DEST"

  if [[ ! $DEV && ! -f "$BUNDLE_FILE" ]]; then
    echo "error: File $BUNDLE_FILE does not exist. This must be a bug with" >&2
    echo "React Native, please report it here: https://github.com/facebook/react-native/issues"
    exit 2
  fi

done
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant