Skip to content

Commit 58a459f

Browse files
authored
feat: support passing options to reanimated / worklets plugin (#1269)
1 parent a9ac9f2 commit 58a459f

File tree

5 files changed

+39
-10
lines changed

5 files changed

+39
-10
lines changed

.changeset/mean-dodos-hear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@callstack/repack-plugin-reanimated": minor
3+
---
4+
5+
Allow passing babel plugin options to RepackPluginReanimated

.changeset/tangy-lines-cheer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@callstack/repack-plugin-reanimated": patch
3+
---
4+
5+
Ensure react-native-worklets is installed when using Reanimated >=4

apps/tester-app/rspack.config.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ export default Repack.defineRspackConfig((env) => {
120120
},
121121
],
122122
}),
123-
new ReanimatedPlugin(),
123+
new ReanimatedPlugin({
124+
babelPluginOptions: { relativeSourceLocation: true },
125+
}),
124126
new NativeWindPlugin({ cssInteropOptions: { inlineRem: 16 } }),
125127
process.env.RSDOCTOR && new RsdoctorRspackPlugin(),
126128
].filter(Boolean),

packages/plugin-reanimated/src/plugin.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@ import path from 'node:path';
22
import type { Compiler as RspackCompiler } from '@rspack/core';
33
import semver, { type SemVer } from 'semver';
44
import type { Compiler as WebpackCompiler } from 'webpack';
5-
import { reanimated3ModuleRules, reanimated4ModuleRules } from './rules.js';
5+
import { createReanimatedModuleRules } from './rules.js';
6+
7+
interface ReanimatedPluginOptions {
8+
/**
9+
* Custom options passed to 'react-native-reanimated/plugin' or 'react-native-worklets/plugin' babel plugins.
10+
*/
11+
babelPluginOptions?: Record<string, any>;
12+
}
613

714
export class ReanimatedPlugin {
15+
constructor(private options: ReanimatedPluginOptions = {}) {}
16+
817
apply(compiler: RspackCompiler): void;
918
apply(compiler: WebpackCompiler): void;
1019

@@ -18,12 +27,17 @@ export class ReanimatedPlugin {
1827

1928
const reanimatedVersion = this.getReanimatedVersion(reanimatedPath);
2029

21-
// add rules for transpiling wih reanimated loader
30+
if (reanimatedVersion.major >= 4) {
31+
this.ensureDependencyInstalled(compiler.context, 'react-native-worklets');
32+
}
33+
34+
// add rules for transpiling with reanimated loader
2235
// TODO made obsolete by the new babel-swc-loader, remove in 6.0
2336
compiler.options.module.rules.push(
24-
reanimatedVersion.major < 4
25-
? reanimated3ModuleRules
26-
: reanimated4ModuleRules
37+
createReanimatedModuleRules(
38+
reanimatedVersion.major,
39+
this.options.babelPluginOptions
40+
)
2741
);
2842

2943
// ignore the 'setUpTests' warning from reanimated which is not relevant

packages/plugin-reanimated/src/rules.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { getModulePaths } from '@callstack/repack';
22

3-
const createReanimatedModuleRules = (majorVersion: number) => {
3+
export const createReanimatedModuleRules = (
4+
majorVersion: number,
5+
pluginOptions: Record<string, any> = {}
6+
) => {
47
const workletsBabelPlugin =
58
majorVersion < 4
69
? 'react-native-reanimated/plugin'
@@ -27,7 +30,7 @@ const createReanimatedModuleRules = (majorVersion: number) => {
2730
'@babel/plugin-syntax-typescript',
2831
{ isTSX: false, allowNamespaces: true },
2932
],
30-
workletsBabelPlugin,
33+
[workletsBabelPlugin, pluginOptions],
3134
],
3235
},
3336
},
@@ -42,7 +45,7 @@ const createReanimatedModuleRules = (majorVersion: number) => {
4245
'@babel/plugin-syntax-typescript',
4346
{ isTSX: true, allowNamespaces: true },
4447
],
45-
workletsBabelPlugin,
48+
[workletsBabelPlugin, pluginOptions],
4649
],
4750
},
4851
},
@@ -54,7 +57,7 @@ const createReanimatedModuleRules = (majorVersion: number) => {
5457
options: {
5558
babelPlugins: [
5659
'babel-plugin-syntax-hermes-parser',
57-
workletsBabelPlugin,
60+
[workletsBabelPlugin, pluginOptions],
5861
],
5962
},
6063
},

0 commit comments

Comments
 (0)