Skip to content

Commit 7cd4a01

Browse files
plugin: support react-native 0.60+
1 parent dd7c67a commit 7cd4a01

9 files changed

+191
-385
lines changed

README.md

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ We have a [central repo](https://github.com/janeasystems/nodejs-mobile/issues) w
99

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

12-
`$ react-native link nodejs-mobile-react-native`
12+
For iOS, run `pod install` for linking the native code parts:
13+
14+
`$ cd iOS && pod install`
1315

1416
### iOS
1517

@@ -307,39 +309,24 @@ react-native run-android
307309
### Duplicate module name
308310

309311
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`.
310-
The `react-native` packager monitors the project's folder for javascript packages and may throw a "`Error: jest-haste-map: @providesModule naming collision`" error.
312+
The `react-native` packager monitors the project's folder for javascript packages and may throw a "`jest-haste-map: Haste module naming collision`" error.
311313

312-
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`):
314+
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:
313315

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

317319
module.exports = {
318-
resolver:{
320+
resolver: {
319321
blacklistRE: blacklist([
320-
/nodejs-assets\/.*/,
321-
/android\/.*/,
322-
/ios\/.*/
322+
/\/nodejs-assets\/.*/,
323+
/\/android\/.*/,
324+
/\/ios\/.*/
323325
])
324326
},
325-
};
326-
327-
```
328327

329-
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.
328+
...
330329

331-
These are the contents of `rn-cli.config.js` if `react-native < v0.57`:
332-
```js
333-
const blacklist = require('metro/src/blacklist');
334-
335-
module.exports = {
336-
getBlacklistRE: function() {
337-
return blacklist([
338-
/nodejs-assets\/.*/,
339-
/android\/.*/,
340-
/ios\/.*/
341-
]);
342-
},
343330
};
344331
```
345332

nodejs-mobile-react-native.podspec

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'json'
2+
3+
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
4+
5+
Pod::Spec.new do |s|
6+
s.name = package['name']
7+
s.version = package['version']
8+
s.summary = package['description']
9+
s.license = package['license']
10+
s.source = { :git => 'https://github.com/JaneaSystems/nodejs-mobile-react-native.git', :tag => 'v#{s.version}' }
11+
s.authors = package['author']
12+
s.homepage = package['homepage']
13+
s.platform = :ios, '9.0'
14+
s.source_files = 'ios/*.{h,m,mm,hpp,cpp}'
15+
s.compiler_flags = '-I$(PODS_TARGET_SRCROOT)/ios/libnode/include/node/'
16+
s.pod_target_xcconfig = {
17+
'CLANG_CXX_LANGUAGE_STANDARD' => 'gnu++0x',
18+
'ENABLE_BITCODE' => 'NO'
19+
}
20+
s.user_target_xcconfig = { 'ENABLE_BITCODE' => 'NO' }
21+
s.ios.vendored_frameworks = 'ios/NodeMobile.framework'
22+
s.static_framework = true
23+
s.dependency 'React'
24+
end

package.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,12 @@
2424
"url": "https://github.com/janeasystems/nodejs-mobile/issues"
2525
},
2626
"peerDependencies": {
27-
"react-native": ">=0.52.0"
27+
"react-native": ">=0.60.0"
2828
},
2929
"dependencies": {
3030
"mkdirp": "^0.5.1",
3131
"ncp": "^2.0.0",
3232
"nodejs-mobile-gyp": "^0.3.1",
3333
"xcode": "^2.0.0"
34-
},
35-
"rnpm": {
36-
"commands": {
37-
"postlink": "node node_modules/nodejs-mobile-react-native/scripts/module-postlink.js"
38-
}
3934
}
4035
}

react-native.config.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = {
2+
dependency: {
3+
platforms: {
4+
android: {
5+
sourceDir: './android'
6+
},
7+
ios: {
8+
project: './ios/RNNodeJsMobile.xcodeproj',
9+
scriptPhases: [
10+
{
11+
name: '[NODEJS MOBILE] Copy Node.js Project files',
12+
path: './scripts/ios-copy-nodejs-project.sh',
13+
execution_position: 'after_compile'
14+
}, {
15+
name: '[NODEJS MOBILE] Build Native Modules',
16+
path: './scripts/ios-build-native-modules.sh',
17+
execution_position: 'after_compile'
18+
}, {
19+
name: '[NODEJS MOBILE] Sign Native Modules',
20+
path: './scripts/ios-sign-native-modules.sh',
21+
execution_position: 'after_compile'
22+
}, {
23+
name: '[NODEJS MOBILE] Remove Simulator Strip',
24+
path: './scripts/ios-remove-framework-simulator-strips.sh',
25+
execution_position: 'after_compile'
26+
}
27+
]
28+
}
29+
}
30+
}
31+
}

scripts/ios-build-native-modules.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/sh
2+
set -e
3+
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
4+
# If build native modules preference is not set, look for it in the project's
5+
#nodejs-assets/BUILD_NATIVE_MODULES.txt file.
6+
NODEJS_ASSETS_DIR="$( cd "$PROJECT_DIR" && cd ../nodejs-assets/ && pwd )"
7+
PREFERENCE_FILE_PATH="$NODEJS_ASSETS_DIR/BUILD_NATIVE_MODULES.txt"
8+
if [ -f "$PREFERENCE_FILE_PATH" ]; then
9+
NODEJS_MOBILE_BUILD_NATIVE_MODULES="$(cat $PREFERENCE_FILE_PATH | xargs)"
10+
fi
11+
fi
12+
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
13+
# If build native modules preference is not set, try to find .gyp files
14+
#to turn it on.
15+
gypfiles=($(find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -type f -name "*.gyp"))
16+
if [ ${#gypfiles[@]} -gt 0 ]; then
17+
NODEJS_MOBILE_BUILD_NATIVE_MODULES=1
18+
else
19+
NODEJS_MOBILE_BUILD_NATIVE_MODULES=0
20+
fi
21+
fi
22+
if [ "1" != "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then exit 0; fi
23+
# Delete object files that may already come from within the npm package.
24+
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.o" -type f -delete
25+
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.a" -type f -delete
26+
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.node" -type f -delete
27+
# Delete bundle contents that may be there from previous builds.
28+
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -path "*/*.node/*" -delete
29+
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.node" -type d -delete
30+
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -path "*/*.framework/*" -delete
31+
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.framework" -type d -delete
32+
# Apply patches to the modules package.json
33+
if [ -d "$CODESIGNING_FOLDER_PATH"/nodejs-project/node_modules/ ]; then
34+
PATCH_SCRIPT_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-react-native/scripts/ && pwd )"
35+
NODEJS_PROJECT_MODULES_DIR="$( cd "$CODESIGNING_FOLDER_PATH" && cd nodejs-project/node_modules/ && pwd )"
36+
node "$PATCH_SCRIPT_DIR"/patch-package.js $NODEJS_PROJECT_MODULES_DIR
37+
fi
38+
# Get the nodejs-mobile-gyp location
39+
if [ -d "$PROJECT_DIR/../node_modules/nodejs-mobile-gyp/" ]; then
40+
NODEJS_MOBILE_GYP_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-gyp/ && pwd )"
41+
else
42+
NODEJS_MOBILE_GYP_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-react-native/node_modules/nodejs-mobile-gyp/ && pwd )"
43+
fi
44+
NODEJS_MOBILE_GYP_BIN_FILE="$NODEJS_MOBILE_GYP_DIR"/bin/node-gyp.js
45+
# Rebuild modules with right environment
46+
NODEJS_HEADERS_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-react-native/ios/libnode/ && pwd )"
47+
pushd $CODESIGNING_FOLDER_PATH/nodejs-project/
48+
if [ "$PLATFORM_NAME" == "iphoneos" ]
49+
then
50+
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
51+
else
52+
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
53+
fi
54+
popd

scripts/ios-copy-nodejs-project.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
set -e
3+
NODEJS_ASSETS_DIR="$( cd "$PROJECT_DIR" && cd ../nodejs-assets/ && pwd )"
4+
NODEJS_BUILT_IN_MODULES_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-react-native/install/resources/nodejs-modules/ && pwd )"
5+
if [ -d "$CODESIGNING_FOLDER_PATH/nodejs-project/" ]
6+
then
7+
rm -rf "$CODESIGNING_FOLDER_PATH/nodejs-project/"
8+
fi
9+
if [ -d "$CODESIGNING_FOLDER_PATH/builtin_modules/" ]
10+
then
11+
rm -rf "$CODESIGNING_FOLDER_PATH/builtin_modules/"
12+
fi
13+
rsync -av --delete "$NODEJS_ASSETS_DIR/nodejs-project" "$CODESIGNING_FOLDER_PATH"
14+
rsync -av --delete "$NODEJS_BUILT_IN_MODULES_DIR/builtin_modules" "$CODESIGNING_FOLDER_PATH"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
set -e
3+
FRAMEWORK_BINARY_PATH="$TARGET_BUILD_DIR/$FRAMEWORKS_FOLDER_PATH/NodeMobile.framework/NodeMobile"
4+
FRAMEWORK_STRIPPED_PATH="$FRAMEWORK_BINARY_PATH-strip"
5+
if [ "$PLATFORM_NAME" != "iphonesimulator" ]; then
6+
if $(lipo "$FRAMEWORK_BINARY_PATH" -verify_arch "x86_64") ; then
7+
lipo -output "$FRAMEWORK_STRIPPED_PATH" -remove "x86_64" "$FRAMEWORK_BINARY_PATH"
8+
rm "$FRAMEWORK_BINARY_PATH"
9+
mv "$FRAMEWORK_STRIPPED_PATH" "$FRAMEWORK_BINARY_PATH"
10+
echo "Removed simulator strip from NodeMobile.framework"
11+
fi
12+
fi

scripts/ios-sign-native-modules.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/sh
2+
set -e
3+
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
4+
# If build native modules preference is not set, look for it in the project's
5+
#nodejs-assets/BUILD_NATIVE_MODULES.txt file.
6+
NODEJS_ASSETS_DIR="$( cd "$PROJECT_DIR" && cd ../nodejs-assets/ && pwd )"
7+
PREFERENCE_FILE_PATH="$NODEJS_ASSETS_DIR/BUILD_NATIVE_MODULES.txt"
8+
if [ -f "$PREFERENCE_FILE_PATH" ]; then
9+
NODEJS_MOBILE_BUILD_NATIVE_MODULES="$(cat $PREFERENCE_FILE_PATH | xargs)"
10+
fi
11+
fi
12+
if [ -z "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then
13+
# If build native modules preference is not set, try to find .gyp files
14+
#to turn it on.
15+
gypfiles=($(find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -type f -name "*.gyp"))
16+
if [ ${#gypfiles[@]} -gt 0 ]; then
17+
NODEJS_MOBILE_BUILD_NATIVE_MODULES=1
18+
else
19+
NODEJS_MOBILE_BUILD_NATIVE_MODULES=0
20+
fi
21+
fi
22+
if [ "1" != "$NODEJS_MOBILE_BUILD_NATIVE_MODULES" ]; then exit 0; fi
23+
# Delete object files
24+
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.o" -type f -delete
25+
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.a" -type f -delete
26+
# Create Info.plist for each framework built and loader override.
27+
PATCH_SCRIPT_DIR="$( cd "$PROJECT_DIR" && cd ../node_modules/nodejs-mobile-react-native/scripts/ && pwd )"
28+
NODEJS_PROJECT_DIR="$( cd "$CODESIGNING_FOLDER_PATH" && cd nodejs-project/ && pwd )"
29+
node "$PATCH_SCRIPT_DIR"/ios-create-plists-and-dlopen-override.js $NODEJS_PROJECT_DIR
30+
# Embed every resulting .framework in the application and delete them afterwards.
31+
embed_framework()
32+
{
33+
FRAMEWORK_NAME="$(basename "$1")"
34+
cp -r "$1" "$TARGET_BUILD_DIR/$FRAMEWORKS_FOLDER_PATH/"
35+
/usr/bin/codesign --force --sign $EXPANDED_CODE_SIGN_IDENTITY --preserve-metadata=identifier,entitlements,flags --timestamp=none "$TARGET_BUILD_DIR/$FRAMEWORKS_FOLDER_PATH/$FRAMEWORK_NAME"
36+
}
37+
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.framework" -type d | while read frmwrk_path; do embed_framework "$frmwrk_path"; done
38+
39+
#Delete gyp temporary .deps dependency folders from the project structure.
40+
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -path "*/.deps/*" -delete
41+
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name ".deps" -type d -delete
42+
43+
#Delete frameworks from their build paths
44+
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -path "*/*.framework/*" -delete
45+
find "$CODESIGNING_FOLDER_PATH/nodejs-project/" -name "*.framework" -type d -delete

0 commit comments

Comments
 (0)