Skip to main content
Version: 1.0 (beta)

Options for Worklet Syntax Babel plugin

The plugin needs no options to work. The ones it offers let you run third party code inside worklet functions:

Type definitions

interface PluginOptions {
importForwarding?: {
moduleNames?: string[];
relativePaths?: string[];
};
silenceDangerousPathsWarning?: boolean;
}

How to use​

Using this is straightforward for Babel plugins; you just need to pass an object containing the options to the plugin in your babel.config.js file. Make sure to pass the plugin and its options as a two-element array.

Here's an example:

/** @type {import('react-native-worklets/plugin').PluginOptions} */
const workletsPluginOptions = {
importForwarding: { moduleNames: ['my-library'] },
}

module.exports = {
...
plugins: [
...
[
'react-native-worklets/plugin',
workletsPluginOptions
],
],
};

Options​

If you are using the Oxc plugin, refer to Oxc plugin options.

silenceDangerousPathsWarning​

Defaults to false.

The plugin prints a warning when an importForwarding.relativePaths entry has only common folder names, such as src, lib, app or app/src. Such a path also matches folders inside packages in node_modules. Set this option to true to hide the warning, for example when you have checked that the path matches only your code.

importForwarding​

Defaults to no forwarding (an empty object).

Configures import forwarding for Bundle Mode. See the dedicated page for the full overview.

importForwarding.moduleNames​

Defaults to an empty array. react-native-worklets is always forwarded, in addition to the listed modules.

Modules in this list are forwarded as imports inside worklet functions. Only named and default imports are forwarded; namespace imports such as import * as lib from 'my-library' are captured from the closure instead.

For example, to use all exports from my-library directly on the Worklet Runtime, add it to the list:

/** @type {import('react-native-worklets/plugin').PluginOptions} */
const workletsPluginOptions = {
// ...
importForwarding: { moduleNames: ['my-library'] },
};

At bundle time, when a worklet function imports from my-library, the plugin forwards that import into the worklet body:

BeforeAfter (conceptually)

import { someExport } from 'my-library';

function foo() {
'worklet';

someExport();
}
// Preserved in case it's used outside the worklet.
import { someExport } from 'my-library';

function foo() {
'worklet';
import { someExport } from 'my-library';
someExport(); // Forwarded.
}

The module name has to be an exact match. With my-library in the list, an import from my-library/some-file isn't forwarded. Add my-library/some-file to the list to forward it too:

BeforeAfter (conceptually)
import { someExport } from 'my-library';
import { someOtherExport } from 'my-library/some-file';

function foo() {
'worklet';

someExport();
someOtherExport();
}
import { someExport } from 'my-library';
import { someOtherExport } from 'my-library/some-file';

function foo() {
'worklet';
import { someExport } from 'my-library';
someExport(); // Forwarded.
someOtherExport(); // NOT forwarded (captured from closure).
}

importForwarding.relativePaths​

Defaults to an empty array. Relative imports inside react-native-worklets are always forwarded, in addition to the listed paths.

List of paths that determine whether a module should forward its relative imports inside worklet functions. If a module's path matches any of the provided paths, all relative imports inside that module's worklet functions will be forwarded.

Use this when authoring a library that calls helpers via relative imports inside its own worklet functions:

my-library/index.js
import { someExport } from './utils';

function foo() {
'worklet';

// We want it forwarded.
someExport();
}

Add the library's path to relativePaths:

/** @type {import('react-native-worklets/plugin').PluginOptions} */
const workletsPluginOptions = {
// ...
importForwarding: { relativePaths: ['my-library'] },
};

At bundle time, the plugin forwards relative imports inside my-library into the worklet body:

BeforeAfter (conceptually)

import { someExport } from './utils';

function foo() {
'worklet';

someExport();
}
// Preserved in case it's used outside the worklet.
import { someExport } from './utils';

function foo() {
'worklet';
import { someExport } from './utils';
someExport(); // Forwarded.
}

You can also narrow the path to a subdirectory — e.g. my-library/src or my-library/src/utils — to forward imports from only part of the library.

The plugin compares each path with the path of the module as a sequence of whole path segments (folder names and the file name), which can appear anywhere in the path. For a module inside node_modules, the plugin compares only the part after the last node_modules, which starts with the package name. So my-library matches node_modules/my-library/index.js, and my-library/src matches node_modules/my-library/src/utils.js.

warning

A common folder name, such as src, also matches every package that has a folder with that name. For example, src matches node_modules/react-native-reanimated/src/.... The plugin then forwards the relative imports inside that package. If a forwarded import loads code that cannot run on the Worklet Runtime, the app can crash. In development builds, the error can be Accessing __fbBatchedBridgeConfig is not allowed on Worklet Runtimes.

Use a sequence of segments that only your code has, for example @my-scope/my-library/src or a folder name that is unique to your app. Check that the sequence does not also occur in the paths of your dependencies. For example, my-library also matches node_modules/other-lib/my-library/index.js.

The plugin prints a warning when a path has only common folder names. You can hide it with silenceDangerousPathsWarning.

moduleNames won't work here: adding './utils' to it would forward every './utils' import across the bundle, potentially breaking other libraries with a file at the same relative path. relativePaths scopes forwarding to a specific package.