@vxrn/compiler 1.25.13 → 1.25.16-1788753284749
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/cache.cjs +3 -1
- package/dist/cjs/configure.cjs +9 -1
- package/dist/cjs/index.cjs +82 -4
- package/dist/cjs/reactNativeCodegen.cjs +140 -0
- package/dist/cjs/reactNativeViewConfig.cjs +489 -0
- package/dist/cjs/transformBabel.cjs +15 -20
- package/dist/cjs/transformBabel.test.cjs +260 -0
- package/dist/cjs/transformHermesLoops.cjs +317 -0
- package/dist/cjs/transformHermesLoops.test.cjs +220 -0
- package/dist/cjs/transformSWC.cjs +102 -137
- package/dist/cjs/transformSWC.test.cjs +74 -0
- package/dist/cjs/transformWorklets.cjs +121 -0
- package/dist/cjs/transformWorklets.test.cjs +604 -0
- package/dist/cjs/worklets/autoworklet.cjs +265 -0
- package/dist/cjs/worklets/globals.cjs +71 -0
- package/dist/cjs/worklets/hash.cjs +38 -0
- package/dist/cjs/worklets/scope.cjs +370 -0
- package/dist/cjs/worklets/serialize.cjs +101 -0
- package/dist/cjs/worklets/transform.cjs +232 -0
- package/dist/cjs/worklets/types.cjs +18 -0
- package/dist/esm/cache.mjs +4 -2
- package/dist/esm/cache.mjs.map +1 -1
- package/dist/esm/configure.mjs +8 -1
- package/dist/esm/configure.mjs.map +1 -1
- package/dist/esm/index.js +72 -5
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.mjs +72 -5
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/reactNativeCodegen.mjs +104 -0
- package/dist/esm/reactNativeCodegen.mjs.map +1 -0
- package/dist/esm/reactNativeViewConfig.mjs +460 -0
- package/dist/esm/reactNativeViewConfig.mjs.map +1 -0
- package/dist/esm/transformBabel.mjs +14 -19
- package/dist/esm/transformBabel.mjs.map +1 -1
- package/dist/esm/transformBabel.test.mjs +261 -1
- package/dist/esm/transformBabel.test.mjs.map +1 -1
- package/dist/esm/transformHermesLoops.mjs +281 -0
- package/dist/esm/transformHermesLoops.mjs.map +1 -0
- package/dist/esm/transformHermesLoops.test.mjs +197 -0
- package/dist/esm/transformHermesLoops.test.mjs.map +1 -0
- package/dist/esm/transformSWC.mjs +89 -138
- package/dist/esm/transformSWC.mjs.map +1 -1
- package/dist/esm/transformSWC.test.mjs +75 -0
- package/dist/esm/transformSWC.test.mjs.map +1 -0
- package/dist/esm/transformWorklets.mjs +81 -0
- package/dist/esm/transformWorklets.mjs.map +1 -0
- package/dist/esm/transformWorklets.test.mjs +581 -0
- package/dist/esm/transformWorklets.test.mjs.map +1 -0
- package/dist/esm/worklets/autoworklet.mjs +234 -0
- package/dist/esm/worklets/autoworklet.mjs.map +1 -0
- package/dist/esm/worklets/globals.mjs +45 -0
- package/dist/esm/worklets/globals.mjs.map +1 -0
- package/dist/esm/worklets/hash.mjs +13 -0
- package/dist/esm/worklets/hash.mjs.map +1 -0
- package/dist/esm/worklets/scope.mjs +345 -0
- package/dist/esm/worklets/scope.mjs.map +1 -0
- package/dist/esm/worklets/serialize.mjs +75 -0
- package/dist/esm/worklets/serialize.mjs.map +1 -0
- package/dist/esm/worklets/transform.mjs +196 -0
- package/dist/esm/worklets/transform.mjs.map +1 -0
- package/dist/esm/worklets/types.mjs +2 -0
- package/dist/esm/worklets/types.mjs.map +1 -0
- package/package.json +10 -6
- package/src/cache.ts +9 -2
- package/src/configure.ts +12 -0
- package/src/index.ts +93 -13
- package/src/reactNativeCodegen.ts +181 -0
- package/src/reactNativeViewConfig.ts +593 -0
- package/src/transformBabel.test.ts +268 -1
- package/src/transformBabel.ts +21 -31
- package/src/transformHermesLoops.test.ts +282 -0
- package/src/transformHermesLoops.ts +433 -0
- package/src/transformSWC.test.ts +88 -0
- package/src/transformSWC.ts +109 -160
- package/src/transformWorklets.test.ts +708 -0
- package/src/transformWorklets.ts +160 -0
- package/src/types.ts +4 -4
- package/src/worklets/autoworklet.ts +391 -0
- package/src/worklets/globals.ts +125 -0
- package/src/worklets/hash.ts +14 -0
- package/src/worklets/scope.ts +401 -0
- package/src/worklets/serialize.ts +114 -0
- package/src/worklets/transform.ts +282 -0
- package/src/worklets/types.ts +26 -0
- package/types/cache.d.ts +1 -0
- package/types/cache.d.ts.map +1 -1
- package/types/configure.d.ts +2 -0
- package/types/configure.d.ts.map +1 -1
- package/types/index.d.ts +3 -0
- package/types/index.d.ts.map +1 -1
- package/types/reactNativeCodegen.d.ts +5 -0
- package/types/reactNativeCodegen.d.ts.map +1 -0
- package/types/reactNativeViewConfig.d.ts +35 -0
- package/types/reactNativeViewConfig.d.ts.map +1 -0
- package/types/transformBabel.d.ts +3 -3
- package/types/transformBabel.d.ts.map +1 -1
- package/types/transformHermesLoops.d.ts +5 -0
- package/types/transformHermesLoops.d.ts.map +1 -0
- package/types/transformHermesLoops.test.d.ts +2 -0
- package/types/transformHermesLoops.test.d.ts.map +1 -0
- package/types/transformSWC.d.ts +12 -3
- package/types/transformSWC.d.ts.map +1 -1
- package/types/transformSWC.test.d.ts +2 -0
- package/types/transformSWC.test.d.ts.map +1 -0
- package/types/transformWorklets.d.ts +22 -0
- package/types/transformWorklets.d.ts.map +1 -0
- package/types/transformWorklets.test.d.ts +2 -0
- package/types/transformWorklets.test.d.ts.map +1 -0
- package/types/types.d.ts +4 -4
- package/types/types.d.ts.map +1 -1
- package/types/worklets/autoworklet.d.ts +14 -0
- package/types/worklets/autoworklet.d.ts.map +1 -0
- package/types/worklets/globals.d.ts +3 -0
- package/types/worklets/globals.d.ts.map +1 -0
- package/types/worklets/hash.d.ts +5 -0
- package/types/worklets/hash.d.ts.map +1 -0
- package/types/worklets/scope.d.ts +7 -0
- package/types/worklets/scope.d.ts.map +1 -0
- package/types/worklets/serialize.d.ts +12 -0
- package/types/worklets/serialize.d.ts.map +1 -0
- package/types/worklets/transform.d.ts +6 -0
- package/types/worklets/transform.d.ts.map +1 -0
- package/types/worklets/types.d.ts +21 -0
- package/types/worklets/types.d.ts.map +1 -0
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all) __defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: true
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
14
|
+
get: () => from[key],
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
21
|
+
value: true
|
|
22
|
+
}), mod);
|
|
23
|
+
var scope_exports = {};
|
|
24
|
+
__export(scope_exports, {
|
|
25
|
+
getClosureVariables: () => getClosureVariables
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(scope_exports);
|
|
28
|
+
function getClosureVariables(fnNode, globals) {
|
|
29
|
+
const rootScope = {
|
|
30
|
+
parent: null,
|
|
31
|
+
isFunction: true,
|
|
32
|
+
bindings: /* @__PURE__ */new Set(),
|
|
33
|
+
varBindings: /* @__PURE__ */new Set()
|
|
34
|
+
};
|
|
35
|
+
let currentScope = rootScope;
|
|
36
|
+
const freeVariables = /* @__PURE__ */new Set();
|
|
37
|
+
if (fnNode.id && fnNode.id.name) {
|
|
38
|
+
rootScope.bindings.add(fnNode.id.name);
|
|
39
|
+
}
|
|
40
|
+
function pushScope(isFunction) {
|
|
41
|
+
const newScope = {
|
|
42
|
+
parent: currentScope,
|
|
43
|
+
isFunction,
|
|
44
|
+
bindings: /* @__PURE__ */new Set(),
|
|
45
|
+
varBindings: isFunction ? /* @__PURE__ */new Set() : currentScope.varBindings
|
|
46
|
+
};
|
|
47
|
+
currentScope = newScope;
|
|
48
|
+
return newScope;
|
|
49
|
+
}
|
|
50
|
+
function popScope() {
|
|
51
|
+
if (currentScope.parent) {
|
|
52
|
+
currentScope = currentScope.parent;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function addBindings(pattern, targetSet) {
|
|
56
|
+
if (!pattern) return;
|
|
57
|
+
switch (pattern.type) {
|
|
58
|
+
case "Identifier":
|
|
59
|
+
targetSet.add(pattern.name);
|
|
60
|
+
break;
|
|
61
|
+
case "AssignmentPattern":
|
|
62
|
+
addBindings(pattern.left, targetSet);
|
|
63
|
+
walk(pattern.right);
|
|
64
|
+
break;
|
|
65
|
+
case "RestElement":
|
|
66
|
+
addBindings(pattern.argument, targetSet);
|
|
67
|
+
break;
|
|
68
|
+
case "ArrayPattern":
|
|
69
|
+
for (const el of pattern.elements) {
|
|
70
|
+
if (el) addBindings(el, targetSet);
|
|
71
|
+
}
|
|
72
|
+
break;
|
|
73
|
+
case "ObjectPattern":
|
|
74
|
+
for (const prop of pattern.properties) {
|
|
75
|
+
if (prop.type === "Property") {
|
|
76
|
+
if (prop.computed) walk(prop.key);
|
|
77
|
+
addBindings(prop.value, targetSet);
|
|
78
|
+
} else if (prop.type === "RestElement") {
|
|
79
|
+
addBindings(prop.argument, targetSet);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function addBindingsOnly(pattern, targetSet) {
|
|
86
|
+
if (!pattern) return;
|
|
87
|
+
switch (pattern.type) {
|
|
88
|
+
case "Identifier":
|
|
89
|
+
targetSet.add(pattern.name);
|
|
90
|
+
break;
|
|
91
|
+
case "AssignmentPattern":
|
|
92
|
+
addBindingsOnly(pattern.left, targetSet);
|
|
93
|
+
break;
|
|
94
|
+
case "RestElement":
|
|
95
|
+
addBindingsOnly(pattern.argument, targetSet);
|
|
96
|
+
break;
|
|
97
|
+
case "ArrayPattern":
|
|
98
|
+
for (const el of pattern.elements) {
|
|
99
|
+
if (el) addBindingsOnly(el, targetSet);
|
|
100
|
+
}
|
|
101
|
+
break;
|
|
102
|
+
case "ObjectPattern":
|
|
103
|
+
for (const prop of pattern.properties) {
|
|
104
|
+
if (prop.type === "Property") {
|
|
105
|
+
addBindingsOnly(prop.value, targetSet);
|
|
106
|
+
} else if (prop.type === "RestElement") {
|
|
107
|
+
addBindingsOnly(prop.argument, targetSet);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function hoistDeclarations(node, direct) {
|
|
114
|
+
if (!node || typeof node !== "object" || typeof node.type !== "string") return;
|
|
115
|
+
switch (node.type) {
|
|
116
|
+
case "FunctionDeclaration":
|
|
117
|
+
if (node.id?.name) {
|
|
118
|
+
currentScope.bindings.add(node.id.name);
|
|
119
|
+
currentScope.varBindings.add(node.id.name);
|
|
120
|
+
}
|
|
121
|
+
return;
|
|
122
|
+
case "FunctionExpression":
|
|
123
|
+
case "ArrowFunctionExpression":
|
|
124
|
+
return;
|
|
125
|
+
case "ClassDeclaration":
|
|
126
|
+
if (direct && node.id?.name) currentScope.bindings.add(node.id.name);
|
|
127
|
+
return;
|
|
128
|
+
case "VariableDeclaration":
|
|
129
|
+
if (node.kind === "var") {
|
|
130
|
+
for (const d of node.declarations) addBindingsOnly(d.id, currentScope.varBindings);
|
|
131
|
+
} else if (direct) {
|
|
132
|
+
for (const d of node.declarations) addBindingsOnly(d.id, currentScope.bindings);
|
|
133
|
+
}
|
|
134
|
+
return;
|
|
135
|
+
case "ExportNamedDeclaration":
|
|
136
|
+
case "ExportDefaultDeclaration":
|
|
137
|
+
hoistDeclarations(node.declaration, direct);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
for (const key of Object.keys(node)) {
|
|
141
|
+
if (key === "start" || key === "end" || key === "loc" || key === "range") continue;
|
|
142
|
+
const child = node[key];
|
|
143
|
+
if (Array.isArray(child)) {
|
|
144
|
+
for (const item of child) hoistDeclarations(item, false);
|
|
145
|
+
} else if (child && typeof child === "object") {
|
|
146
|
+
hoistDeclarations(child, false);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (fnNode.params) {
|
|
151
|
+
for (const param of fnNode.params) {
|
|
152
|
+
addBindings(param, rootScope.bindings);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function isDeclaredInScopes(name, fromScope) {
|
|
156
|
+
let s = fromScope;
|
|
157
|
+
while (s) {
|
|
158
|
+
if (s.bindings.has(name) || s.varBindings.has(name)) {
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
s = s.parent;
|
|
162
|
+
}
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
function resolveIdentifier(name) {
|
|
166
|
+
if (!isDeclaredInScopes(name, currentScope) && !globals.has(name)) {
|
|
167
|
+
freeVariables.add(name);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function walk(node) {
|
|
171
|
+
if (!node || typeof node !== "object") return;
|
|
172
|
+
if (node.type === "TSTypeAnnotation" || node.type === "TSTypeReference" || node.type === "TSTypeAliasDeclaration" || node.type === "TSInterfaceDeclaration" || node.type === "TSTypeParameterDeclaration" || node.type === "TSTypeParameterInstantiation" || node.type === "TSTypeQuery" || node.type === "TSQualifiedName") {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
if (node.type === "TSAsExpression" || node.type === "TSSatisfiesExpression" || node.type === "TSTypeAssertion" || node.type === "TSNonNullExpression" || node.type === "TSInstantiationExpression") {
|
|
176
|
+
walk(node.expression);
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
switch (node.type) {
|
|
180
|
+
case "BlockStatement":
|
|
181
|
+
{
|
|
182
|
+
const isRootBody = node === fnNode.body;
|
|
183
|
+
if (!isRootBody) {
|
|
184
|
+
pushScope(false);
|
|
185
|
+
}
|
|
186
|
+
for (const stmt of node.body) {
|
|
187
|
+
hoistDeclarations(stmt, true);
|
|
188
|
+
}
|
|
189
|
+
for (const stmt of node.body) {
|
|
190
|
+
walk(stmt);
|
|
191
|
+
}
|
|
192
|
+
if (!isRootBody) {
|
|
193
|
+
popScope();
|
|
194
|
+
}
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
case "VariableDeclaration":
|
|
198
|
+
{
|
|
199
|
+
const isVar = node.kind === "var";
|
|
200
|
+
const targetSet = isVar ? currentScope.varBindings : currentScope.bindings;
|
|
201
|
+
for (const decl of node.declarations) {
|
|
202
|
+
addBindings(decl.id, targetSet);
|
|
203
|
+
if (decl.init) walk(decl.init);
|
|
204
|
+
}
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
case "FunctionDeclaration":
|
|
208
|
+
case "FunctionExpression":
|
|
209
|
+
{
|
|
210
|
+
if (node.id && node.id.name) {
|
|
211
|
+
currentScope.bindings.add(node.id.name);
|
|
212
|
+
}
|
|
213
|
+
pushScope(true);
|
|
214
|
+
if (node.params) {
|
|
215
|
+
for (const param of node.params) {
|
|
216
|
+
addBindings(param, currentScope.bindings);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
walk(node.body);
|
|
220
|
+
popScope();
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
case "ArrowFunctionExpression":
|
|
224
|
+
{
|
|
225
|
+
pushScope(true);
|
|
226
|
+
if (node.params) {
|
|
227
|
+
for (const param of node.params) {
|
|
228
|
+
addBindings(param, currentScope.bindings);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
walk(node.body);
|
|
232
|
+
popScope();
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
case "CatchClause":
|
|
236
|
+
{
|
|
237
|
+
pushScope(false);
|
|
238
|
+
if (node.param) {
|
|
239
|
+
addBindings(node.param, currentScope.bindings);
|
|
240
|
+
}
|
|
241
|
+
walk(node.body);
|
|
242
|
+
popScope();
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
case "ForStatement":
|
|
246
|
+
{
|
|
247
|
+
pushScope(false);
|
|
248
|
+
if (node.init) walk(node.init);
|
|
249
|
+
if (node.test) walk(node.test);
|
|
250
|
+
if (node.update) walk(node.update);
|
|
251
|
+
walk(node.body);
|
|
252
|
+
popScope();
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
case "ForInStatement":
|
|
256
|
+
case "ForOfStatement":
|
|
257
|
+
{
|
|
258
|
+
pushScope(false);
|
|
259
|
+
walk(node.left);
|
|
260
|
+
walk(node.right);
|
|
261
|
+
walk(node.body);
|
|
262
|
+
popScope();
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
case "ClassDeclaration":
|
|
266
|
+
case "ClassExpression":
|
|
267
|
+
{
|
|
268
|
+
if (node.id && node.id.name) {
|
|
269
|
+
currentScope.bindings.add(node.id.name);
|
|
270
|
+
}
|
|
271
|
+
if (node.superClass) walk(node.superClass);
|
|
272
|
+
pushScope(false);
|
|
273
|
+
walk(node.body);
|
|
274
|
+
popScope();
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
case "MemberExpression":
|
|
278
|
+
{
|
|
279
|
+
walk(node.object);
|
|
280
|
+
if (node.computed) {
|
|
281
|
+
walk(node.property);
|
|
282
|
+
}
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
case "Property":
|
|
286
|
+
{
|
|
287
|
+
if (node.computed) {
|
|
288
|
+
walk(node.key);
|
|
289
|
+
}
|
|
290
|
+
walk(node.value);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
case "MethodDefinition":
|
|
294
|
+
{
|
|
295
|
+
if (node.computed) {
|
|
296
|
+
walk(node.key);
|
|
297
|
+
}
|
|
298
|
+
walk(node.value);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
case "Identifier":
|
|
302
|
+
{
|
|
303
|
+
resolveIdentifier(node.name);
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
case "JSXElement":
|
|
307
|
+
{
|
|
308
|
+
if (node.openingElement) {
|
|
309
|
+
const nameNode = node.openingElement.name;
|
|
310
|
+
if (nameNode.type === "JSXIdentifier") {
|
|
311
|
+
if (/^[A-Z]/.test(nameNode.name)) {
|
|
312
|
+
resolveIdentifier(nameNode.name);
|
|
313
|
+
}
|
|
314
|
+
} else if (nameNode.type === "JSXMemberExpression") {
|
|
315
|
+
let root = nameNode.object;
|
|
316
|
+
while (root.type === "JSXMemberExpression") {
|
|
317
|
+
root = root.object;
|
|
318
|
+
}
|
|
319
|
+
if (root.type === "JSXIdentifier") {
|
|
320
|
+
resolveIdentifier(root.name);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (node.openingElement.attributes) {
|
|
324
|
+
for (const attr of node.openingElement.attributes) {
|
|
325
|
+
walk(attr);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
if (node.children) {
|
|
330
|
+
for (const child of node.children) {
|
|
331
|
+
walk(child);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
case "JSXAttribute":
|
|
337
|
+
{
|
|
338
|
+
if (node.value) {
|
|
339
|
+
walk(node.value);
|
|
340
|
+
}
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
case "JSXExpressionContainer":
|
|
344
|
+
{
|
|
345
|
+
walk(node.expression);
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
case "BreakStatement":
|
|
349
|
+
case "ContinueStatement":
|
|
350
|
+
case "LabeledStatement":
|
|
351
|
+
{
|
|
352
|
+
if (node.body) walk(node.body);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
for (const key of Object.keys(node)) {
|
|
357
|
+
if (key === "start" || key === "end" || key === "loc" || key === "range") continue;
|
|
358
|
+
const child = node[key];
|
|
359
|
+
if (Array.isArray(child)) {
|
|
360
|
+
for (const item of child) {
|
|
361
|
+
walk(item);
|
|
362
|
+
}
|
|
363
|
+
} else if (child && typeof child === "object") {
|
|
364
|
+
walk(child);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
walk(fnNode.body);
|
|
369
|
+
return Array.from(freeVariables).sort();
|
|
370
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all) __defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: true
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
14
|
+
get: () => from[key],
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
21
|
+
value: true
|
|
22
|
+
}), mod);
|
|
23
|
+
var serialize_exports = {};
|
|
24
|
+
__export(serialize_exports, {
|
|
25
|
+
buildLocalFunction: () => buildLocalFunction,
|
|
26
|
+
serializeWorkletForUI: () => serializeWorkletForUI
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(serialize_exports);
|
|
29
|
+
var import_oxc_transform = require("oxc-transform");
|
|
30
|
+
var import_autoworklet = require("./autoworklet.cjs");
|
|
31
|
+
function assertTransformed(transformed, name, what) {
|
|
32
|
+
const error = transformed.errors?.[0];
|
|
33
|
+
if (error) {
|
|
34
|
+
throw new Error(`[worklets] failed to build ${what} code for ${name || "anonymous worklet"}: ${error.message || error}`);
|
|
35
|
+
}
|
|
36
|
+
if (!transformed.code.trim()) {
|
|
37
|
+
throw new Error(`[worklets] built empty ${what} code for ${name || "anonymous worklet"}`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function serializeWorkletForUI(fnNode, code, name, closureVars) {
|
|
41
|
+
let rawParams = "";
|
|
42
|
+
if (fnNode.params && fnNode.params.length > 0) {
|
|
43
|
+
const pStart = fnNode.params[0].start;
|
|
44
|
+
const pEnd = fnNode.params[fnNode.params.length - 1].end;
|
|
45
|
+
rawParams = code.slice(pStart, pEnd);
|
|
46
|
+
}
|
|
47
|
+
let rawBody = "";
|
|
48
|
+
if (fnNode.body.type === "BlockStatement") {
|
|
49
|
+
const bodyStart = (0, import_autoworklet.bodyStartAfterDirectives)(fnNode, code);
|
|
50
|
+
const bodyEnd = fnNode.body.end - 1;
|
|
51
|
+
rawBody = code.slice(bodyStart, bodyEnd);
|
|
52
|
+
} else {
|
|
53
|
+
rawBody = `return ${code.slice(fnNode.body.start, fnNode.body.end)};`;
|
|
54
|
+
}
|
|
55
|
+
const unpacker = closureVars.length > 0 ? `const { ${closureVars.join(", ")} } = this.__closure ?? this._closure;
|
|
56
|
+
` : "";
|
|
57
|
+
const asyncPrefix = fnNode.async ? "async " : "";
|
|
58
|
+
const genPrefix = fnNode.generator ? "*" : "";
|
|
59
|
+
const fnName = name || "_worklet";
|
|
60
|
+
const fullFn = `${asyncPrefix}function${genPrefix} ${fnName}(${rawParams}) {
|
|
61
|
+
${unpacker}${rawBody}
|
|
62
|
+
}`;
|
|
63
|
+
const transformed = (0, import_oxc_transform.transformSync)("worklet.ts", fullFn);
|
|
64
|
+
assertTransformed(transformed, name, "serialized");
|
|
65
|
+
return transformed.code.trim();
|
|
66
|
+
}
|
|
67
|
+
function buildLocalFunction(fnNode, code, name) {
|
|
68
|
+
let rawParams = "";
|
|
69
|
+
if (fnNode.params && fnNode.params.length > 0) {
|
|
70
|
+
const pStart = fnNode.params[0].start;
|
|
71
|
+
const pEnd = fnNode.params[fnNode.params.length - 1].end;
|
|
72
|
+
rawParams = code.slice(pStart, pEnd);
|
|
73
|
+
}
|
|
74
|
+
let rawBody = "";
|
|
75
|
+
if (fnNode.body.type === "BlockStatement") {
|
|
76
|
+
const bodyStart = (0, import_autoworklet.bodyStartAfterDirectives)(fnNode, code);
|
|
77
|
+
const bodyEnd = fnNode.body.end - 1;
|
|
78
|
+
rawBody = code.slice(bodyStart, bodyEnd);
|
|
79
|
+
} else {
|
|
80
|
+
rawBody = code.slice(fnNode.body.start, fnNode.body.end);
|
|
81
|
+
}
|
|
82
|
+
let rawFn = "";
|
|
83
|
+
const asyncPrefix = fnNode.async ? "async " : "";
|
|
84
|
+
const genPrefix = fnNode.generator ? "*" : "";
|
|
85
|
+
if (fnNode.type === "ArrowFunctionExpression") {
|
|
86
|
+
if (fnNode.body.type === "BlockStatement") {
|
|
87
|
+
rawFn = `${asyncPrefix}(${rawParams}) => {
|
|
88
|
+
${rawBody}
|
|
89
|
+
}`;
|
|
90
|
+
} else {
|
|
91
|
+
rawFn = `${asyncPrefix}(${rawParams}) => (${rawBody})`;
|
|
92
|
+
}
|
|
93
|
+
} else {
|
|
94
|
+
rawFn = `${asyncPrefix}function${genPrefix} ${name || "_worklet"}(${rawParams}) {
|
|
95
|
+
${rawBody}
|
|
96
|
+
}`;
|
|
97
|
+
}
|
|
98
|
+
const transformed = (0, import_oxc_transform.transformSync)("local.ts", rawFn);
|
|
99
|
+
assertTransformed(transformed, name, "local");
|
|
100
|
+
return transformed.code.trim().replace(/;$/, "");
|
|
101
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all) __defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: true
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
16
|
+
get: () => from[key],
|
|
17
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
23
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
24
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
25
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
26
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
27
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
28
|
+
value: mod,
|
|
29
|
+
enumerable: true
|
|
30
|
+
}) : target, mod));
|
|
31
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
32
|
+
value: true
|
|
33
|
+
}), mod);
|
|
34
|
+
var transform_exports = {};
|
|
35
|
+
__export(transform_exports, {
|
|
36
|
+
executeWorkletTransform: () => executeWorkletTransform
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(transform_exports);
|
|
39
|
+
var import_node_path = __toESM(require("node:path"), 1);
|
|
40
|
+
var import_magic_string = __toESM(require("magic-string"), 1);
|
|
41
|
+
var import_remapping = __toESM(require("@jridgewell/remapping"), 1);
|
|
42
|
+
var import_oxc_parser = require("oxc-parser");
|
|
43
|
+
var import_autoworklet = require("./autoworklet.cjs");
|
|
44
|
+
var import_globals = require("./globals.cjs");
|
|
45
|
+
var import_hash = require("./hash.cjs");
|
|
46
|
+
var import_scope = require("./scope.cjs");
|
|
47
|
+
var import_serialize = require("./serialize.cjs");
|
|
48
|
+
const FUNCTION_TYPES = /* @__PURE__ */new Set(["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"]);
|
|
49
|
+
function findEnclosingFunction(program, node) {
|
|
50
|
+
let best = void 0;
|
|
51
|
+
const walk = n => {
|
|
52
|
+
if (!n || typeof n !== "object") return;
|
|
53
|
+
if (Array.isArray(n)) {
|
|
54
|
+
for (const c of n) walk(c);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (typeof n.type !== "string") return;
|
|
58
|
+
if (typeof n.start === "number" && (n.start > node.start || n.end < node.end)) return;
|
|
59
|
+
if (FUNCTION_TYPES.has(n.type) && n !== node && n.body?.type === "BlockStatement" && n.body.start < node.start && n.body.end > node.end && (!best || n.body.start > best.body.start)) {
|
|
60
|
+
best = n;
|
|
61
|
+
}
|
|
62
|
+
for (const key in n) {
|
|
63
|
+
if (key === "parent") continue;
|
|
64
|
+
walk(n[key]);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
walk(program);
|
|
68
|
+
return best;
|
|
69
|
+
}
|
|
70
|
+
function executeWorkletTransform(id, code, sourceMaps = false, options, resolveVersionFn) {
|
|
71
|
+
const cleanId = id.split("?")[0];
|
|
72
|
+
const lang = /\.[cm]?ts$/.test(cleanId) ? "ts" : cleanId.endsWith(".tsx") ? "tsx" : "jsx";
|
|
73
|
+
const initialParse = (0, import_oxc_parser.parseSync)(cleanId, code, {
|
|
74
|
+
sourceType: "module",
|
|
75
|
+
lang
|
|
76
|
+
});
|
|
77
|
+
if (initialParse.errors && initialParse.errors.length > 0) {
|
|
78
|
+
const err = initialParse.errors[0];
|
|
79
|
+
throw new Error(err.codeframe || err.message || "Syntax Error while parsing worklet");
|
|
80
|
+
}
|
|
81
|
+
const initialCandidates = (0, import_autoworklet.findWorkletCandidates)(initialParse.program);
|
|
82
|
+
if (initialCandidates.length === 0) {
|
|
83
|
+
let map2 = void 0;
|
|
84
|
+
if (sourceMaps) {
|
|
85
|
+
const ms = new import_magic_string.default(code);
|
|
86
|
+
map2 = ms.generateMap({
|
|
87
|
+
source: cleanId,
|
|
88
|
+
file: cleanId,
|
|
89
|
+
hires: true,
|
|
90
|
+
includeContent: true
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
code,
|
|
95
|
+
map: map2
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const pluginVersion = options?.pluginVersion || (resolveVersionFn ? resolveVersionFn(options?.projectRoot, cleanId) : "0.0.0");
|
|
99
|
+
const globals = (0, import_globals.createGlobalsSet)(options?.globals, options?.strictGlobal);
|
|
100
|
+
let location = cleanId;
|
|
101
|
+
if (options?.relativeSourceLocation && options?.projectRoot) {
|
|
102
|
+
location = import_node_path.default.relative(options.projectRoot, cleanId).replace(/\\/g, "/");
|
|
103
|
+
}
|
|
104
|
+
let currentCode = code;
|
|
105
|
+
const initDataDefs = /* @__PURE__ */new Map();
|
|
106
|
+
const passMaps = [];
|
|
107
|
+
let remainingPassBudget = 100;
|
|
108
|
+
while (remainingPassBudget-- > 0) {
|
|
109
|
+
const parseResult = (0, import_oxc_parser.parseSync)(cleanId, currentCode, {
|
|
110
|
+
sourceType: "module",
|
|
111
|
+
lang
|
|
112
|
+
});
|
|
113
|
+
if (parseResult.errors && parseResult.errors.length > 0) {
|
|
114
|
+
const err = parseResult.errors[0];
|
|
115
|
+
throw new Error(err.codeframe || err.message || "Syntax Error while parsing worklet");
|
|
116
|
+
}
|
|
117
|
+
const candidates = (0, import_autoworklet.findWorkletCandidates)(parseResult.program);
|
|
118
|
+
if (candidates.length === 0) {
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
const innermostCandidates = candidates.filter(c => {
|
|
122
|
+
return !candidates.some(other => other !== c && other.fnNode.start >= c.fnNode.start && other.fnNode.end <= c.fnNode.end);
|
|
123
|
+
});
|
|
124
|
+
if (innermostCandidates.length === 0) {
|
|
125
|
+
throw new Error(`[worklets] Cyclic or unresolvable worklet nesting detected in ${cleanId}`);
|
|
126
|
+
}
|
|
127
|
+
const ms = new import_magic_string.default(currentCode);
|
|
128
|
+
const inlinedInitData = /* @__PURE__ */new Map();
|
|
129
|
+
for (const candidate of innermostCandidates) {
|
|
130
|
+
const closureVars = (0, import_autoworklet.hasDirective)(candidate.fnNode, "no-worklet-closure") ? [] : (0, import_scope.getClosureVariables)(candidate.fnNode, globals);
|
|
131
|
+
const fnName = candidate.name;
|
|
132
|
+
const serializedCode = (0, import_serialize.serializeWorkletForUI)(candidate.fnNode, currentCode, fnName, closureVars);
|
|
133
|
+
const workletHash = (0, import_hash.calculateWorkletHash)(serializedCode);
|
|
134
|
+
const initDataVar = `_worklet_${workletHash}_init_data`;
|
|
135
|
+
const initDataDecl = `var ${initDataVar} = {
|
|
136
|
+
code: ${JSON.stringify(serializedCode)},
|
|
137
|
+
location: ${JSON.stringify(location)}
|
|
138
|
+
};`;
|
|
139
|
+
const enclosingFn = (0, import_autoworklet.hasDirective)(candidate.fnNode, "limit-init-data-hoisting") ? findEnclosingFunction(parseResult.program, candidate.fnNode) : void 0;
|
|
140
|
+
if (enclosingFn) {
|
|
141
|
+
const insertAt = (0, import_autoworklet.bodyStartAfterDirectives)(enclosingFn, currentCode);
|
|
142
|
+
let declared = inlinedInitData.get(insertAt);
|
|
143
|
+
if (!declared) {
|
|
144
|
+
declared = /* @__PURE__ */new Set();
|
|
145
|
+
inlinedInitData.set(insertAt, declared);
|
|
146
|
+
}
|
|
147
|
+
if (!declared.has(initDataVar)) {
|
|
148
|
+
declared.add(initDataVar);
|
|
149
|
+
ms.appendLeft(insertAt, `
|
|
150
|
+
${initDataDecl}
|
|
151
|
+
`);
|
|
152
|
+
}
|
|
153
|
+
} else if (!initDataDefs.has(initDataVar)) {
|
|
154
|
+
initDataDefs.set(initDataVar, initDataDecl);
|
|
155
|
+
}
|
|
156
|
+
const localFnCode = (0, import_serialize.buildLocalFunction)(candidate.fnNode, currentCode, fnName);
|
|
157
|
+
const localName = fnName || "_worklet";
|
|
158
|
+
const closureProps = closureVars.map(v => `${v}: ${v}`).join(", ");
|
|
159
|
+
const iife = `(function () {
|
|
160
|
+
var _e = [new (typeof global !== 'undefined' ? global : globalThis).Error(), 1, -27];
|
|
161
|
+
var ${localName} = ${localFnCode};
|
|
162
|
+
${localName}.__closure = { ${closureProps} };
|
|
163
|
+
${localName}.__workletHash = ${workletHash};
|
|
164
|
+
${localName}.__pluginVersion = ${JSON.stringify(pluginVersion)};
|
|
165
|
+
${localName}.__initData = ${initDataVar};
|
|
166
|
+
${localName}.__stackDetails = _e;
|
|
167
|
+
return ${localName};
|
|
168
|
+
})()`;
|
|
169
|
+
if (candidate.kind === "function_declaration") {
|
|
170
|
+
if (candidate.parent?.type === "ExportNamedDeclaration") {
|
|
171
|
+
ms.overwrite(candidate.parent.start, candidate.parent.end, `export var ${candidate.name} = ${iife};`);
|
|
172
|
+
} else if (candidate.parent?.type === "ExportDefaultDeclaration") {
|
|
173
|
+
ms.overwrite(candidate.parent.start, candidate.parent.end, `var ${candidate.name || "_defaultWorklet"} = ${iife};
|
|
174
|
+
export default ${candidate.name || "_defaultWorklet"};`);
|
|
175
|
+
} else {
|
|
176
|
+
ms.overwrite(candidate.node.start, candidate.node.end, `var ${candidate.name} = ${iife};`);
|
|
177
|
+
}
|
|
178
|
+
} else if (candidate.kind === "object_method") {
|
|
179
|
+
ms.overwrite(candidate.node.start, candidate.node.end, `${candidate.name}: ${iife}`);
|
|
180
|
+
} else {
|
|
181
|
+
ms.overwrite(candidate.node.start, candidate.node.end, iife);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (sourceMaps) {
|
|
185
|
+
passMaps.push(ms.generateMap({
|
|
186
|
+
source: cleanId,
|
|
187
|
+
file: cleanId,
|
|
188
|
+
hires: true,
|
|
189
|
+
includeContent: true
|
|
190
|
+
}));
|
|
191
|
+
}
|
|
192
|
+
currentCode = ms.toString();
|
|
193
|
+
}
|
|
194
|
+
const finalCheck = (0, import_oxc_parser.parseSync)(cleanId, currentCode, {
|
|
195
|
+
sourceType: "module",
|
|
196
|
+
lang
|
|
197
|
+
});
|
|
198
|
+
const remaining = (0, import_autoworklet.findWorkletCandidates)(finalCheck.program);
|
|
199
|
+
if (remaining.length > 0) {
|
|
200
|
+
throw new Error(`[worklets] Exceeded maximum pass budget while transforming worklets in ${cleanId}. ${remaining.length} worklet(s) remained untransformed.`);
|
|
201
|
+
}
|
|
202
|
+
if (initDataDefs.size > 0) {
|
|
203
|
+
const initDataCode = Array.from(initDataDefs.values()).join("\n") + "\n";
|
|
204
|
+
const msPrepend = new import_magic_string.default(currentCode);
|
|
205
|
+
msPrepend.prepend(initDataCode);
|
|
206
|
+
if (sourceMaps) {
|
|
207
|
+
passMaps.push(msPrepend.generateMap({
|
|
208
|
+
source: cleanId,
|
|
209
|
+
file: cleanId,
|
|
210
|
+
hires: true,
|
|
211
|
+
includeContent: true
|
|
212
|
+
}));
|
|
213
|
+
}
|
|
214
|
+
currentCode = msPrepend.toString();
|
|
215
|
+
}
|
|
216
|
+
let map = void 0;
|
|
217
|
+
if (sourceMaps && passMaps.length > 0) {
|
|
218
|
+
if (passMaps.length === 1) {
|
|
219
|
+
map = passMaps[0];
|
|
220
|
+
} else {
|
|
221
|
+
map = (0, import_remapping.default)(passMaps.slice().reverse(), () => null);
|
|
222
|
+
}
|
|
223
|
+
if (map) {
|
|
224
|
+
map.sources = [cleanId];
|
|
225
|
+
map.file = cleanId;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
code: currentCode,
|
|
230
|
+
map
|
|
231
|
+
};
|
|
232
|
+
}
|