Skip to content

Files

Latest commit

author
Ivan Kuchaev
Nov 3, 2024
1069a79 · Nov 3, 2024

History

History
78 lines (60 loc) · 2.35 KB

codepush-example.md

File metadata and controls

78 lines (60 loc) · 2.35 KB

Thanks to react-native-chunk, CodePush/OTA bundles can be reduced by up to 400 KB in zip size. You can check the codepush-example app as a reference, Here the key features are described.

To use react-native-chunks for reducing bundles, you need to:

  1. Create a chunks.config.js file in the root of the project with empty chunks, including the react-native related code and the entry point file.
module.exports = {
  bundle: {
    include: [
      { deps: true, pattern: '**/node_modules/react-native/**' }, // react-native files and all dependencies of react-native
      '**/node_modules/react-native-chunks/**', // react-native-chunks files
      '<rootDir>/index.js', // entry point
    ],
    exclude: ['**/*'],
  },
}

and add the logic to include changed files into the codepush bundle

+let changedFiles = []

+if (process.env.CODEPUSH_DIFF) {
+  changedFiles = require('./changed-deps.json') // path to the file that contains the changed files between two Git revisions..
+}

module.exports = {
  bundle: {
    include: [
      { deps: true, pattern: '**/node_modules/react-native/**' }, // react-native files and all dependencies of react-native
      '**/node_modules/react-native-chunks/**', // react-native-chunks files
      '<rootDir>/index.js', // entry point
+     ...changedFiles
    ],
    exclude: ['**/*'],
  },
}
  1. Wrap the metro config into the withChunksConfig helper.
const { getDefaultConfig, mergeConfig } = require('@react-native/metro-config')
const { withChunksConfig } = require('react-native-chunks/metro-config')

const config = {
  // ...
}

module.exports = withChunksConfig(
  mergeConfig(getDefaultConfig(__dirname), config)
)
  1. [Android] add CHUNKS_APK_BUILD_TIME to defaultConfig in the android/app/build.gradle
android {
    defaultConfig {
        applicationId "chunks.example"
+       resValue 'string', "CHUNKS_APK_BUILD_TIME", String.format("\"%d\"", System.currentTimeMillis())
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
    }
  1. Create a shell script to build codepush bundles
yarn react-native chunk-changed-deps --platform {platform} --entry-file {your_entry_file}  --commit {commit} > ./changed-deps.json
CODEPUSH_DIFF=true yarn code-push-diff release --app {codepush_app} --base {commit}