Skip to content

Commit 1adcc47

Browse files
committed
expo: initialize batch module from main application
1 parent 571b292 commit 1adcc47

File tree

4 files changed

+3192
-4783
lines changed

4 files changed

+3192
-4783
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"eslint": "^8.16.0",
4444
"eslint-plugin-prettier": "^4.0.0",
4545
"eslint-plugin-simple-import-sort": "^7.0.0",
46-
"expo-module-scripts": "^2.0.0",
46+
"expo-module-scripts": "^3.5.2",
4747
"jest": "^27.3.1",
4848
"prettier": "^1.14.2",
4949
"react-native": "^0.66.1",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { ConfigPlugin, withMainApplication } from '@expo/config-plugins';
2+
3+
export const modifyMainJavaApplication = (content: string): string => {
4+
let newContent = content;
5+
if (newContent.includes('import com.facebook.react.PackageList;')) {
6+
newContent = content.replace(
7+
'import com.facebook.react.PackageList;',
8+
`import com.batch.batch_rn.RNBatchModule;
9+
import com.facebook.react.PackageList;`
10+
);
11+
}
12+
if (newContent.includes('ApplicationLifecycleDispatcher.onApplicationCreate(this)')) {
13+
newContent = newContent.replace(
14+
'ApplicationLifecycleDispatcher.onApplicationCreate(this);',
15+
`ApplicationLifecycleDispatcher.onApplicationCreate(this);
16+
RNBatchModule.initialize(this);`
17+
);
18+
}
19+
return newContent;
20+
};
21+
22+
export const modifyMainKotlinApplication = (content: string): string => {
23+
let newContent = content;
24+
if (newContent.includes('import com.facebook.react.PackageList')) {
25+
newContent = content.replace(
26+
'import com.facebook.react.PackageList',
27+
`import com.batch.batch_rn.RNBatchModule
28+
import com.facebook.react.PackageList`
29+
);
30+
}
31+
if (newContent.includes('ApplicationLifecycleDispatcher.onApplicationCreate(this)')) {
32+
newContent = newContent.replace(
33+
'ApplicationLifecycleDispatcher.onApplicationCreate(this)',
34+
`ApplicationLifecycleDispatcher.onApplicationCreate(this)
35+
RNBatchModule.initialize(this)`
36+
);
37+
}
38+
return newContent;
39+
};
40+
41+
export const modifyMainApplication = (content: string): string => {
42+
return isKotlinMainApplication(content) ? modifyMainKotlinApplication(content) : modifyMainJavaApplication(content);
43+
};
44+
45+
const isKotlinMainApplication = (content: string): boolean => {
46+
return content.includes('class MainApplication : Application(), ReactApplication');
47+
};
48+
49+
export const withReactNativeBatchMainApplication: ConfigPlugin<object | void> = config => {
50+
return withMainApplication(config, config => {
51+
return {
52+
...config,
53+
modResults: {
54+
...config.modResults,
55+
contents: modifyMainApplication(config.modResults.contents),
56+
},
57+
};
58+
});
59+
};

plugin/src/withReactNativeBatch.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { withClassPath, withApplyPlugin, withGoogleServicesFile } from '@expo/co
33

44
import { withReactNativeBatchAppBuildGradle } from './android/withReactNativeBatchAppBuildGradle';
55
import { withReactNativeBatchMainActivity } from './android/withReactNativeBatchMainActivity';
6+
import { withReactNativeBatchMainApplication } from './android/withReactNativeBatchMainApplication';
67
import { withReactNativeBatchManifest } from './android/withReactNativeBatchManifest';
78
import { withReactNativeBatchAppDelegate } from './ios/withReactNativeBatchAppDelegate';
89
import { withReactNativeBatchInfoPlist } from './ios/withReactNativeBatchInfoPlist';
@@ -26,6 +27,7 @@ const withReactNativeBatch: ConfigPlugin<Props | void> = (config, props) => {
2627
newConfig = withApplyPlugin(newConfig);
2728
newConfig = withReactNativeBatchManifest(newConfig, _props);
2829
newConfig = withReactNativeBatchAppBuildGradle(newConfig, _props);
30+
newConfig = withReactNativeBatchMainApplication(newConfig);
2931
newConfig = withReactNativeBatchMainActivity(newConfig);
3032
newConfig = withReactNativeBatchInfoPlist(newConfig, _props);
3133
newConfig = withReactNativeBatchAppDelegate(newConfig);

0 commit comments

Comments
 (0)