--- sidebar_position: 2 --- import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import useBaseUrl from '@docusaurus/useBaseUrl'; import ThemedImage from '@theme/ThemedImage'; # Setup Bundle Mode is the only mode of running worklets, so every native app using `react-native-worklets` has to be configured as follows: ## Configure Worklet Syntax plugin \{#configure-worklets-plugin} 1. **Add the Worklet Syntax plugin to your Babel configuration**. The [Worklet Syntax plugin](../worklets-plugin/about.mdx) extracts each worklet function into a module that Worklet Runtimes load from your bundle. ```javascript {7} module.exports = { presets: [ ... // don't add it here :) ], plugins: [ ... 'react-native-worklets/plugin', ], }; ``` The plugin needs no options. See [Options](../worklets-plugin/options.mdx) for what you can configure. ```javascript {7} module.exports = { presets: [ ... // don't add it here :) ], plugins: [ ... 'react-native-worklets/plugin-oxc/babel', ], }; ``` The Oxc plugin needs no options either. See the [Oxc plugin](../worklets-plugin/oxc.mdx) page. 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: ```javascript presets: [ ['babel-preset-expo', { worklets: false, reanimated: false }], ], ``` ## Configure Metro 2. **Modify your Metro configuration**. Metro needs to be configured to pre-load Bundle Mode entry points into your JavaScript bundle and to resolve the worklet modules the plugin emits. In your `metro.config.js` file, apply the config required for Bundle Mode. For example: ```javascript {3,10} // 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; ``` ```javascript {6,12-16} const { getDefaultConfig, mergeConfig, } = require('@react-native/metro-config'); const { bundleModeMetroConfig } = require('react-native-worklets/bundleMode'); const config = { // Your Metro config }; module.exports = mergeConfig( getDefaultConfig(__dirname), bundleModeMetroConfig, config ); ``` `bundleModeMetroConfig` sets `serializer.polyfillModuleNames`. `mergeConfig` replaces this array, it does not concatenate it. If your Metro config sets `serializer.polyfillModuleNames` too, use `getBundleModeMetroConfig`, which appends to your list: ```javascript const { getDefaultConfig, mergeConfig, } = require('@react-native/metro-config'); const { getBundleModeMetroConfig } = require('react-native-worklets/bundleMode'); const config = { // Your Metro config }; module.exports = getBundleModeMetroConfig( mergeConfig(getDefaultConfig(__dirname), config) ); ``` ## Use a Metro with inline modules Worklets 1.0 requires a Metro that supports inline modules: Metro 0.88.0 or newer, together with React Native 0.89 or newer, whose `@react-native/metro-babel-transformer` passes the `supportsInlineModules` flag to Babel plugins. Such a Metro supports `metro:inline` specifiers and virtual modules ([react/metro#2037](https://github.com/react/metro/pull/2037)). The plugin emits each worklet module inline, as a `require("metro:inline;base64,")` dependency of the file that defines the worklet, and Metro resolves it to a virtual module beside that file. Nothing is written to disk and Metro caches each worklet module like any other file. `bundleModeMetroConfig` and `getBundleModeMetroConfig` enable `resolver.unstable_allowVirtualModules` for you. No `metro` patch is needed. ### Bundlers other than Metro Under any other bundler, such as webpack, Turbopack or Babel run on its own, the plugin emits each worklet in place, as a factory function in the file that defines it. Nothing is written to disk and no module has to be resolved. Such a bundle has no Worklet Runtimes, so this output serves the web and tooling; running worklets on native requires Metro. A library that precompiles its worklets this way must still let Metro transform its source, for example through the `react-native` or `source` field of its `package.json`. A worklet taken from such precompiled output has no module to load on a Worklet Runtime and fails with `[Worklets] This worklet was compiled without 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 from module ...". This is due to the fact that the Fast Refresh update wasn't sent to the Worklet Runtime and it doesn't know that the module holding the new worklet function 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. 1. 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 the patch in [Step 3 of the installation](../getting-started/installation.mdx#step-3-patch-metro-runtime-for-fast-refresh). This patch is a temporary workaround until necessary changes are merged into Metro. ## Final steps Make sure to clear your Metro bundler's cache to ensure that all the changes are applied correctly. ```bash npm start -- --reset-cache ``` ```bash yarn start --reset-cache ``` And that's it! You can now happily enjoy worklets! You can learn more about using Bundle Mode [here](./usage.mdx). ## Reference To see how the setup looks in a real project, you can check out the [Bundle Mode Showcase App](https://github.com/software-mansion-labs/Bundle-Mode-showcase-app) repository.