Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iOS): release builds #12

Open
wants to merge 2 commits into
base: exodus
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,34 +135,52 @@ remotely behave unpredictably. I recommend using a third party debugging tool li
including your main application as well as your thread code can connect to Reactotron
and log debugging messages.

### Building for Release

### Set up automated bundling [iOS]

Bundling can be automated on iOS.

- Create a build phase "Bundle thread" and paste script below.

```bash
if [[ $CONFIGURATION == 'Release' ]]
then
source ../scripts/detect-nvm.sh
cd ..
sh ./scripts/bundleThread.sh
else
rm index.thread.jsbundle
touch index.thread.jsbundle
fi
```

- Once the script is there add item to Output Files and set `$(SRCROOT)/index.thread.jsbundle`
- Now create a `bundleThread.sh` script and paste code below. (Make sure the entry file is correct)

```bash
REACT_NATIVE_DIR="node_modules/react-native"
node "$REACT_NATIVE_DIR/local-cli/cli.js" bundle --dev false --assets-dest ./ios --entry-file threads/sdk/index.js --platform ios --bundle-output ./ios/index.thread.jsbundle
```



### Building for Release [Android]

You will need to manually bundle your thread files for use in a production release
of your app. This documentation assumes you have a single thread file called
`index.thread.js` in your project root. If your file is named differently or in
a different location, you can update the documented commands accordingly.

**Note**: If your single thread file is in a different location, the folder structure needs to
be replicated under `./ios` and `./android/app/src/main/assets/threads`.
be replicated under `./android/app/src/main/assets/threads`.

```
./App/Workers/worker.thread.js => ./ios/App/Workers/worker.thread.jsbundle
./App/Workers/worker.thread.js => ./android/app/src/main/assets/threads/App/Workers/worker.thread.jsbundle
```

For iOS you can use the following command:

`node node_modules/react-native/local-cli/cli.js bundle --dev false --assets-dest ./ios --entry-file index.thread.js --platform ios --bundle-output ./ios/index.thread.jsbundle`

Once you have generated the bundle file in your ios folder, you will also need to add
the bundle file to you project in Xcode. In Xcode's file explorer you should see
a folder with the same name as your app, containing a `main.jsbundle` file as well
as an `appDelegate.m` file. Right click on that folder and select the 'Add Files to <Your App Name>'
option, which will open up finder and allow you to select your `ios/index.thread.jsbundle`
file. You will only need to do this once, and the file will be included in all future
builds.

For Android create this direactory
Create this direactory
`mkdir ./android/app/src/main/assets/threads`

And then you can use the following command:
Expand Down
40 changes: 18 additions & 22 deletions ios/ThreadManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,30 @@ -(void) checkAndSendEvent:(NSString*)name body:(id)body
}

int threadId = nextThreadId++;

NSString *jsFileSlug = [name containsString:@"./"]
? [name stringByReplacingOccurrencesOfString:@"./" withString:@""]
: name;

// Bundled JavaScript is placed within the resources directory.
NSURL *bundledThreadURL = [[NSBundle mainBundle] URLForResource:[NSString stringWithFormat:@"%@", [jsFileSlug lastPathComponent]] withExtension:@"jsbundle"];

NSURL *threadURL;

#ifdef DEBUG
threadURL = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:name];
cawfree marked this conversation as resolved.
Show resolved Hide resolved
#else
NSString *jsBundlePath = [[NSBundle mainBundle] pathForResource:@"index.thread" ofType:@"jsbundle"];
// Check whether we can read bundle JS the resources directory. If
// the file is not found, error will be non-nil.
NSError *error = nil;
NSURL *threadURL = [bundledThreadURL checkResourceIsReachableAndReturnError:&error]
? bundledThreadURL
: [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:name];

#ifndef DEBUG
// For non-debug builds, we enforce that the bundle JS must
// be available in the resources dir by terminating before
// falling back to loading via Metro.
if (error) {
reject(

if(error != nil){
reject(
[NSString stringWithFormat:@"%ld", (long)error.code],
[NSString stringWithFormat:@"Unable to resolve thread bundle: %@", error.localizedDescription],
error
);
return;
}
#endif
);
return;
}

threadURL = [NSURL fileURLWithPath:jsBundlePath];
#endif

NSLog(@"starting Thread %@", [threadURL absoluteString]);

RCTBridgeModuleListProvider threadModuleProvider = ^NSArray<id<RCTBridgeModule>> *{
Expand Down