Skip to content

Commit c7df1fd

Browse files
authored
feat: Do not ship Mapeo for ICCAs assets with Mapeo (#447)
* feat: Create ICCAs build scripts & exclude ICCA screens from main build Use Metro build config to conditionally include ICCA screens so that ICCA resouces are not included in the main Mapeo build. This avoids the images used in the ICCAs intro screens increasing the file size of the main Mapeo APK. * feat: Ship apk variants with only the presets they need Choose the presets that are included in the apk at build time instead of runtime. This reduces the apk size and removes runtime code for conditionally running code based on app variant. * chore: Patch react-native so gradle tasks package ICCA variant Adds the app variant name as an environment variable in the gradle build script so that metro bundler can package JS files according to variant by using the `sourceExts` option.
1 parent 08fe2d6 commit c7df1fd

File tree

16 files changed

+259
-141
lines changed

16 files changed

+259
-141
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,8 @@ buck-out/
7272
# Private env variables
7373
.env
7474
.bitrise.secrets.yml
75+
76+
# nodejs-mobile assets generated by build script
77+
# TODO: somehow modify gradle build scripts so that these can be stored in
78+
# a build folder rather than in these app/src folders
79+
/android/app/src/*/assets/nodejs-assets

CONTRIBUTING.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,22 @@ alongside existing copies of the app on their phone. It has a red logo.
145145
This is the main variant of the app for our partners and the public. It has a
146146
dark blue logo and Application ID `com.mapeo`.
147147

148+
### Mapeo for ICCAs Variant
149+
150+
This is a special variant created for the WCMC-UNEP for using Mapeo to map ICCAs. To build the Mapeo for ICCAs Variant, run:
151+
152+
```sh
153+
npm run build:release-icca
154+
```
155+
156+
To develop with the debug build of Mapeo for ICCAs:
157+
158+
```sh
159+
npm run start-icca
160+
# In another tab
161+
npm run android-icca
162+
```
163+
148164
## Releases & Builds
149165

150166
Mapeo is built using the CI service
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">Mapeo for ICCAs</string>
3+
</resources>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">Mapeo ICCAs Dev</string>
3+
</resources>

metro.config.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,35 @@
55
* @format
66
*/
77
const blacklist = require("metro-config/src/defaults/blacklist");
8+
const defaultSourceExts = require("metro-config/src/defaults/defaults")
9+
.sourceExts;
10+
11+
const customSourceExts = [];
12+
13+
if (process.env.APP_VARIANT) {
14+
const match = process.env.APP_VARIANT.match(/^[^A-Z]*/);
15+
if (match) {
16+
customSourceExts.push(match[0] + ".js");
17+
}
18+
}
19+
20+
if (process.env.RN_SRC_EXT) {
21+
process.env.RN_SRC_EXT.split(",").forEach(ext => {
22+
customSourceExts.push(ext);
23+
});
24+
}
825

926
module.exports = {
1027
transformer: {
1128
getTransformOptions: async () => ({
1229
transform: {
1330
experimentalImportSupport: false,
14-
inlineRequires: false
15-
}
16-
})
31+
inlineRequires: false,
32+
},
33+
}),
1734
},
1835
resolver: {
19-
blacklistRE: blacklist([/nodejs-assets\/.*/, /android\/.*/, /ios\/.*/])
20-
}
36+
blacklistRE: blacklist([/nodejs-assets\/.*/, /android\/.*/, /ios\/.*/]),
37+
sourceExts: customSourceExts.concat(defaultSourceExts),
38+
},
2139
};

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
},
88
"scripts": {
99
"start": "react-native start",
10+
"start-icca": "RN_SRC_EXT=icca.js react-native start",
1011
"android": "npm run build:backend && npm run android-no-backend-rebuild",
1112
"android-no-backend-rebuild": "react-native run-android --variant=appDebug --appIdSuffix=debug",
1213
"android-storybook": "react-native run-android --variant=storybookDebug --appIdSuffix=storybook.debug",
13-
"android-icca": "react-native run-android --variant=iccaDebug --appIdSuffix=debug",
14+
"android-icca": "RN_SRC_EXT=icca.js react-native run-android --variant=iccaDebug --appIdSuffix=icca.debug",
1415
"build:backend": "./scripts/build-backend.sh",
1516
"build:translations": "node ./scripts/build-translations.js",
1617
"build:release": "npm run build:translations && npm run build:backend && ./scripts/build-release-android.sh",
18+
"build:release-icca": "npm run build:translations && npm run build:backend && RN_SRC_EXT=icca.js ./scripts/build-release-android.sh",
1719
"build:storybook": "./node_modules/@storybook/react/bin/build.js -c ./storybook-web -s ./public -o ./build --no-dll",
1820
"test": "jest",
1921
"lint": "eslint *.js \"src/**/*.js\"",

patches/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,20 @@
22

33
These patches use [`patch-package`](https://www.npmjs.com/package/patch-package)
44
to update dependencies which have unpublished fixes.
5+
6+
## `nodejs-mobile-react-native`
7+
8+
This patch additionally extracts files in the folder `assets/nodejs-assets` so
9+
that they can be accessed by the nodejs process. This is necessary so that
10+
different variants (Mapeo for ICCAs vs normal Mapeo) can each ship their own
11+
assets (e.g. presets)
12+
13+
## `react-native`
14+
15+
This patch adds an environment variable `APP_VARIANT` with the app variant name
16+
when calling `react-native bundle` in the react gradle script. This is used to
17+
define custom `sourceExts` for Metro bundler, so we can package specific code
18+
based on application variant. It is necessary to patch this file rather than
19+
just add an environment variable as part of the build script because it allows
20+
gradle tasks like `./gradlew assembleRelease` to work as expected — each variant
21+
will be built correctly with the correct bundled files.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
diff --git a/node_modules/nodejs-mobile-react-native/android/src/main/java/com/janeasystems/rn_nodejs_mobile/RNNodeJsMobileModule.java b/node_modules/nodejs-mobile-react-native/android/src/main/java/com/janeasystems/rn_nodejs_mobile/RNNodeJsMobileModule.java
2+
index e882a0c..02616d0 100644
3+
--- a/node_modules/nodejs-mobile-react-native/android/src/main/java/com/janeasystems/rn_nodejs_mobile/RNNodeJsMobileModule.java
4+
+++ b/node_modules/nodejs-mobile-react-native/android/src/main/java/com/janeasystems/rn_nodejs_mobile/RNNodeJsMobileModule.java
5+
@@ -32,6 +32,7 @@ public class RNNodeJsMobileModule extends ReactContextBaseJavaModule implements
6+
private final ReactApplicationContext reactContext;
7+
private static final String TAG = "NODEJS-RN";
8+
private static final String NODEJS_PROJECT_DIR = "nodejs-project";
9+
+ private static final String NODEJS_ASSETS_DIR = "nodejs-assets";
10+
private static final String NODEJS_BUILTIN_MODULES = "nodejs-builtin_modules";
11+
private static final String TRASH_DIR = "nodejs-project-trash";
12+
private static final String SHARED_PREFS = "NODEJS_MOBILE_PREFS";
13+
@@ -42,6 +43,7 @@ public class RNNodeJsMobileModule extends ReactContextBaseJavaModule implements
14+
private static String trashDirPath;
15+
private static String filesDirPath;
16+
private static String nodeJsProjectPath;
17+
+ private static String nodeJsAssetsPath;
18+
private static String builtinModulesPath;
19+
private static String nativeAssetsPath;
20+
21+
@@ -74,6 +76,7 @@ public class RNNodeJsMobileModule extends ReactContextBaseJavaModule implements
22+
23+
// The paths where we expect the node project assets to be at runtime.
24+
nodeJsProjectPath = filesDirPath + "/" + NODEJS_PROJECT_DIR;
25+
+ nodeJsAssetsPath = filesDirPath + "/" + NODEJS_ASSETS_DIR;
26+
builtinModulesPath = filesDirPath + "/" + NODEJS_BUILTIN_MODULES;
27+
trashDirPath = filesDirPath + "/" + TRASH_DIR;
28+
nativeAssetsPath = BUILTIN_NATIVE_ASSETS_PREFIX + getCurrentABIName();
29+
@@ -387,6 +390,9 @@ public class RNNodeJsMobileModule extends ReactContextBaseJavaModule implements
30+
// Copy the nodejs built-in modules to the application's data path.
31+
copyAssetFolder("builtin_modules", builtinModulesPath);
32+
33+
+ // Copy nodejs assets (e.g. presets) which can vary between variants
34+
+ copyAssetFolder("nodejs-assets", nodeJsAssetsPath);
35+
+
36+
saveLastUpdateTime();
37+
Log.d(TAG, "Node assets copy completed successfully");
38+
}

patches/react-native+0.62.2.patch

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
diff --git a/node_modules/react-native/react.gradle b/node_modules/react-native/react.gradle
2+
index d886a9b..444c345 100644
3+
--- a/node_modules/react-native/react.gradle
4+
+++ b/node_modules/react-native/react.gradle
5+
@@ -153,6 +153,8 @@ afterEvaluate {
6+
extraArgs.add(bundleConfig);
7+
}
8+
9+
+ environment("APP_VARIANT", variant.name);
10+
+
11+
commandLine(*execCommand, bundleCommand, "--platform", "android", "--dev", "${devEnabled}",
12+
"--reset-cache", "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir,
13+
"--sourcemap-output", enableHermes ? jsPackagerSourceMapFile : jsOutputSourceMapFile, *extraArgs)

scripts/build-backend.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,18 @@ cd ../..
4242
echo -en " done.\n"
4343

4444
echo -en "Create presets fallback folders..."
45+
ANDROID_SRC_DIR="$(pwd)/android/app/src"
4546
cd ./nodejs-assets/nodejs-project
46-
mkdir -p presets
47-
mkdir -p presets-icca
48-
mv ./node_modules/mapeo-default-settings/dist ./presets/default
49-
mv ./node_modules/mapeo-config-icca/dist ./presets-icca/default
47+
# We use a patched version of nodejs-mobile-react-native that extracts files in
48+
# the assets/nodejs-assets folder, so that they are accessible to Node. Here we
49+
# copy default presets into assets folders by variant, so that the icca variant
50+
# has its own custom presets shipped with the app.
51+
rm -rf "${ANDROID_SRC_DIR}/main/assets/nodejs-assets/presets"
52+
rm -rf "${ANDROID_SRC_DIR}/icca/assets/nodejs-assets/presets"
53+
mkdir -p "${ANDROID_SRC_DIR}/main/assets/nodejs-assets/presets"
54+
mkdir -p "${ANDROID_SRC_DIR}/icca/assets/nodejs-assets/presets"
55+
mv ./node_modules/mapeo-default-settings/dist "${ANDROID_SRC_DIR}/main/assets/nodejs-assets/presets/default"
56+
mv ./node_modules/mapeo-config-icca/dist "${ANDROID_SRC_DIR}/icca/assets/nodejs-assets/presets/default"
5057
cd ../..
5158
echo -en " done.\n"
5259

0 commit comments

Comments
 (0)