Skip to content

Commit 396593a

Browse files
authored
feat: support Reanimated 4 (#1226)
1 parent 47bf30c commit 396593a

File tree

10 files changed

+292
-113
lines changed

10 files changed

+292
-113
lines changed

.changeset/blue-ears-leave.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@callstack/repack-plugin-reanimated": minor
3+
"@callstack/repack": minor
4+
---
5+
6+
Reanimated Plugin: Add support for Reanimated 4

apps/tester-app/babel.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ module.exports = {
99
importSource: 'nativewind',
1010
},
1111
],
12+
'react-native-worklets/plugin',
1213
],
1314
};

apps/tester-app/ios/Podfile.lock

Lines changed: 82 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,7 +2308,7 @@ PODS:
23082308
- ReactCommon/turbomodule/core
23092309
- SocketRocket
23102310
- Yoga
2311-
- RNReanimated (3.18.0):
2311+
- RNReanimated (4.0.0):
23122312
- boost
23132313
- DoubleConversion
23142314
- fast_float
@@ -2335,11 +2335,11 @@ PODS:
23352335
- ReactCodegen
23362336
- ReactCommon/turbomodule/bridging
23372337
- ReactCommon/turbomodule/core
2338-
- RNReanimated/reanimated (= 3.18.0)
2339-
- RNReanimated/worklets (= 3.18.0)
2338+
- RNReanimated/reanimated (= 4.0.0)
2339+
- RNWorklets
23402340
- SocketRocket
23412341
- Yoga
2342-
- RNReanimated/reanimated (3.18.0):
2342+
- RNReanimated/reanimated (4.0.0):
23432343
- boost
23442344
- DoubleConversion
23452345
- fast_float
@@ -2366,10 +2366,12 @@ PODS:
23662366
- ReactCodegen
23672367
- ReactCommon/turbomodule/bridging
23682368
- ReactCommon/turbomodule/core
2369-
- RNReanimated/reanimated/apple (= 3.18.0)
2369+
- RNReanimated/reanimated/apple (= 4.0.0)
2370+
- RNReanimated/reanimated/view (= 4.0.0)
2371+
- RNWorklets
23702372
- SocketRocket
23712373
- Yoga
2372-
- RNReanimated/reanimated/apple (3.18.0):
2374+
- RNReanimated/reanimated/apple (4.0.0):
23732375
- boost
23742376
- DoubleConversion
23752377
- fast_float
@@ -2396,9 +2398,10 @@ PODS:
23962398
- ReactCodegen
23972399
- ReactCommon/turbomodule/bridging
23982400
- ReactCommon/turbomodule/core
2401+
- RNWorklets
23992402
- SocketRocket
24002403
- Yoga
2401-
- RNReanimated/worklets (3.18.0):
2404+
- RNReanimated/reanimated/view (4.0.0):
24022405
- boost
24032406
- DoubleConversion
24042407
- fast_float
@@ -2425,10 +2428,10 @@ PODS:
24252428
- ReactCodegen
24262429
- ReactCommon/turbomodule/bridging
24272430
- ReactCommon/turbomodule/core
2428-
- RNReanimated/worklets/apple (= 3.18.0)
2431+
- RNWorklets
24292432
- SocketRocket
24302433
- Yoga
2431-
- RNReanimated/worklets/apple (3.18.0):
2434+
- RNSVG (15.12.0):
24322435
- boost
24332436
- DoubleConversion
24342437
- fast_float
@@ -2455,9 +2458,10 @@ PODS:
24552458
- ReactCodegen
24562459
- ReactCommon/turbomodule/bridging
24572460
- ReactCommon/turbomodule/core
2461+
- RNSVG/common (= 15.12.0)
24582462
- SocketRocket
24592463
- Yoga
2460-
- RNSVG (15.12.0):
2464+
- RNSVG/common (15.12.0):
24612465
- boost
24622466
- DoubleConversion
24632467
- fast_float
@@ -2484,10 +2488,69 @@ PODS:
24842488
- ReactCodegen
24852489
- ReactCommon/turbomodule/bridging
24862490
- ReactCommon/turbomodule/core
2487-
- RNSVG/common (= 15.12.0)
24882491
- SocketRocket
24892492
- Yoga
2490-
- RNSVG/common (15.12.0):
2493+
- RNWorklets (0.4.0):
2494+
- boost
2495+
- DoubleConversion
2496+
- fast_float
2497+
- fmt
2498+
- glog
2499+
- hermes-engine
2500+
- RCT-Folly
2501+
- RCT-Folly/Fabric
2502+
- RCTRequired
2503+
- RCTTypeSafety
2504+
- React-Core
2505+
- React-debug
2506+
- React-Fabric
2507+
- React-featureflags
2508+
- React-graphics
2509+
- React-hermes
2510+
- React-ImageManager
2511+
- React-jsi
2512+
- React-NativeModulesApple
2513+
- React-RCTFabric
2514+
- React-renderercss
2515+
- React-rendererdebug
2516+
- React-utils
2517+
- ReactCodegen
2518+
- ReactCommon/turbomodule/bridging
2519+
- ReactCommon/turbomodule/core
2520+
- RNWorklets/worklets (= 0.4.0)
2521+
- SocketRocket
2522+
- Yoga
2523+
- RNWorklets/worklets (0.4.0):
2524+
- boost
2525+
- DoubleConversion
2526+
- fast_float
2527+
- fmt
2528+
- glog
2529+
- hermes-engine
2530+
- RCT-Folly
2531+
- RCT-Folly/Fabric
2532+
- RCTRequired
2533+
- RCTTypeSafety
2534+
- React-Core
2535+
- React-debug
2536+
- React-Fabric
2537+
- React-featureflags
2538+
- React-graphics
2539+
- React-hermes
2540+
- React-ImageManager
2541+
- React-jsi
2542+
- React-NativeModulesApple
2543+
- React-RCTFabric
2544+
- React-renderercss
2545+
- React-rendererdebug
2546+
- React-utils
2547+
- ReactCodegen
2548+
- ReactCommon/turbomodule/bridging
2549+
- ReactCommon/turbomodule/core
2550+
- RNWorklets/worklets/apple (= 0.4.0)
2551+
- SocketRocket
2552+
- Yoga
2553+
- RNWorklets/worklets/apple (0.4.0):
24912554
- boost
24922555
- DoubleConversion
24932556
- fast_float
@@ -2597,12 +2660,13 @@ DEPENDENCIES:
25972660
- ReactAppDependencyProvider (from `build/generated/ios`)
25982661
- ReactCodegen (from `build/generated/ios`)
25992662
- ReactCommon/turbomodule/core (from `../node_modules/react-native/ReactCommon`)
2600-
- "ReactNativeHost (from `../../../node_modules/.pnpm/react-native-test-app@4.4.4_react-native@0.80.0_@babel+core@7.25.2_@react-native-community+cl_2c2ikb6jukpexpxx5nck4v64iq/node_modules/@rnx-kit/react-native-host`)"
2663+
- "ReactNativeHost (from `../../../node_modules/.pnpm/react-native-test-app@4.4.4_patch_hash=wsdoulokkdggtxg73oqeyh4rwm_react-native@0.80.0_@babel+_swgpimjjfa6bbuovbhzmrpqrqu/node_modules/@rnx-kit/react-native-host`)"
26012664
- ReactTestApp-DevSupport (from `../node_modules/react-native-test-app`)
26022665
- ReactTestApp-Resources (from `..`)
26032666
- "RNCAsyncStorage (from `../node_modules/@react-native-async-storage/async-storage`)"
26042667
- RNReanimated (from `../node_modules/react-native-reanimated`)
26052668
- RNSVG (from `../node_modules/react-native-svg`)
2669+
- RNWorklets (from `../node_modules/react-native-worklets`)
26062670
- SocketRocket (~> 0.7.1)
26072671
- Yoga (from `../node_modules/react-native/ReactCommon/yoga`)
26082672

@@ -2761,7 +2825,7 @@ EXTERNAL SOURCES:
27612825
ReactCommon:
27622826
:path: "../node_modules/react-native/ReactCommon"
27632827
ReactNativeHost:
2764-
:path: "../../../node_modules/.pnpm/react-native-test-app@4.4.4_react-native@0.80.0_@babel+core@7.25.2_@react-native-community+cl_2c2ikb6jukpexpxx5nck4v64iq/node_modules/@rnx-kit/react-native-host"
2828+
:path: "../../../node_modules/.pnpm/react-native-test-app@4.4.4_patch_hash=wsdoulokkdggtxg73oqeyh4rwm_react-native@0.80.0_@babel+_swgpimjjfa6bbuovbhzmrpqrqu/node_modules/@rnx-kit/react-native-host"
27652829
ReactTestApp-DevSupport:
27662830
:path: "../node_modules/react-native-test-app"
27672831
ReactTestApp-Resources:
@@ -2772,6 +2836,8 @@ EXTERNAL SOURCES:
27722836
:path: "../node_modules/react-native-reanimated"
27732837
RNSVG:
27742838
:path: "../node_modules/react-native-svg"
2839+
RNWorklets:
2840+
:path: "../node_modules/react-native-worklets"
27752841
Yoga:
27762842
:path: "../node_modules/react-native/ReactCommon/yoga"
27772843

@@ -2854,8 +2920,9 @@ SPEC CHECKSUMS:
28542920
ReactTestApp-DevSupport: bc5bc7f645b7bc18d2c9b6d4203e78d8ab634250
28552921
ReactTestApp-Resources: 8d72c3deef156833760694a288ff334af4d427d7
28562922
RNCAsyncStorage: 767abb068db6ad28b5f59a129fbc9fab18b377e2
2857-
RNReanimated: 7e0ba8b6914407ca44bb187dbafa7a90c9f0fa52
2923+
RNReanimated: a21eac73d0e9c3d50821ec49d7c6a409706f78b4
28582924
RNSVG: c73af7848d94ca3e8136a5191d055e3c1d6fedab
2925+
RNWorklets: 5e10b99988778b7a08a8aaeba9ad7f3e56787bf4
28592926
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
28602927
SwiftyRSA: 8c6dd1ea7db1b8dc4fb517a202f88bb1354bc2c6
28612928
Yoga: 395b5d614cd7cbbfd76b05d01bd67230a6ad004e

apps/tester-app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
"react": "catalog:",
2727
"react-native": "catalog:",
2828
"react-native-css-interop": "0.1.22",
29-
"react-native-reanimated": "3.18.0",
29+
"react-native-reanimated": "4.0.0",
3030
"react-native-safe-area-context": "5.5.0",
3131
"react-native-svg": "15.12.0",
32+
"react-native-worklets": "0.4.0",
3233
"tailwind-merge": "^2.6.0"
3334
},
3435
"devDependencies": {

packages/plugin-reanimated/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
"engines": {
3737
"node": ">=18"
3838
},
39-
"dependencies": {},
39+
"dependencies": {
40+
"semver": "^7.7.2"
41+
},
4042
"peerDependencies": {
4143
"@babel/core": "^7.20",
4244
"@callstack/repack": "workspace:*"
@@ -46,6 +48,7 @@
4648
"@rspack/core": "catalog:",
4749
"@types/babel__core": "7.20.5",
4850
"@types/node": "catalog:",
51+
"@types/semver": "^7.7.0",
4952
"webpack": "catalog:"
5053
},
5154
"scripts": {
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
export { ReanimatedPlugin } from './plugin.js';
2-
export { reanimatedModuleRules } from './rules.js';
2+
export {
3+
reanimated3ModuleRules,
4+
reanimated4ModuleRules,
5+
reanimatedModuleRules,
6+
} from './rules.js';

packages/plugin-reanimated/src/loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default function reanimatedLoader(
5454
configFile: false,
5555
compact: false,
5656
comments: true,
57-
plugins: [...babelPlugins, 'react-native-reanimated/plugin'],
57+
plugins: babelPlugins,
5858
},
5959
(err, result) => {
6060
if (err) {

packages/plugin-reanimated/src/plugin.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import path from 'node:path';
12
import type { Compiler as RspackCompiler } from '@rspack/core';
3+
import semver, { type SemVer } from 'semver';
24
import type { Compiler as WebpackCompiler } from 'webpack';
3-
import { reanimatedModuleRules } from './rules.js';
5+
import { reanimated3ModuleRules, reanimated4ModuleRules } from './rules.js';
46

57
export class ReanimatedPlugin {
68
apply(compiler: RspackCompiler): void;
@@ -9,8 +11,19 @@ export class ReanimatedPlugin {
911
apply(__compiler: unknown) {
1012
const compiler = __compiler as RspackCompiler;
1113

14+
const reanimatedPath = this.ensureDependencyInstalled(
15+
compiler.context,
16+
'react-native-reanimated'
17+
);
18+
19+
const reanimatedVersion = this.getReanimatedVersion(reanimatedPath);
20+
1221
// add rules for transpiling wih reanimated loader
13-
compiler.options.module.rules.push(reanimatedModuleRules);
22+
compiler.options.module.rules.push(
23+
reanimatedVersion.major < 4
24+
? reanimated3ModuleRules
25+
: reanimated4ModuleRules
26+
);
1427

1528
// ignore the 'setUpTests' warning from reanimated which is not relevant
1629
compiler.options.ignoreWarnings = compiler.options.ignoreWarnings ?? [];
@@ -20,4 +33,38 @@ export class ReanimatedPlugin {
2033
)
2134
);
2235
}
36+
37+
private ensureDependencyInstalled(context: string, dependency: string) {
38+
try {
39+
// resolve the path to the dependency package.json
40+
// since its always in the root dir of the dependency
41+
const dependencyPackageJsonPath = path.join(dependency, 'package.json');
42+
const dependencyPath = require.resolve(dependencyPackageJsonPath, {
43+
paths: [context],
44+
});
45+
return path.dirname(dependencyPath);
46+
} catch {
47+
const error = new Error(
48+
`[RepackReanimatedPlugin] Dependency named '${dependency}' is required but not found in your project. ` +
49+
'Did you forget to install it?'
50+
);
51+
// remove the stack trace to make the error more readable
52+
error.stack = undefined;
53+
throw error;
54+
}
55+
}
56+
57+
private getReanimatedVersion(reanimatedPath: string): SemVer {
58+
const reanimatedPackageJsonPath = path.join(reanimatedPath, 'package.json');
59+
const reanimatedPackageJson = require(reanimatedPackageJsonPath);
60+
const version = semver.parse(reanimatedPackageJson.version);
61+
62+
if (!version) {
63+
throw new Error(
64+
`[RepackReanimatedPlugin] Unable to parse version for react-native-reanimated: ${reanimatedPackageJson.version}`
65+
);
66+
}
67+
68+
return version;
69+
}
2370
}

0 commit comments

Comments
 (0)