-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathesbuildReanimatedPlugin.js
More file actions
160 lines (136 loc) · 4.32 KB
/
Copy pathesbuildReanimatedPlugin.js
File metadata and controls
160 lines (136 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const babel = require('@babel/core');
const generate = require('@babel/generator').default;
const babelParser = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const t = require('@babel/types');
const fs = require('fs').promises;
const REANIMATED_IMPORT_REGEX = /['"]react-native-reanimated['"]/;
const WORKLET_DIRECTIVE_REGEX = /['"]worklet['"]/;
/**
* Adds a dependency array to reanimated hook calls (useAnimatedStyle,
* useDerivedValue, ...) that omit one, collecting every `foo.value` read
* inside the callback. Returns the (possibly rewritten) source.
*
* @param {string} code Original file source.
* @returns {string}
*/
function addMissingDependencyArrays(code) {
const ast = babelParser.parse(code, {
sourceType: 'module',
plugins: ['jsx', 'typescript'],
});
const reanimatedImports = new Set();
traverse(ast, {
ImportDeclaration(path) {
if (path.node.source.value === 'react-native-reanimated') {
for (const spec of path.node.specifiers) {
if (t.isImportSpecifier(spec)) {
reanimatedImports.add(spec.local.name);
}
}
}
},
});
if (reanimatedImports.size === 0) {
return code;
}
traverse(ast, {
CallExpression(path) {
const { node } = path;
const callee = node.callee;
if (!t.isIdentifier(callee) || !reanimatedImports.has(callee.name)) {
return;
}
const callback = node.arguments[0];
if (
!t.isArrowFunctionExpression(callback) &&
!t.isFunctionExpression(callback)
) {
return;
}
const dependencies = new Set();
// Only collect the object identifiers used like foo.value
traverse(
callback.body,
{
MemberExpression(innerPath) {
const innerNode = innerPath.node;
if (
t.isIdentifier(innerNode.object) &&
t.isIdentifier(innerNode.property) &&
innerNode.property.name === 'value'
) {
dependencies.add(innerNode.object.name);
}
},
},
path.scope,
);
if (node.arguments.length < 2 || !t.isArrayExpression(node.arguments[1])) {
const depArray = t.arrayExpression(
Array.from(dependencies).map(name => t.identifier(name)),
);
if (node.arguments.length === 1) {
node.arguments.push(depArray);
} else {
node.arguments[1] = depArray;
}
}
},
});
return generate(ast, {}, code).code;
}
/**
* Runs the official Reanimated babel plugin so that `'worklet'` functions and
* auto-workletized hook callbacks (useAnimatedStyle, useAnimatedGestureHandler,
* Gesture.* callbacks, ...) get their `__workletHash` metadata. Reanimated 3.16+
* throws at runtime on web when a handler is not a worklet, so this must run
* for app code and node_modules alike. Only syntax plugins are enabled - JSX
* and TypeScript are left intact for esbuild to strip.
*
* @param {string} code Source to transform.
* @param {string} filename Absolute path, used for source maps and TSX detection.
* @returns {Promise<string>}
*/
async function workletize(code, filename) {
const isTypeScript = /\.tsx?$/.test(filename);
const result = await babel.transformAsync(code, {
filename,
babelrc: false,
configFile: false,
sourceType: 'module',
compact: false,
retainLines: true,
plugins: [
isTypeScript
? ['@babel/plugin-syntax-typescript', { isTSX: true }]
: '@babel/plugin-syntax-jsx',
'react-native-reanimated/plugin',
],
});
return result.code;
}
module.exports = function reanimatedPlugin() {
return {
name: 'reanimated-plugin',
setup(build) {
build.onLoad({ filter: /\.(js|jsx|ts|tsx)$/ }, async args => {
const code = await fs.readFile(args.path, 'utf8');
const importsReanimated = REANIMATED_IMPORT_REGEX.test(code);
const hasWorklet = WORKLET_DIRECTIVE_REGEX.test(code);
if (!importsReanimated && !hasWorklet) {
return;
}
let _code = code;
if (importsReanimated) {
_code = addMissingDependencyArrays(_code);
}
_code = await workletize(_code, args.path);
return {
contents: _code,
loader: 'tsx',
};
});
},
};
};