Skip to content

Commit ab5fd00

Browse files
giautmEvanBacon
andauthored
feat: create expo config plugin (#313)
* feat: create expo config plugin * chore: update README * chore: remove duplicated section * chore: update README * fix: apply suggestions from code review Co-authored-by: Evan Bacon <baconbrix@gmail.com> * fix: fixed plugin imports Co-authored-by: Evan Bacon <baconbrix@gmail.com>
1 parent d1f8cc8 commit ab5fd00

6 files changed

Lines changed: 8431 additions & 304 deletions

File tree

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
You must have Facebook developer account in order to start integrating your app with this library. If you don't have one sign up [here](https://developers.facebook.com/).
3030

31-
Follow the instructions on [react-native-fbsdk](https://github.com/facebook/react-native-fbsdk) to integrate the **Facebook SDK** into your project.
31+
Follow the instructions on [react-native-fbsdk-next](https://github.com/thebergamo/react-native-fbsdk-next) to integrate the **Facebook SDK** into your project.
3232

3333
### Get a Placement ID
3434

@@ -81,6 +81,42 @@ $ react-native link react-native-fbads
8181

8282
</details>
8383

84+
## Expo installation
85+
86+
> This package cannot be used in the "Expo Go" app because [it requires custom native code](https://docs.expo.io/workflow/customizing/).
87+
88+
> First install the package with yarn, npm, or [`expo install`](https://docs.expo.io/workflow/expo-cli/#expo-install).
89+
90+
```sh
91+
expo install react-native-fbsdk-next react-native-fbads
92+
```
93+
94+
After installing this npm package, add the [config plugin](https://docs.expo.io/guides/config-plugins/) to the [`plugins`](https://docs.expo.io/versions/latest/config/app/#plugins) array of your `app.json` or `app.config.js`:
95+
96+
```json
97+
{
98+
"expo": {
99+
"plugins": [
100+
[
101+
"react-native-fbsdk-next",
102+
{
103+
"appID": "48127127xxxxxxxx",
104+
"clientToken": "c5078631e4065b60d7544a95xxxxxxxx",
105+
"displayName": "RN SDK Demo",
106+
"advertiserIDCollectionEnabled": false,
107+
"autoLogAppEventsEnabled": false,
108+
"isAutoInitEnabled": true,
109+
"iosUserTrackingPermission": "This identifier will be used to deliver personalized ads to you."
110+
}
111+
],
112+
"react-native-fbads"
113+
]
114+
}
115+
}
116+
```
117+
118+
Next, rebuild your app as described in the ["Adding custom native code"](https://docs.expo.io/workflow/customizing/) guide.
119+
84120
## Usage
85121

86122
### Interstitial Ads

app.plugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require("./plugin/build/withReactNativeFbads");

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
"example": "example"
1414
},
1515
"scripts": {
16+
"build:plugin": "tsc --build plugin",
17+
"clean:plugin": "expo-module clean plugin",
18+
"lint:plugin": "eslint plugin/src/*",
19+
"prepare": "npm run build:plugin",
1620
"tsc": "tsc --project .",
1721
"lint": "tslint --project ."
1822
},
@@ -22,11 +26,13 @@
2226
"react-native": "*"
2327
},
2428
"dependencies": {
29+
"@expo/config-plugins": "^4.0.4",
2530
"fbemitter": "^2.1.1"
2631
},
2732
"devDependencies": {
2833
"@types/fbemitter": "^2.0.32",
2934
"@types/react-native": "^0.57.4",
35+
"expo-module-scripts": "^2.0.0",
3036
"tslint": "^5.11.0",
3137
"tslint-config-airbnb": "^5.11.0",
3238
"tslint-config-prettier": "^1.15.0",
@@ -46,6 +52,8 @@
4652
"package.json",
4753
"dist",
4854
"react-native.config.js",
49-
"ReactNativeAdsFacebook.podspec"
55+
"ReactNativeAdsFacebook.podspec",
56+
"app.plugin.js",
57+
"plugin/build/"
5058
]
5159
}

plugin/src/withReactNativeFbads.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import {
2+
AndroidConfig,
3+
ConfigPlugin,
4+
createRunOncePlugin,
5+
withAndroidManifest,
6+
} from '@expo/config-plugins';
7+
const { getMainApplicationOrThrow, prefixAndroidKeys } = AndroidConfig.Manifest;
8+
9+
const INTERSTITIAL_AD_ACTIVITY = 'com.facebook.ads.InterstitialAdActivity';
10+
11+
export const withFacebookManifest: ConfigPlugin = (config) => {
12+
return withAndroidManifest(config, (config) => {
13+
config.modResults = setFacebookConfig(config.modResults);
14+
return config;
15+
});
16+
};
17+
18+
export function setFacebookConfig(
19+
androidManifest: AndroidConfig.Manifest.AndroidManifest
20+
) {
21+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
22+
let mainApplication = getMainApplicationOrThrow(androidManifest);
23+
mainApplication = ensureFacebookActivity({ mainApplication });
24+
25+
return androidManifest;
26+
}
27+
28+
function ensureFacebookActivity({
29+
mainApplication,
30+
}: {
31+
mainApplication: AndroidConfig.Manifest.ManifestApplication;
32+
}) {
33+
if (Array.isArray(mainApplication.activity)) {
34+
// Remove all Facebook InterstitialAdActivity first
35+
mainApplication.activity = mainApplication.activity.filter((activity) => {
36+
return activity.$?.['android:name'] !== INTERSTITIAL_AD_ACTIVITY;
37+
});
38+
} else {
39+
mainApplication.activity = [];
40+
}
41+
42+
mainApplication.activity.push(getFacebookAdActivity());
43+
return mainApplication;
44+
}
45+
46+
function buildXMLItem({
47+
head,
48+
children,
49+
}: {
50+
head: Record<string, string>;
51+
children?: Record<string, string | any[]>;
52+
}) {
53+
return { ...(children ?? {}), $: head };
54+
}
55+
56+
function getFacebookAdActivity() {
57+
/**
58+
<activity
59+
android:name="com.facebook.ads.InterstitialAdActivity"
60+
android:configChanges="keyboardHidden|orientation"
61+
/>
62+
*/
63+
return buildXMLItem({
64+
head: prefixAndroidKeys({
65+
name: INTERSTITIAL_AD_ACTIVITY,
66+
configChanges: 'keyboardHidden|orientation',
67+
}),
68+
}) as AndroidConfig.Manifest.ManifestActivity;
69+
}
70+
71+
/**
72+
* Apply react-native-fbads configuration for Expo SDK 44 projects.
73+
*/
74+
const withReactNativeFbads: ConfigPlugin = (config) => {
75+
return withFacebookManifest(config);
76+
};
77+
78+
const pkg = require('react-native-fbads/package.json');
79+
80+
export default createRunOncePlugin(withReactNativeFbads, pkg.name, pkg.version);

plugin/tsconfig.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "expo-module-scripts/tsconfig.plugin",
3+
"compilerOptions": {
4+
"outDir": "./build"
5+
},
6+
"include": ["./src"],
7+
"exclude": ["**/__mocks__/*", "**/__tests__/*"]
8+
}

0 commit comments

Comments
 (0)