Skip to main content
Version: Unreleased

Setup

To use react-native-worklets's Bundle Mode feature, you need to make the following changes in your repository:

Configure Worklet Syntax plugin​

  1. Modify your Babel configuration. Worklet Syntax plugin needs to know that it has to prepare your code for the Bundle Mode. This is also the source of truth for your app to know if the Bundle Mode is enabled. In your babel.config.js file, add the following:
/** @type {import('react-native-worklets/plugin').PluginOptions} */
const workletsPluginOptions = {
bundleMode: true,
strictGlobal: true, // optional, but recommended
}

module.exports = {
presets: [
... // don't add it here :)
],
plugins: [
...
['react-native-worklets/plugin', workletsPluginOptions],
],
};

If you use Expo, babel-preset-expo already adds the Worklet Syntax plugin on its own, without your options. With Reanimated installed, it adds the same plugin through react-native-reanimated/plugin. Turn both off in the preset, so the plugin doesn't run twice:

presets: [
['babel-preset-expo', { worklets: false, reanimated: false }],
],

Configure Metro​

  1. Modify your Metro configuration. Metro needs to be configured to pre-load Bundle Mode entry points into your JavaScript bundle. In your metro.config.js file, apply the config required for the bundle mode. For example:
// Learn more https://docs.expo.io/guides/customizing-metro
const { getDefaultConfig } = require("expo/metro-config");
const { getBundleModeMetroConfig } = require("react-native-worklets/bundleMode");

/** @type {import('expo/metro-config').MetroConfig} */
let config = getDefaultConfig(__dirname);

// Your modifications to the config

config = getBundleModeMetroConfig(config);

module.exports = config;

Enable seamless Metro bundling​

This step is optional but highly recommended.

If you run the app after just executing the above steps, you will see an error "Failed to get the SHA-1 for ...":

Bundle Mode Metro error logsBundle Mode Metro error logs

This is due to the fact that Bundle Mode isolates each worklet function in a separate module (file) on the fly and the Metro bundler doesn't index these isolated modules in time.

To fix this issue you can do one of the following:

  1. Reload the app and Metro a couple of times until Metro picks up all the new modules. You will have to do it each time. Not recommended.
  2. Patch metro package for it to synchronously index new modules created by Bundle Mode. This is the recommended approach. This patch does not require building React Native from source. You can find patching instructions in the Worklets repository.

This patch is a temporary workaround until necessary changes are merged into Metro.

Enable fast refresh on Worklet Runtimes​

This step is optional but highly recommended.

React Native's Fast Refresh (hot reload) feature doesn't apply to Worklet Runtimes out of the box. Therefore, when you change a worklet function's code you will see an error "[Worklets] Unable to resolve worklet with hash ...".

Bundle Mode Fast Refresh error logsBundle Mode Fast Refresh error logs

This is due to the fact that the Fast Refresh update wasn't sent to the Worklet Runtime and it doesn't know that a new worklet function with the given hash exists.

To fix this issue you can do one of the following:

  1. Reload the app on each worklet code change. This virtually defeats the purpose of Fast Refresh. Not recommended.
  2. Patch metro-runtime package for it to send Fast Refresh updates to Worklet Runtimes. This is the recommended approach. This patch does not require building React Native from source. You can find patching instructions in the Worklets repository.

This patch is a temporary workaround until necessary changes are merged into Metro.

Reduce development memory usage​

This step is optional and experimental.

Every Worklet Runtime loads the application bundle. In production builds, this is virtually free. In development, however, the bundle is sent as source code and evaluated by each Runtime. The evaluation causes a persistent increase in memory consumption for each Runtime, proportional to the bundle size. This often leads to OOM crashes on both simulators and physical devices for larger apps (GH issue).

To work around this, you can send the development bundle as bytecode instead of source code. Metro pull request #2316 adds this feature.

Until the feature is released, you can patch Metro locally. Apply the prepared Metro 0.88.0 development-bytecode patches using the package patching instructions. Then add the Hermes compiler bundled with React Native to the framework-specific Metro config from the previous section:

metro.config.js
const path = require('node:path');

config.serializer ??= {};
config.serializer.unstable_hermesCompilerPath = getHermesCompilerPath();

function getHermesCompilerPath() {
const binary = {
darwin: 'osx-bin/hermesc',
linux: 'linux64-bin/hermesc',
win32: 'win64-bin/hermesc.exe',
}[process.platform];

return require.resolve(`hermes-compiler/hermesc/${binary}`, {
paths: [path.dirname(require.resolve('react-native/package.json'))],
});
}

Restart Metro with a cleared cache. Rebuild the native app after changing React Native so that its Hermes runtime matches the compiler.

This option is experimental and supports Android and iOS Hermes builds. The bundled Linux compiler requires an x86-64 host.

React Native DevTools can connect to the app, but it cannot display or source-debug the initial bytecode bundle. Breakpoints and stepping in that bundle are unavailable.

Fast Refresh, console evaluation, and error symbolication still work.

Final steps​

Make sure to clear your Metro bundler's cache to ensure that all the changes are applied correctly.

npm start -- --reset-cache

And that's it! You can now happily enjoy the Bundle Mode! You can learn more about using the Bundle Mode here.

Reference​

To see how the setup looks in a real project, you can check out the Bundle Mode Showcase App repository.