Skip to content

Commit

Permalink
plugin: support react-native 0.60+
Browse files Browse the repository at this point in the history
  • Loading branch information
jaimecbernardo committed Sep 17, 2019
1 parent dd7c67a commit 7cd4a01
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 385 deletions.
33 changes: 10 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ We have a [central repo](https://github.com/janeasystems/nodejs-mobile/issues) w

`$ npm install nodejs-mobile-react-native --save`

`$ react-native link nodejs-mobile-react-native`
For iOS, run `pod install` for linking the native code parts:

`$ cd iOS && pod install`

### iOS

Expand Down Expand Up @@ -307,39 +309,24 @@ react-native run-android
### Duplicate module name

During the `react-native` application's build process, the `nodejs-project` gets copied to the application's assets, where they'll be used by `nodejs-mobile`.
The `react-native` packager monitors the project's folder for javascript packages and may throw a "`Error: jest-haste-map: @providesModule naming collision`" error.
The `react-native` packager monitors the project's folder for javascript packages and may throw a "`jest-haste-map: Haste module naming collision`" error.

To avoid this error, instruct the `react-native` packager to ignore the `nodejs-project` and the platform folders where it is copied to. Create a `rn-cli.config.js` file in your `react-native` project's root path with the following contents if you're using recent versions of `react-native` (`>= v0.57`):
To avoid this error, instruct the `react-native` packager to ignore the `nodejs-project` and the platform folders where it is copied to. Edit the `metro.config.js` file in your `react-native` project's root path with the following contents if you're using recent versions of `react-native` (`>= v0.60`) and add the `blacklist` require and the following `resolver` to the module exports:

```js
const blacklist = require('metro-config/src/defaults/blacklist');

module.exports = {
resolver:{
resolver: {
blacklistRE: blacklist([
/nodejs-assets\/.*/,
/android\/.*/,
/ios\/.*/
/\/nodejs-assets\/.*/,
/\/android\/.*/,
/\/ios\/.*/
])
},
};

```

If the project has a `metro.config.js` file, it will be picked up instead of `rn-cli.config.js` on newer versions of `react-native`. If that's the case, the `resolver` block has to be added to the `metro.config.js` file instead.
...

These are the contents of `rn-cli.config.js` if `react-native < v0.57`:
```js
const blacklist = require('metro/src/blacklist');

module.exports = {
getBlacklistRE: function() {
return blacklist([
/nodejs-assets\/.*/,
/android\/.*/,
/ios\/.*/
]);
},
};
```

Expand Down
24 changes: 24 additions & 0 deletions nodejs-mobile-react-native.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'json'

package = JSON.parse(File.read(File.join(__dir__, 'package.json')))

Pod::Spec.new do |s|
s.name = package['name']
s.version = package['version']
s.summary = package['description']
s.license = package['license']
s.source = { :git => 'https://github.com/JaneaSystems/nodejs-mobile-react-native.git', :tag => 'v#{s.version}' }
s.authors = package['author']
s.homepage = package['homepage']
s.platform = :ios, '9.0'
s.source_files = 'ios/*.{h,m,mm,hpp,cpp}'
s.compiler_flags = '-I$(PODS_TARGET_SRCROOT)/ios/libnode/include/node/'
s.pod_target_xcconfig = {
'CLANG_CXX_LANGUAGE_STANDARD' => 'gnu++0x',
'ENABLE_BITCODE' => 'NO'
}
s.user_target_xcconfig = { 'ENABLE_BITCODE' => 'NO' }
s.ios.vendored_frameworks = 'ios/NodeMobile.framework'
s.static_framework = true
s.dependency 'React'
end
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,12 @@
"url": "https://github.com/janeasystems/nodejs-mobile/issues"
},
"peerDependencies": {
"react-native": ">=0.52.0"
"react-native": ">=0.60.0"
},
"dependencies": {
"mkdirp": "^0.5.1",
"ncp": "^2.0.0",
"nodejs-mobile-gyp": "^0.3.1",
"xcode": "^2.0.0"
},
"rnpm": {
"commands": {
"postlink": "node node_modules/nodejs-mobile-react-native/scripts/module-postlink.js"
}
}
}
31 changes: 31 additions & 0 deletions react-native.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
dependency: {
platforms: {
android: {
sourceDir: './android'
},
ios: {
project: './ios/RNNodeJsMobile.xcodeproj',
scriptPhases: [
{
name: '[NODEJS MOBILE] Copy Node.js Project files',
path: './scripts/ios-copy-nodejs-project.sh',
execution_position: 'after_compile'
}, {
name: '[NODEJS MOBILE] Build Native Modules',
path: './scripts/ios-build-native-modules.sh',
execution_position: 'after_compile'
}, {
name: '[NODEJS MOBILE] Sign Native Modules',
path: './scripts/ios-sign-native-modules.sh',
execution_position: 'after_compile'
}, {
name: '[NODEJS MOBILE] Remove Simulator Strip',
path: './scripts/ios-remove-framework-simulator-strips.sh',
execution_position: 'after_compile'
}
]
}
}
}
}
54 changes: 54 additions & 0 deletions scripts/ios-build-native-modules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/sh
set -e
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
# If build native modules preference is not set, look for it in the project's
#nodejs-assets/BUILD_NATIVE_MODULES.txt file.
NODEJS_ASSETS_DIR="$( cd "$PROJECT_DIR" && cd ../nodejs-assets/ && pwd )"
PREFERENCE_FILE_PATH="$NODEJS_ASSETS_DIR/BUILD_NATIVE_MODULES.txt"
if [ -f "$PREFERENCE_FILE_PATH" ]; then
NODEJS_MOBILE_BUILD_NATIVE_MODULES="$(cat $PREFERENCE_FILE_PATH | xargs)"
fi
fi
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
# If build native modules preference is not set, try to find .gyp files
#to turn it on.
gypfiles=($(find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -type f -name "*.gyp"))
if [ ${#gypfiles[@]} -gt 0 ]; then
NODEJS_MOBILE_BUILD_NATIVE_MODULES=1
else
NODEJS_MOBILE_BUILD_NATIVE_MODULES=0
fi
fi
if [ "1" != "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then exit 0; fi
# Delete object files that may already come from within the npm package.
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.o" -type f -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.a" -type f -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.node" -type f -delete
# Delete bundle contents that may be there from previous builds.
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -path "*/*.node/*" -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.node" -type d -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -path "*/*.framework/*" -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.framework" -type d -delete
# Apply patches to the modules package.json
if [ -d "$CODESIGNING_FOLDER_PATH"/nodejs-project/node_modules/ ]; then
PATCH_SCRIPT_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-react-native/scripts/ && pwd )"
NODEJS_PROJECT_MODULES_DIR="$( cd "$CODESIGNING_FOLDER_PATH" && cd nodejs-project/node_modules/ && pwd )"
node "$PATCH_SCRIPT_DIR"/patch-package.js $NODEJS_PROJECT_MODULES_DIR
fi
# Get the nodejs-mobile-gyp location
if [ -d "$PROJECT_DIR/../node_modules/nodejs-mobile-gyp/" ]; then
NODEJS_MOBILE_GYP_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-gyp/ && pwd )"
else
NODEJS_MOBILE_GYP_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-react-native/node_modules/nodejs-mobile-gyp/ && pwd )"
fi
NODEJS_MOBILE_GYP_BIN_FILE="$NODEJS_MOBILE_GYP_DIR"/bin/node-gyp.js
# Rebuild modules with right environment
NODEJS_HEADERS_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-react-native/ios/libnode/ && pwd )"
pushd $CODESIGNING_FOLDER_PATH/nodejs-project/
if [ "$PLATFORM_NAME" == "iphoneos" ]
then
GYP_DEFINES="OS=ios" npm_config_nodedir="$NODEJS_HEADERS_DIR" npm_config_node_gyp="$NODEJS_MOBILE_GYP_BIN_FILE" npm_config_platform="ios" npm_config_format="make-ios" npm_config_node_engine="chakracore" npm_config_arch="arm64" npm --verbose rebuild --build-from-source
else
GYP_DEFINES="OS=ios" npm_config_nodedir="$NODEJS_HEADERS_DIR" npm_config_node_gyp="$NODEJS_MOBILE_GYP_BIN_FILE" npm_config_platform="ios" npm_config_format="make-ios" npm_config_node_engine="chakracore" npm_config_arch="x64" npm --verbose rebuild --build-from-source
fi
popd
14 changes: 14 additions & 0 deletions scripts/ios-copy-nodejs-project.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
set -e
NODEJS_ASSETS_DIR="$( cd "$PROJECT_DIR" && cd ../nodejs-assets/ && pwd )"
NODEJS_BUILT_IN_MODULES_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-react-native/install/resources/nodejs-modules/ && pwd )"
if [ -d "$CODESIGNING_FOLDER_PATH/nodejs-project/" ]
then
rm -rf "$CODESIGNING_FOLDER_PATH/nodejs-project/"
fi
if [ -d "$CODESIGNING_FOLDER_PATH/builtin_modules/" ]
then
rm -rf "$CODESIGNING_FOLDER_PATH/builtin_modules/"
fi
rsync -av --delete "$NODEJS_ASSETS_DIR/nodejs-project" "$CODESIGNING_FOLDER_PATH"
rsync -av --delete "$NODEJS_BUILT_IN_MODULES_DIR/builtin_modules" "$CODESIGNING_FOLDER_PATH"
12 changes: 12 additions & 0 deletions scripts/ios-remove-framework-simulator-strips.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh
set -e
FRAMEWORK_BINARY_PATH="$TARGET_BUILD_DIR/$FRAMEWORKS_FOLDER_PATH/NodeMobile.framework/NodeMobile"
FRAMEWORK_STRIPPED_PATH="$FRAMEWORK_BINARY_PATH-strip"
if [ "$PLATFORM_NAME" != "iphonesimulator" ]; then
if $(lipo "$FRAMEWORK_BINARY_PATH" -verify_arch "x86_64") ; then
lipo -output "$FRAMEWORK_STRIPPED_PATH" -remove "x86_64" "$FRAMEWORK_BINARY_PATH"
rm "$FRAMEWORK_BINARY_PATH"
mv "$FRAMEWORK_STRIPPED_PATH" "$FRAMEWORK_BINARY_PATH"
echo "Removed simulator strip from NodeMobile.framework"
fi
fi
45 changes: 45 additions & 0 deletions scripts/ios-sign-native-modules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/sh
set -e
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
# If build native modules preference is not set, look for it in the project's
#nodejs-assets/BUILD_NATIVE_MODULES.txt file.
NODEJS_ASSETS_DIR="$( cd "$PROJECT_DIR" && cd ../nodejs-assets/ && pwd )"
PREFERENCE_FILE_PATH="$NODEJS_ASSETS_DIR/BUILD_NATIVE_MODULES.txt"
if [ -f "$PREFERENCE_FILE_PATH" ]; then
NODEJS_MOBILE_BUILD_NATIVE_MODULES="$(cat $PREFERENCE_FILE_PATH | xargs)"
fi
fi
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
# If build native modules preference is not set, try to find .gyp files
#to turn it on.
gypfiles=($(find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -type f -name "*.gyp"))
if [ ${#gypfiles[@]} -gt 0 ]; then
NODEJS_MOBILE_BUILD_NATIVE_MODULES=1
else
NODEJS_MOBILE_BUILD_NATIVE_MODULES=0
fi
fi
if [ "1" != "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then exit 0; fi
# Delete object files
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.o" -type f -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.a" -type f -delete
# Create Info.plist for each framework built and loader override.
PATCH_SCRIPT_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-react-native/scripts/ && pwd )"
NODEJS_PROJECT_DIR="$( cd "$CODESIGNING_FOLDER_PATH" && cd nodejs-project/ && pwd )"
node "$PATCH_SCRIPT_DIR"/ios-create-plists-and-dlopen-override.js $NODEJS_PROJECT_DIR
# Embed every resulting .framework in the application and delete them afterwards.
embed_framework()
{
FRAMEWORK_NAME="$(basename "$1")"
cp -r "$1" "$TARGET_BUILD_DIR/$FRAMEWORKS_FOLDER_PATH/"
/usr/bin/codesign --force --sign $EXPANDED_CODE_SIGN_IDENTITY --preserve-metadata=identifier,entitlements,flags --timestamp=none "$TARGET_BUILD_DIR/$FRAMEWORKS_FOLDER_PATH/$FRAMEWORK_NAME"
}
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.framework" -type d | while read frmwrk_path; do embed_framework "$frmwrk_path"; done

#Delete gyp temporary .deps dependency folders from the project structure.
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -path "*/.deps/*" -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name ".deps" -type d -delete

#Delete frameworks from their build paths
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -path "*/*.framework/*" -delete
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.framework" -type d -delete
Loading

0 comments on commit 7cd4a01

Please sign in to comment.