Skip to content

Commit

Permalink
[js/rn] add expo config plugin support (microsoft#11556)
Browse files Browse the repository at this point in the history
* [js/rn] add expo config plugin support

* resolve comments
  • Loading branch information
fs-eire authored May 25, 2022
1 parent d03d7af commit f0dff6b
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 5 deletions.
53 changes: 53 additions & 0 deletions js/react_native/app.plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const configPlugin = require('@expo/config-plugins');
const generateCode = require('@expo/config-plugins/build/utils/generateCode');
const pkg = require('onnxruntime-react-native/package.json');
const path = require('path');
const fs = require('fs');

const withOrt = (config) => {
// Add build dependency to gradle file
config = configPlugin.withAppBuildGradle(config, (config) => {
if (config.modResults.language === 'groovy') {
config.modResults.contents = generateCode
.mergeContents({
src: config.modResults.contents,
newSrc: ' implementation project(\':onnxruntime-react-native\')',
tag: 'onnxruntime-react-native',
anchor: /^dependencies[ \t]*\{$/,
offset: 1,
comment: ' // onnxruntime-react-native'
})
.contents;
} else {
throw new Error('Cannot add ONNX Runtime maven gradle because the build.gradle is not groovy');
}

return config;
});

// Add build dependency to pod file
config = configPlugin.withDangerousMod(config, [
'ios',
(config) => {
const podFilePath = path.join(config.modRequest.platformProjectRoot, 'PodFile');
const contents = fs.readFileSync(podFilePath, {encoding: 'utf-8'});
const updatedContents =
generateCode
.mergeContents({
src: contents,
newSrc: ' pod \'onnxruntime-react-native\', :path => \'../node_modules/onnxruntime-react-native\'',
tag: 'onnxruntime-react-native',
anchor: /^target.+do$/,
offset: 1,
comment: ' # onnxruntime-react-native'
})
.contents;
fs.writeFileSync(podFilePath, updatedContents);
return config;
}
]);

return config;
};

exports.default = configPlugin.createRunOncePlugin(withOrt, pkg.name, pkg.version)
3 changes: 3 additions & 0 deletions js/react_native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
"ios/*.h",
"ios/*.mm",
"onnxruntime-react-native.podspec",
"app.plugin.js",
"unimodule.json",
"!dist/commonjs/*.js.map",
"!dist/module/*.js.map",
"!android/.gitignore",
Expand Down Expand Up @@ -75,6 +77,7 @@
"output": "dist"
},
"dependencies": {
"@expo/config-plugins": "^4.1.5",
"buffer": "^6.0.3",
"onnxruntime-common": "file:../common"
},
Expand Down
4 changes: 4 additions & 0 deletions js/react_native/unimodule.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "onnxruntime-react-native",
"platforms": ["ios", "android"]
}
Loading

0 comments on commit f0dff6b

Please sign in to comment.