
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
react-native-chunks
Advanced tools
A tool that allows you to split your bundle into several smaller chunks.
A tool that allows you to split your bundle into several smaller chunks. This is very helpful for OTA bundles when some shared parts of your bundle remain unchanged (such as localizations, third-party libraries, or internationalization polyfills). It can significantly reduce your OTA bundle size. For example, see the codepush-example, where the zipped CodePush bundle is reduced by up to 400 KB (only React Native and changed files are included in the bundle).
yarn add react-native-chunks
Wrap your metro.config into withChunksConfig helper
const { withChunksConfig } = require('react-native-chunks/metro-config')
const config = getConfig(getDefaultConfig(__dirname), {
// ...
})
module.exports = withChunksConfig(config)
Create chunks.config.json file in the root of the project.
You can check description of file config structure here.
{
"outputDir": "./.chunks",
"chunks": [{
"name": "core",
"entryFile": "src/core/index.ts",
"exclude": [
"node_modules/react-native"
]
}]
}
bundleCommand as chunk-bundle command in the android/app/build.gradle/* Bundling */
// A list containing the node command and its flags. Default is just 'node'.
// nodeExecutableAndArgs = ["node"]
//
// The command to run when bundling. By default is 'bundle'
-// bundleCommand = "ram-bundle"
+bundleCommand = "chunk-bundle"
//
// The path to the CLI configuration file. Default is empty.
// bundleConfig = file(../rn-cli.config.js)
CHUNKS_APK_BUILD_TIME to defaultConfig in the android/app/build.gradleandroid {
ndkVersion rootProject.ext.ndkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
compileSdk rootProject.ext.compileSdkVersion
namespace "chunks.example"
defaultConfig {
applicationId "chunks.example"
+ resValue 'string', "CHUNKS_APK_BUILD_TIME", String.format("\"%d\"", System.currentTimeMillis())
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
In .xcode.env add CLI_PATH and BUNDLE_COMMAND
# This `.xcode.env` file is versioned and is used to source the environment
# used when running script phases inside Xcode.
# To customize your local environment, you can create an `.xcode.env.local`
# file that is not versioned.
+export CLI_PATH="../node_modules/react-native/cli.js"
+export BUNDLE_COMMAND="chunk-bundle"
# NODE_BINARY variable contains the PATH to the node executable.
#
# Customize the NODE_BINARY variable here.
# For example, to use nvm with brew, add the following line
# . "$(brew --prefix nvm)/nvm.sh" --no-use
export NODE_BINARY=$(command -v node)
chunks builds chunks and places the output in the outputDir specified in the chunks.config.json file.
react-native chunks --platform android
--chunkreact-native chunks --platform android --chunk core
--force flag to force it to rebuild the chunks.react-native chunks --platform android --force
The chunk command builds chunks and the main bundle. It functions similarly to react-native bundle, but it builds the chunks before the main bundle.
react-native chunk-bundle --platform android
The chunks-compose-sourcemaps command composes all sourcemaps into one.
This command collects all sourcemaps from the chunks and composes them with the provided --bundle-sourcemap. The output is the composed source map, which is written to the file specified in --bundle-sourcemap-output. If --bundle-sourcemap-output is not defined, the output will be written to --bundle-sourcemap.
yarn react-native chunks-compose-sourcemaps --platform android --bundle-sourcemap ./android/app/build/generated/sourcemaps/react/release/index.android.bundle.map
react-native-chunks has a configuration mechanism that allows changing its behavior and providing additional features.
react-native-chunks can be configured by creating a chunks.config.js at the root of the project.
interface ChunksConfig {
/**
* The output directory where the bundled chunks should be located.
**/
outputDir?: string
chunks?: Array<{
/**
* Name of the chunk
**/
name: string
/**
* The entry point of the chunk.
**/
entryFile: string
/**
* Path to files that should be excluded from chunks.
* This can be helpful when a chunk uses shared libraries that are already present in the main bundle, such as react-native.
*
* Example:
*
* exclude: ['**/node_modules/react-native/**', '<rootDir>/src/index.js', { deps: true, pattern: '**/node_modules/react/**' }]
**/
exclude?: Array<string | { deps?: boolean; pattern: string }>
/**
* Path to files that should be included in the chunk.
* This can be helpful when a chunk should include some modules, even if they are excluded by the exclude option.
*
* Example:
*
* exclude: ['**/node_modules/react-native/**', '<rootDir>/src/index.js', { deps: true, pattern: '**/node_modules/react/**' }]
**/
include?: Array<string | { deps?: boolean; pattern: string }>
/**
* [Optional]
*
* The output directory where the bundled chunk should be located. This overrides the root outputDir property.
**/
outputDir?: string
}>
bundle?: {
/**
* Path to files that should be excluded from the bundle.
*
* Example:
*
* exclude: ['**/node_modules/react-native/**', '<rootDir>/src/index.js', { deps: true, pattern: '**/node_modules/react/**' }]
**/
exclude?: Array<string | { deps?: boolean; pattern: string }>
/**
* Path to files that should be included in the bundle.
*
* Example:
*
* exclude: ['**/node_modules/react-native/**', '<rootDir>/src/index.js', { deps: true, pattern: '**/node_modules/react/**' }]
**/
include?: Array<string | { deps?: boolean; pattern: string }>
}
}
react-native-chunks builds small chunks, resulting in several bundles and sourcemap files. To see the correct stack trace in monitoring systems like Sentry and Bugsnag, you need to compose the sourcemap file before sending it to these monitoring systems.
You can do it via chunks-compose-sourcemaps command. Please check the description for more details.
It works only for production builds, so debugging and developer mode are not affected in any way.
See "Under the hood section for more details.
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT
Made with create-react-native-library
FAQs
A tool that allows you to split your bundle into several smaller chunks.
The npm package react-native-chunks receives a total of 3 weekly downloads. As such, react-native-chunks popularity was classified as not popular.
We found that react-native-chunks demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.