Skip to content

Commit ecacb53

Browse files
committed
init
1 parent 4115e83 commit ecacb53

File tree

8 files changed

+700
-3578
lines changed

8 files changed

+700
-3578
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"packages": [
77
"packages/react-native-reanimated",
88
"packages/eslint-plugin-reanimated",
9+
"packages/react-native-reanimated/plugin",
910
"apps/*"
1011
]
1112
},
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const reanimatedPlugin = require('./index.js');
2+
3+
module.exports = {
4+
plugins: [reanimatedPlugin],
5+
};

packages/react-native-reanimated/plugin/build/plugin.js

Lines changed: 79 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/react-native-reanimated/plugin/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
22
"devDependencies": {
3+
"@babel/cli": "^7.20.0",
4+
"@babel/core": "^7.20.0",
5+
"@babel/plugin-transform-unicode-regex": "^7.24.7",
6+
"@babel/traverse": "^7.20.0",
7+
"@babel/types": "^7.20.0",
38
"@react-native/eslint-config": "^0.72.1",
49
"@types/node": "^18.15.11",
510
"@typescript-eslint/eslint-plugin": "^7.0.2",
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import {
2+
blockStatement,
3+
directive,
4+
directiveLiteral,
5+
returnStatement,
6+
} from '@babel/types';
7+
8+
import type {
9+
Expression,
10+
Program,
11+
BlockStatement,
12+
VariableDeclaration,
13+
} from '@babel/types';
14+
import type { NodePath } from '@babel/core';
15+
import type { ReanimatedPluginPass } from './types';
16+
17+
export function processIfWorkletFile(
18+
path: NodePath<Program>,
19+
state: ReanimatedPluginPass
20+
) {
21+
if (
22+
path.node.directives.some(
23+
(functionDirective) => functionDirective.value.value === 'worklet'
24+
)
25+
) {
26+
processWorkletFile(path, state);
27+
// Remove 'worklet' directive from the file afterwards.
28+
path.node.directives = path.node.directives.filter(
29+
(functionDirective) => functionDirective.value.value !== 'worklet'
30+
);
31+
}
32+
}
33+
34+
function processWorkletFile(
35+
path: NodePath<Program>,
36+
_state: ReanimatedPluginPass
37+
) {
38+
path.get('body').forEach((bodyPath) => {
39+
if (bodyPath.isVariableDeclaration()) {
40+
processVariableDeclaration(bodyPath);
41+
}
42+
if (bodyPath.isFunctionDeclaration()) {
43+
appendWorkletDirective(bodyPath.node.body);
44+
}
45+
});
46+
}
47+
48+
function processVariableDeclaration(path: NodePath<VariableDeclaration>) {
49+
path.get('declarations').forEach((declaration) => {
50+
const initPath = declaration.get('init');
51+
if (initPath.isFunctionExpression()) {
52+
appendWorkletDirective(initPath.node.body);
53+
} else if (initPath.isArrowFunctionExpression()) {
54+
const bodyPath = initPath.get('body');
55+
56+
// In case of arrow function with no body, i.e. () => 1.
57+
if (!bodyPath.isBlockStatement()) {
58+
bodyPath.replaceWith(
59+
blockStatement([returnStatement(bodyPath.node as Expression)])
60+
);
61+
}
62+
appendWorkletDirective(bodyPath.node as BlockStatement);
63+
} else if (initPath.isObjectExpression()) {
64+
initPath.node.properties.forEach((property) => {
65+
if (property.type === 'ObjectMethod') {
66+
appendWorkletDirective(property.body);
67+
}
68+
});
69+
}
70+
});
71+
}
72+
73+
function appendWorkletDirective(node: BlockStatement) {
74+
if (
75+
!node.directives.some(
76+
(functionDirective) => functionDirective.value.value === 'worklet'
77+
)
78+
) {
79+
node.directives.push(directive(directiveLiteral('worklet')));
80+
}
81+
}

packages/react-native-reanimated/plugin/src/plugin.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { PluginItem, NodePath } from '@babel/core';
2-
import type { CallExpression } from '@babel/types';
2+
import type { CallExpression, JSXAttribute, Program } from '@babel/types';
33
import {
44
processIfAutoworkletizableCallback,
55
processCalleesAutoworkletizableCallbacks,
@@ -11,6 +11,7 @@ import { processInlineStylesWarning } from './inlineStylesWarning';
1111
import { addCustomGlobals } from './utils';
1212
import { initializeGlobals } from './globals';
1313
import { substituteWebCallExpression } from './webOptimization';
14+
import { processIfWorkletFile } from './file';
1415

1516
module.exports = function (): PluginItem {
1617
function runWithTaggedExceptions(fun: () => void) {
@@ -50,8 +51,15 @@ module.exports = function (): PluginItem {
5051
});
5152
},
5253
},
54+
Program: {
55+
enter(path: NodePath<Program>, state: ReanimatedPluginPass) {
56+
runWithTaggedExceptions(() => {
57+
processIfWorkletFile(path, state);
58+
});
59+
},
60+
},
5361
JSXAttribute: {
54-
enter(path, state) {
62+
enter(path: NodePath<JSXAttribute>, state: ReanimatedPluginPass) {
5563
runWithTaggedExceptions(() =>
5664
processInlineStylesWarning(path, state)
5765
);

0 commit comments

Comments
 (0)