Skip to content

Commit

Permalink
Allow different flavors JS and App (#5404)
Browse files Browse the repository at this point in the history
<!-- Thanks for submitting a pull request! We appreciate you spending
the time to work on these changes. Please follow the template so that
the reviewers can easily understand what the code changes affect. -->

## Summary

tl;dr

## Test plan

### To make DEBUG bundle in RELEASE app

in `metro/Server.js` add

```js
transformOptions.dev = true;
```

below

```js
  async build(options) {
    const {
      entryFile,
      graphOptions,
      onProgress,
      resolverOptions,
      serializerOptions,
      transformOptions,
    } = splitBundleOptions(options);
```

and add `RCT_DEV=1` flag to pods in XCode

### To make RELEASE bundle in DEBUG app

```
Manually create release bundle with Metro, put it as e.g. `CUSTOM_BUNDLE.jsbundle` in `ios` and load it from file in AppDelegate.mm like this:

```mm
//#if DEBUG
// return [[RCTBundleURLProvider sharedSettings]
jsBundleURLForBundleRoot:@"index"];
//#else
return [[NSBundle mainBundle] URLForResource:@"CUSTOM_BUNDLE"
withExtension:@"jsbundle"];
//#endif
```

```

---------

Co-authored-by: Tomek Zawadzki <tomekzawadzki98@gmail.com>
  • Loading branch information
2 people authored and Aleksandra Cynk committed Nov 24, 2023
1 parent 713a98e commit a41d459
Show file tree
Hide file tree
Showing 22 changed files with 83 additions and 58 deletions.
9 changes: 2 additions & 7 deletions Common/cpp/LayoutAnimations/LayoutAnimationsManager.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#pragma once

#include "JSLogger.h"
#include "LayoutAnimationType.h"
#include "Shareables.h"

#ifndef NDEBUG
#include "JSLogger.h"
#endif

#include <jsi/jsi.h>
#include <stdio.h>
#include <functional>
Expand All @@ -23,10 +20,8 @@ using namespace facebook;

class LayoutAnimationsManager {
public:
#ifndef NDEBUG
explicit LayoutAnimationsManager(const std::shared_ptr<JSLogger> &jsLogger)
: jsLogger_(jsLogger) {}
#endif
void configureAnimation(
int tag,
LayoutAnimationType type,
Expand Down Expand Up @@ -54,8 +49,8 @@ class LayoutAnimationsManager {
std::unordered_map<int, std::shared_ptr<Shareable>> &getConfigsForType(
LayoutAnimationType type);

#ifndef NDEBUG
std::shared_ptr<JSLogger> jsLogger_;
#ifndef NDEBUG
// This set's function is to detect duplicate sharedTags on a single screen
// it contains strings in form: "reactScreenTag:sharedTag"
std::unordered_set<std::string> screenSharedTagSet_;
Expand Down
5 changes: 2 additions & 3 deletions Common/cpp/NativeModules/NativeReanimatedModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ NativeReanimatedModule::NativeReanimatedModule(
onRender(timestampMs);
}),
animatedSensorModule_(platformDepMethodsHolder),
#ifndef NDEBUG
layoutAnimationsManager_(std::make_shared<JSLogger>(jsScheduler_)),
#endif
jsLogger_(std::make_shared<JSLogger>(jsScheduler_)),
layoutAnimationsManager_(jsLogger_),
#ifdef RCT_NEW_ARCH_ENABLED
synchronouslyUpdateUIPropsFunction_(
platformDepMethodsHolder.synchronouslyUpdateUIPropsFunction),
Expand Down
5 changes: 5 additions & 0 deletions Common/cpp/NativeModules/NativeReanimatedModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec {
const jsi::Value &payload,
double currentTime);

inline std::shared_ptr<JSLogger> getJSLogger() const {
return jsLogger_;
}

#ifdef RCT_NEW_ARCH_ENABLED
bool handleRawEvent(const RawEvent &rawEvent, double currentTime);

Expand Down Expand Up @@ -185,6 +189,7 @@ class NativeReanimatedModule : public NativeReanimatedModuleSpec {
volatile bool renderRequested_{false};
const std::function<void(const double)> onRenderCallback_;
AnimatedSensorModule animatedSensorModule_;
const std::shared_ptr<JSLogger> jsLogger_;
LayoutAnimationsManager layoutAnimationsManager_;

#ifdef RCT_NEW_ARCH_ENABLED
Expand Down
4 changes: 1 addition & 3 deletions Common/cpp/ReanimatedRuntime/RNRuntimeDecorator.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "RNRuntimeDecorator.h"
#ifndef NDEBUG
#include "ReanimatedVersion.h"
#endif // NDEBUG

namespace reanimated {

Expand Down Expand Up @@ -32,7 +30,7 @@ void RNRuntimeDecorator::decorate(
rnRuntime.global().setProperty(rnRuntime, "_IS_FABRIC", isFabric);

#ifndef NDEBUG
checkJSVersion(rnRuntime);
checkJSVersion(rnRuntime, nativeReanimatedModule->getJSLogger());
#endif // NDEBUG

rnRuntime.global().setProperty(
Expand Down
6 changes: 2 additions & 4 deletions Common/cpp/Tools/JSLogger.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
#ifndef NDEBUG

#include "JSLogger.h"
#include <memory>

namespace reanimated {

void JSLogger::warnOnJS(const std::string &warning) const {
#ifndef NDEBUG
jsScheduler_->scheduleOnJS([warning](jsi::Runtime &rt) {
auto console = rt.global().getPropertyAsObject(rt, "console");
auto warn = console.getPropertyAsFunction(rt, "warn");
warn.call(rt, jsi::String::createFromUtf8(rt, warning));
});
#endif // NDEBUG
}

} // namespace reanimated

#endif // NDEBUG
4 changes: 0 additions & 4 deletions Common/cpp/Tools/JSLogger.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#ifndef NDEBUG

#pragma once

#include "JSScheduler.h"
Expand All @@ -20,5 +18,3 @@ class JSLogger {
};

} // namespace reanimated

#endif // NDEBUG
12 changes: 9 additions & 3 deletions Common/cpp/Tools/ReanimatedVersion.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "ReanimatedVersion.h"
#include <memory>
#include <regex>
#include <string>
#include "JSLogger.h"

#ifdef REANIMATED_VERSION
#define STRINGIZE(x) #x
Expand Down Expand Up @@ -41,26 +43,30 @@ bool matchVersion(const std::string &version1, const std::string &version2) {
}
}

void checkJSVersion(jsi::Runtime &rnRuntime) {
void checkJSVersion(
jsi::Runtime &rnRuntime,
const std::shared_ptr<JSLogger> &jsLogger) {
auto cppVersion = getReanimatedCppVersion();

auto maybeJSVersion =
rnRuntime.global().getProperty(rnRuntime, "_REANIMATED_VERSION_JS");
if (maybeJSVersion.isUndefined()) {
throw std::runtime_error(
jsLogger->warnOnJS(
std::string(
"[Reanimated] C++ side failed to resolve JavaScript code version\n") +
"See `https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#c-side-failed-to-resolve-javascript-code-version` for more details.");
return;
}

auto jsVersion = maybeJSVersion.asString(rnRuntime).utf8(rnRuntime);

if (!matchVersion(cppVersion, jsVersion)) {
throw std::runtime_error(
jsLogger->warnOnJS(
std::string(
"[Reanimated] Mismatch between C++ code version and JavaScript code version (") +
cppVersion + " vs. " + jsVersion + " respectively).\n" +
"See `https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#mismatch-between-c-code-version-and-javascript-code-version` for more details.");
return;
}

rnRuntime.global().setProperty(
Expand Down
6 changes: 3 additions & 3 deletions Common/cpp/Tools/ReanimatedVersion.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#pragma once

#include <jsi/jsi.h>
#include <memory>
#include <string>
#include "JSLogger.h"

using namespace facebook;

namespace reanimated {

std::string getReanimatedCppVersion();

#ifndef NDEBUG
bool matchVersion(const std::string &, const std::string &);
void checkJSVersion(jsi::Runtime &);
#endif // NDEBUG
void checkJSVersion(jsi::Runtime &, const std::shared_ptr<JSLogger> &);

}; // namespace reanimated
6 changes: 5 additions & 1 deletion Example/ios/ReanimatedExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,10 @@
baseConfigurationReference = 5B7EB9410499542E8C5724F5 /* Pods-ReanimatedExample-ReanimatedExampleTests.debug.xcconfig */;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
GCC_PREPROCESSOR_DEFINITIONS = "$(inherited)";
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
INFOPLIST_FILE = ReanimatedExampleTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.4;
LD_RUNPATH_SEARCH_PATHS = (
Expand Down Expand Up @@ -621,6 +624,7 @@
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION,
);
Expand Down
2 changes: 1 addition & 1 deletion Example/ios/ReanimatedExample/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#ifndef NDEBUG
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
Expand Down
6 changes: 5 additions & 1 deletion FabricExample/ios/FabricExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,10 @@
baseConfigurationReference = 5B7EB9410499542E8C5724F5 /* Pods-FabricExample-FabricExampleTests.debug.xcconfig */;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
GCC_PREPROCESSOR_DEFINITIONS = "$(inherited)";
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
INFOPLIST_FILE = FabricExampleTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.4;
LD_RUNPATH_SEARCH_PATHS = (
Expand Down Expand Up @@ -616,6 +619,7 @@
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION,
);
Expand Down
2 changes: 1 addition & 1 deletion FabricExample/ios/FabricExample/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#ifndef NDEBUG
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
Expand Down
6 changes: 5 additions & 1 deletion MacOSExample/ios/MacOSExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,10 @@
baseConfigurationReference = 5B7EB9410499542E8C5724F5 /* Pods-MacOSExample-MacOSExampleTests.debug.xcconfig */;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
GCC_PREPROCESSOR_DEFINITIONS = "$(inherited)";
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
INFOPLIST_FILE = MacOSExampleTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.4;
LD_RUNPATH_SEARCH_PATHS = (
Expand Down Expand Up @@ -567,6 +570,7 @@
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION,
);
Expand Down
2 changes: 1 addition & 1 deletion MacOSExample/ios/MacOSExample/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#ifndef NDEBUG
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
Expand Down
2 changes: 1 addition & 1 deletion MacOSExample/macos/MacOSExample-macOS/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#ifndef NDEBUG
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
Expand Down
1 change: 1 addition & 0 deletions MacOSExample/macos/MacOSExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
_LIBCPP_ENABLE_CXX17_REMOVED_UNARY_BINARY_FUNCTION,
);
Expand Down
10 changes: 8 additions & 2 deletions TVOSExample/ios/TVOSExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,10 @@
baseConfigurationReference = 36771B2C8C43AC6099096637 /* Pods-TVOSExample-TVOSExampleTests.debug.xcconfig */;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
GCC_PREPROCESSOR_DEFINITIONS = "$(inherited)";
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
INFOPLIST_FILE = TVOSExampleTests/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
Expand Down Expand Up @@ -912,7 +915,10 @@
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = "$(inherited)";
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
Expand Down
2 changes: 1 addition & 1 deletion TVOSExample/ios/TVOSExample/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ - (NSDictionary *)prepareInitialProps

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#ifndef NDEBUG
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
Expand Down
10 changes: 8 additions & 2 deletions __tests__/checkVersion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ describe('checkCppVersion', () => {
});

it('checks version successfully', () => {
expect(checkCppVersion).not.toThrow();
jest.spyOn(console, 'warn').mockImplementation();
checkCppVersion();
expect(console.warn).not.toBeCalled();
jest.clearAllMocks();
});

it('throws error when version is undefined', () => {
jest.spyOn(console, 'warn').mockImplementation();
delete global._REANIMATED_VERSION_CPP;
expect(checkCppVersion).toThrow();
checkCppVersion();
expect(console.warn).toBeCalled();
jest.clearAllMocks();
});
});

Expand Down
6 changes: 3 additions & 3 deletions app/src/examples/AboutExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ function getPlatformVersion() {
return Platform.Version;
}

function getMode() {
return __DEV__ ? 'Debug' : 'Release';
function getBundle() {
return __DEV__ ? 'dev' : 'production';
}

function getRuntime() {
Expand Down Expand Up @@ -54,7 +54,7 @@ export default function AboutExample() {
{getPlatformVersion()}
</Text>
<Text style={styles.text}>
<Text style={styles.bold}>Build type:</Text> {getMode()}
<Text style={styles.bold}>Bundle:</Text> {getBundle()}
</Text>
{!isWeb() && (
<>
Expand Down
7 changes: 4 additions & 3 deletions src/reanimated2/platform-specific/checkCppVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { jsVersion } from './jsVersion';
export function checkCppVersion() {
const cppVersion = global._REANIMATED_VERSION_CPP;
if (cppVersion === undefined) {
throw new Error(
console.warn(
`[Reanimated] Couldn't determine the version of the native part of Reanimated.
See \`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#couldnt-determine-the-version-of-the-native-part-of-reanimated\` for more details.`
See \`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#couldnt-determine-the-version-of-the-native-part-of-reanimated\` for more details.`
);
return;
}
const ok = matchVersion(jsVersion, cppVersion);
if (!ok) {
throw new Error(
`[Reanimated] Mismatch between JavaScript part and native part of Reanimated (${jsVersion} vs ${cppVersion}).
See \`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#mismatch-between-javascript-part-and-native-part-of-reanimated\` for more details.`
See \`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#mismatch-between-javascript-part-and-native-part-of-reanimated\` for more details.`
);
}
}
Expand Down
Loading

0 comments on commit a41d459

Please sign in to comment.