@vxrn/compiler 2.0.0-beta.33.1 → 2.0.0-beta.45.1

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.
Files changed (94) hide show
  1. package/dist/cjs/cache.cjs +112 -109
  2. package/dist/cjs/configure.cjs +26 -28
  3. package/dist/cjs/constants.cjs +56 -60
  4. package/dist/cjs/index.cjs +420 -438
  5. package/dist/cjs/reactNativeCodegen.cjs +105 -114
  6. package/dist/cjs/reactNativeViewConfig.cjs +362 -381
  7. package/dist/cjs/refresh-runtime.cjs +390 -397
  8. package/dist/cjs/transformBabel.cjs +278 -218
  9. package/dist/cjs/transformHermesAsync.cjs +36 -47
  10. package/dist/cjs/transformHermesLoops.cjs +279 -281
  11. package/dist/cjs/transformSWC.cjs +183 -200
  12. package/dist/cjs/transformWorklets.cjs +107 -95
  13. package/dist/cjs/types.cjs +9 -11
  14. package/dist/cjs/worklets/autoworklet.cjs +277 -222
  15. package/dist/cjs/worklets/globals.cjs +124 -60
  16. package/dist/cjs/worklets/hash.cjs +23 -27
  17. package/dist/cjs/worklets/scope.cjs +338 -359
  18. package/dist/cjs/worklets/serialize.cjs +73 -75
  19. package/dist/cjs/worklets/transform.cjs +217 -222
  20. package/dist/cjs/worklets/types.cjs +9 -11
  21. package/dist/esm/cache.mjs +96 -89
  22. package/dist/esm/cache.mjs.map +1 -1
  23. package/dist/esm/configure.mjs +11 -10
  24. package/dist/esm/configure.mjs.map +1 -1
  25. package/dist/esm/constants.mjs +41 -41
  26. package/dist/esm/constants.mjs.map +1 -1
  27. package/dist/esm/index.js +417 -420
  28. package/dist/esm/index.js.map +1 -1
  29. package/dist/esm/index.mjs +417 -420
  30. package/dist/esm/index.mjs.map +1 -1
  31. package/dist/esm/reactNativeCodegen.mjs +90 -88
  32. package/dist/esm/reactNativeCodegen.mjs.map +1 -1
  33. package/dist/esm/reactNativeViewConfig.mjs +347 -362
  34. package/dist/esm/reactNativeViewConfig.mjs.map +1 -1
  35. package/dist/esm/refresh-runtime.mjs +372 -376
  36. package/dist/esm/refresh-runtime.mjs.map +1 -1
  37. package/dist/esm/transformBabel.mjs +261 -194
  38. package/dist/esm/transformBabel.mjs.map +1 -1
  39. package/dist/esm/transformHermesAsync.mjs +20 -21
  40. package/dist/esm/transformHermesAsync.mjs.map +1 -1
  41. package/dist/esm/transformHermesLoops.mjs +264 -255
  42. package/dist/esm/transformHermesLoops.mjs.map +1 -1
  43. package/dist/esm/transformSWC.mjs +163 -171
  44. package/dist/esm/transformSWC.mjs.map +1 -1
  45. package/dist/esm/transformWorklets.mjs +89 -69
  46. package/dist/esm/transformWorklets.mjs.map +1 -1
  47. package/dist/esm/types.mjs +0 -2
  48. package/dist/esm/worklets/autoworklet.mjs +258 -200
  49. package/dist/esm/worklets/autoworklet.mjs.map +1 -1
  50. package/dist/esm/worklets/globals.mjs +109 -42
  51. package/dist/esm/worklets/globals.mjs.map +1 -1
  52. package/dist/esm/worklets/hash.mjs +10 -9
  53. package/dist/esm/worklets/hash.mjs.map +1 -1
  54. package/dist/esm/worklets/scope.mjs +325 -341
  55. package/dist/esm/worklets/scope.mjs.map +1 -1
  56. package/dist/esm/worklets/serialize.mjs +60 -58
  57. package/dist/esm/worklets/serialize.mjs.map +1 -1
  58. package/dist/esm/worklets/transform.mjs +201 -197
  59. package/dist/esm/worklets/transform.mjs.map +1 -1
  60. package/dist/esm/worklets/types.mjs +0 -2
  61. package/package.json +7 -6
  62. package/src/cache.ts +37 -7
  63. package/src/index.ts +33 -18
  64. package/src/transformBabel.test.ts +311 -1
  65. package/src/transformBabel.ts +110 -13
  66. package/types/cache.d.ts +2 -2
  67. package/types/cache.d.ts.map +1 -1
  68. package/types/index.d.ts.map +1 -1
  69. package/types/transformBabel.d.ts +16 -1
  70. package/types/transformBabel.d.ts.map +1 -1
  71. package/types/transformSWC.d.ts +1 -4
  72. package/types/transformSWC.d.ts.map +1 -1
  73. package/dist/cjs/transformBabel.test.cjs +0 -360
  74. package/dist/cjs/transformHermesLoops.test.cjs +0 -257
  75. package/dist/cjs/transformSWC.test.cjs +0 -117
  76. package/dist/cjs/transformWorklets.test.cjs +0 -604
  77. package/dist/esm/transformBabel.test.mjs +0 -337
  78. package/dist/esm/transformBabel.test.mjs.map +0 -1
  79. package/dist/esm/transformHermesLoops.test.mjs +0 -234
  80. package/dist/esm/transformHermesLoops.test.mjs.map +0 -1
  81. package/dist/esm/transformSWC.test.mjs +0 -118
  82. package/dist/esm/transformSWC.test.mjs.map +0 -1
  83. package/dist/esm/transformWorklets.test.mjs +0 -581
  84. package/dist/esm/transformWorklets.test.mjs.map +0 -1
  85. package/dist/esm/types.mjs.map +0 -1
  86. package/dist/esm/worklets/types.mjs.map +0 -1
  87. package/types/transformBabel.test.d.ts +0 -2
  88. package/types/transformBabel.test.d.ts.map +0 -1
  89. package/types/transformHermesLoops.test.d.ts +0 -2
  90. package/types/transformHermesLoops.test.d.ts.map +0 -1
  91. package/types/transformSWC.test.d.ts +0 -2
  92. package/types/transformSWC.test.d.ts.map +0 -1
  93. package/types/transformWorklets.test.d.ts +0 -2
  94. package/types/transformWorklets.test.d.ts.map +0 -1
@@ -1,604 +0,0 @@
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 __copyProps = (to, from, except, desc) => {
8
- if (from && typeof from === "object" || typeof from === "function") {
9
- for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
10
- get: () => from[key],
11
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
12
- });
13
- }
14
- return to;
15
- };
16
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
17
- // If the importer is in node compatibility mode or this is not an ESM
18
- // file that has been converted to a CommonJS file using a Babel-
19
- // compatible transform (i.e. "__esModule" has not been set), then set
20
- // "default" to the CommonJS "module.exports" for node compatibility.
21
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
22
- value: mod,
23
- enumerable: true
24
- }) : target, mod));
25
- var import_node_fs = __toESM(require("node:fs"), 1);
26
- var import_node_os = __toESM(require("node:os"), 1);
27
- var import_node_path = __toESM(require("node:path"), 1);
28
- var import_vitest = require("vitest");
29
- var import_configure = require("./configure.cjs");
30
- var import_transformWorklets = require("./transformWorklets.cjs");
31
- (0, import_vitest.afterEach)(() => {
32
- (0, import_configure.configureVXRNCompilerPlugin)({
33
- enableReanimated: false,
34
- enableNativeWorklets: false
35
- });
36
- });
37
- (0, import_vitest.describe)("shouldTransformWorklets", () => {
38
- (0, import_vitest.it)("returns false when enableReanimated is false", () => {
39
- (0, import_configure.configureVXRNCompilerPlugin)({
40
- enableReanimated: false,
41
- enableNativeWorklets: true
42
- });
43
- (0, import_vitest.expect)((0, import_transformWorklets.shouldTransformWorklets)({
44
- id: "/app/index.tsx",
45
- code: `export const foo = () => { 'worklet'; return 1; }`
46
- })).toBe(false);
47
- });
48
- (0, import_vitest.it)("returns false by default when enableNativeWorklets is false even if enableReanimated is true", () => {
49
- (0, import_configure.configureVXRNCompilerPlugin)({
50
- enableReanimated: true,
51
- enableNativeWorklets: false
52
- });
53
- (0, import_vitest.expect)((0, import_transformWorklets.shouldTransformWorklets)({
54
- id: "/app/index.tsx",
55
- code: `export const foo = () => { 'worklet'; return 1; }`
56
- })).toBe(false);
57
- });
58
- (0, import_vitest.it)("returns true when both enableReanimated and enableNativeWorklets are true and worklet keyword is present", () => {
59
- (0, import_configure.configureVXRNCompilerPlugin)({
60
- enableReanimated: true,
61
- enableNativeWorklets: true
62
- });
63
- (0, import_vitest.expect)((0, import_transformWorklets.shouldTransformWorklets)({
64
- id: "/app/index.tsx",
65
- code: `export const foo = () => { 'worklet'; return 1; }`
66
- })).toBe(true);
67
- });
68
- (0, import_vitest.it)("returns false for ignored paths such as react-native internals", () => {
69
- (0, import_configure.configureVXRNCompilerPlugin)({
70
- enableReanimated: true,
71
- enableNativeWorklets: true
72
- });
73
- (0, import_vitest.expect)((0, import_transformWorklets.shouldTransformWorklets)({
74
- id: "/project/node_modules/react-native/Libraries/StyleSheet/index.js",
75
- code: `export const foo = () => { 'worklet'; return 1; }`
76
- })).toBe(false);
77
- });
78
- });
79
- (0, import_vitest.describe)("transformWorklets", () => {
80
- (0, import_vitest.it)("transforms functions with worklet directive", async () => {
81
- const code2 = `
82
- export function animate() {
83
- 'worklet'
84
- return 42
85
- }
86
- `;
87
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/animate.ts", code2, false);
88
- (0, import_vitest.expect)(result2.code).toContain("__workletHash");
89
- (0, import_vitest.expect)(result2.code).toContain("__initData");
90
- (0, import_vitest.expect)(result2.code).toContain("__closure");
91
- (0, import_vitest.expect)(result2.map).toBeUndefined();
92
- });
93
- (0, import_vitest.it)("ignores calls whose callee name collides with Object.prototype", async () => {
94
- const code2 = `
95
- const out = value.constructor(1)
96
- const s = thing.toString()
97
- const h = obj.hasOwnProperty('x')
98
- const v = obj.valueOf()
99
- `;
100
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/collide.ts", code2, false);
101
- (0, import_vitest.expect)(result2.code).not.toContain("__workletHash");
102
- });
103
- (0, import_vitest.it)("workletizes gesture callbacks only when the receiver is a Gesture chain", async () => {
104
- const gesture = await (0, import_transformWorklets.transformWorklets)("/app/gesture.ts", `const g = Gesture.Pan().onStart((e) => { scale.value = e.x }).onChange((e) => { scale.value = e.y })`, false);
105
- (0, import_vitest.expect)((gesture.code.match(/__workletHash/g) ?? []).length).toBe(2);
106
- const notAGesture = await (0, import_transformWorklets.transformWorklets)("/app/plain.ts", `onChange((d) => d + scrollDiff); emitter.onStart(function () { return count })`, false);
107
- (0, import_vitest.expect)(notAGesture.code).not.toContain("__workletHash");
108
- });
109
- (0, import_vitest.it)("workletizes withCallback only on a layout animation chain", async () => {
110
- const layout = await (0, import_transformWorklets.transformWorklets)("/app/layout.ts", `const a = FadeIn.duration(300).withCallback((finished) => { done.value = finished })`, false);
111
- (0, import_vitest.expect)(layout.code).toContain("__workletHash");
112
- const plain = await (0, import_transformWorklets.transformWorklets)("/app/plain-callback.ts", `queue.withCallback((finished) => { done.value = finished })`, false);
113
- (0, import_vitest.expect)(plain.code).not.toContain("__workletHash");
114
- });
115
- (0, import_vitest.it)("transforms closure variables into worklet parameters", async () => {
116
- const code2 = `
117
- const factor = 2
118
- export function multiply(x: number) {
119
- 'worklet'
120
- return x * factor
121
- }
122
- `;
123
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/multiply.ts", code2, false);
124
- (0, import_vitest.expect)(result2.code).toContain("__closure");
125
- (0, import_vitest.expect)(result2.code).toContain("factor");
126
- });
127
- (0, import_vitest.it)("transforms an anonymous function expression worklet", async () => {
128
- const code2 = `
129
- register(function (e) {
130
- 'worklet'
131
- return e.x
132
- })
133
- `;
134
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/handler.ts", code2, false);
135
- (0, import_vitest.expect)(result2.code).toContain("__workletHash");
136
- (0, import_vitest.expect)(result2.code).toMatch(/code: "function _worklet\(e\)/);
137
- (0, import_vitest.expect)(result2.code).toContain("var _worklet = function _worklet(e)");
138
- });
139
- (0, import_vitest.it)("does not capture hoisted declarations used before their declaration", async () => {
140
- const code2 = `
141
- export function setupLoop() {
142
- 'worklet'
143
- function nativeFlush(t: number) {
144
- flushQueue(t)
145
- }
146
- globalThis.__native(nativeFlush)
147
- function flushQueue(t: number) {
148
- globalThis.__stamp = t
149
- }
150
- const start = () => later()
151
- const later = () => start
152
- return start
153
- }
154
- `;
155
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/setupLoop.ts", code2, false);
156
- (0, import_vitest.expect)(result2.code).toMatch(/setupLoop\.__closure = \{\s*\}/);
157
- const init = result2.code.match(/code: "function setupLoop[^"]*"/);
158
- (0, import_vitest.expect)(init, "serialized worklet code").toBeTruthy();
159
- (0, import_vitest.expect)(init[0]).not.toContain("this.__closure");
160
- (0, import_vitest.expect)(init[0]).toContain("flushQueue");
161
- });
162
- (0, import_vitest.it)("honors no-worklet-closure and limit-init-data-hoisting the way the worklets runtime needs", async () => {
163
- const code2 = `
164
- const outerModuleValue = 1
165
- export function installUnpacker() {
166
- 'worklet'
167
- 'no-worklet-closure'
168
- const proxy = globalThis.__proxy
169
- proxy.register(outerModuleValue)
170
- proxy.install(() => {
171
- 'worklet'
172
- 'limit-init-data-hoisting'
173
- return 42
174
- })
175
- }
176
- `;
177
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/installUnpacker.ts", code2, false);
178
- const outerInit = result2.code.match(/code: "function installUnpacker[^"]*"/);
179
- (0, import_vitest.expect)(outerInit, "outer worklet serialized code").toBeTruthy();
180
- (0, import_vitest.expect)(outerInit[0]).not.toContain("this.__closure");
181
- (0, import_vitest.expect)(result2.code).toMatch(/installUnpacker\.__closure = \{\s*\}/);
182
- (0, import_vitest.expect)(outerInit[0]).not.toContain("no-worklet-closure");
183
- const innerVar = result2.code.match(/var (_worklet_\d+_init_data) = \{\s*code: "function _worklet/);
184
- (0, import_vitest.expect)(innerVar, "inner worklet init data").toBeTruthy();
185
- const emitted = result2.code.slice(result2.code.indexOf("export var installUnpacker"));
186
- (0, import_vitest.expect)(emitted).toContain(`var ${innerVar[1]} = {`);
187
- (0, import_vitest.expect)(result2.code.slice(0, result2.code.indexOf("export var installUnpacker"))).not.toContain(`
188
- var ${innerVar[1]} = {`);
189
- });
190
- (0, import_vitest.it)("autoworkletizes hooks like useAnimatedStyle and withTiming", async () => {
191
- const code2 = `
192
- export function Card() {
193
- const style = useAnimatedStyle(() => {
194
- return {
195
- opacity: withTiming(1),
196
- }
197
- })
198
- return <div style={style} />
199
- }
200
- `;
201
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/Card.tsx", code2, false);
202
- (0, import_vitest.expect)(result2.code).toContain("__workletHash");
203
- (0, import_vitest.expect)(result2.code).toContain("useAnimatedStyle");
204
- (0, import_vitest.expect)(result2.code).toContain("<div style={style} />");
205
- });
206
- (0, import_vitest.it)("generates sourcemaps when requested", async () => {
207
- const code2 = `
208
- export const workletFn = () => {
209
- 'worklet'
210
- return 'ok'
211
- }
212
- `;
213
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/worklet.ts", code2, true);
214
- (0, import_vitest.expect)(result2.map).toBeDefined();
215
- (0, import_vitest.expect)(result2.map?.mappings).toBeDefined();
216
- });
217
- (0, import_vitest.it)("rejects on transform errors instead of returning untransformed source", async () => {
218
- await (0, import_vitest.expect)((0, import_transformWorklets.transformWorklets)("/app/broken.ts", `export const fn = () => { 'worklet'; return ;;; const = }`, false)).rejects.toThrow();
219
- });
220
- (0, import_vitest.it)("stamps the package runtime version into __pluginVersion to avoid runtime mismatch", async () => {
221
- const testRoot = import_node_fs.default.realpathSync(import_node_fs.default.mkdtempSync(import_node_path.default.join(import_node_os.default.tmpdir(), "vxrn-worklets-ver-")));
222
- const packageRoot = import_node_path.default.join(testRoot, "node_modules", "react-native-worklets");
223
- import_node_fs.default.mkdirSync(packageRoot, {
224
- recursive: true
225
- });
226
- import_node_fs.default.writeFileSync(import_node_path.default.join(packageRoot, "package.json"), JSON.stringify({
227
- name: "react-native-worklets",
228
- version: "0.10.1"
229
- }));
230
- try {
231
- const result2 = await (0, import_transformWorklets.transformWorklets)(import_node_path.default.join(testRoot, "entry.ts"), `export const fn = () => { 'worklet'; return 1; }`, false, {
232
- projectRoot: testRoot
233
- });
234
- (0, import_vitest.expect)(result2.code).toContain('__pluginVersion = "0.10.1"');
235
- } finally {
236
- import_node_fs.default.rmSync(testRoot, {
237
- recursive: true,
238
- force: true
239
- });
240
- }
241
- });
242
- (0, import_vitest.it)("correctly resolves hoisted react-native-worklets in a monorepo when child has reanimated", async () => {
243
- const rootDir = import_node_fs.default.realpathSync(import_node_fs.default.mkdtempSync(import_node_path.default.join(import_node_os.default.tmpdir(), "vxrn-worklets-hoist-")));
244
- const appDir = import_node_path.default.join(rootDir, "apps", "mobile");
245
- const hoistedWorkletsDir = import_node_path.default.join(rootDir, "node_modules", "react-native-worklets");
246
- const localReanimatedDir = import_node_path.default.join(appDir, "node_modules", "react-native-reanimated");
247
- import_node_fs.default.mkdirSync(hoistedWorkletsDir, {
248
- recursive: true
249
- });
250
- import_node_fs.default.writeFileSync(import_node_path.default.join(hoistedWorkletsDir, "package.json"), JSON.stringify({
251
- name: "react-native-worklets",
252
- version: "0.10.1"
253
- }));
254
- import_node_fs.default.mkdirSync(localReanimatedDir, {
255
- recursive: true
256
- });
257
- import_node_fs.default.writeFileSync(import_node_path.default.join(localReanimatedDir, "package.json"), JSON.stringify({
258
- name: "react-native-reanimated",
259
- version: "4.5.1"
260
- }));
261
- import_node_fs.default.writeFileSync(import_node_path.default.join(appDir, "package.json"), JSON.stringify({
262
- name: "mobile"
263
- }));
264
- try {
265
- const result2 = await (0, import_transformWorklets.transformWorklets)(import_node_path.default.join(appDir, "entry.ts"), `export function test() { 'worklet'; return 1; }`, false, {
266
- projectRoot: appDir
267
- });
268
- (0, import_vitest.expect)(result2.code).toContain('__pluginVersion = "0.10.1"');
269
- } finally {
270
- import_node_fs.default.rmSync(rootDir, {
271
- recursive: true,
272
- force: true
273
- });
274
- }
275
- });
276
- (0, import_vitest.it)("throws an actionable error when neither worklets nor reanimated can be resolved", async () => {
277
- const emptyRoot = import_node_fs.default.realpathSync(import_node_fs.default.mkdtempSync(import_node_path.default.join(import_node_os.default.tmpdir(), "vxrn-worklets-empty-")));
278
- import_node_fs.default.writeFileSync(import_node_path.default.join(emptyRoot, "package.json"), JSON.stringify({
279
- name: "empty-test"
280
- }));
281
- try {
282
- await (0, import_vitest.expect)((0, import_transformWorklets.transformWorklets)(import_node_path.default.join(emptyRoot, "entry.ts"), `export function test() { 'worklet'; return 1; }`, false, {
283
- projectRoot: emptyRoot
284
- })).rejects.toThrow(/Unable to resolve "react-native-worklets" or "react-native-reanimated"/);
285
- } finally {
286
- import_node_fs.default.rmSync(emptyRoot, {
287
- recursive: true,
288
- force: true
289
- });
290
- }
291
- });
292
- (0, import_vitest.it)("forwards plugin options such as bundleMode and globals", async () => {
293
- const code2 = `
294
- export function testCustom() {
295
- 'worklet'
296
- return customGlobalValue * 2
297
- }
298
- `;
299
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/custom.ts", code2, false, {
300
- globals: ["customGlobalValue"],
301
- bundleMode: false
302
- });
303
- (0, import_vitest.expect)(result2.code).toContain("__workletHash");
304
- (0, import_vitest.expect)(result2.code).toContain("__initData");
305
- });
306
- (0, import_vitest.it)("satisfies runtime worklet serialization, local execution, and UI-thread reconstruction", async () => {
307
- const code = `
308
- const multiplier = 5
309
- export function scale(val: number) {
310
- 'worklet'
311
- return val * multiplier
312
- }
313
- `;
314
- const result = await (0, import_transformWorklets.transformWorklets)("/app/scale.ts", code, false);
315
- const sandbox = {
316
- global: globalThis,
317
- exports: {}
318
- };
319
- const runner = new Function("exports", "global", result.code.replace("export var scale", "exports.scale"));
320
- runner(sandbox.exports, sandbox.global);
321
- const scaleFn = sandbox.exports.scale;
322
- (0, import_vitest.expect)(typeof scaleFn).toBe("function");
323
- (0, import_vitest.expect)(scaleFn.__workletHash).toBeDefined();
324
- (0, import_vitest.expect)(typeof scaleFn.__workletHash).toBe("number");
325
- (0, import_vitest.expect)(scaleFn.__pluginVersion).toBe("0.10.1");
326
- (0, import_vitest.expect)(scaleFn.__closure).toEqual({
327
- multiplier: 5
328
- });
329
- (0, import_vitest.expect)(scaleFn.__initData).toBeDefined();
330
- (0, import_vitest.expect)(scaleFn.__initData.code).toContain("__closure");
331
- (0, import_vitest.expect)(scaleFn(10)).toBe(50);
332
- const reconstructedUIFn = eval(`(${scaleFn.__initData.code})`);
333
- const uiResult = reconstructedUIFn.call({
334
- __closure: scaleFn.__closure
335
- }, 10);
336
- (0, import_vitest.expect)(uiResult).toBe(50);
337
- });
338
- (0, import_vitest.it)("autoworkletizes hook callbacks with closure capture and UI-thread execution parity", async () => {
339
- const code = `
340
- import { useAnimatedStyle } from 'react-native-reanimated'
341
- const baseOpacity = 0.8
342
- export function getCardStyle() {
343
- return useAnimatedStyle(() => {
344
- return { opacity: baseOpacity }
345
- })
346
- }
347
- `;
348
- const result = await (0, import_transformWorklets.transformWorklets)("/app/CardStyle.ts", code, false);
349
- let capturedCallback = null;
350
- const sandbox = {
351
- global: globalThis,
352
- exports: {},
353
- useAnimatedStyle: cb => {
354
- capturedCallback = cb;
355
- return cb;
356
- }
357
- };
358
- const runner = new Function("exports", "global", "useAnimatedStyle", result.code.replace(/import\s+{[^}]+}\s+from\s+['"][^'"]+['"];?/, "").replace("export function getCardStyle", "exports.getCardStyle = function getCardStyle"));
359
- runner(sandbox.exports, sandbox.global, sandbox.useAnimatedStyle);
360
- sandbox.exports.getCardStyle();
361
- (0, import_vitest.expect)(capturedCallback).toBeDefined();
362
- (0, import_vitest.expect)(capturedCallback.__workletHash).toBeDefined();
363
- (0, import_vitest.expect)(capturedCallback.__closure).toEqual({
364
- baseOpacity: 0.8
365
- });
366
- const reconstructedStyleFn = eval(`(${capturedCallback.__initData.code})`);
367
- const styleResult = reconstructedStyleFn.call({
368
- __closure: capturedCallback.__closure
369
- });
370
- (0, import_vitest.expect)(styleResult).toEqual({
371
- opacity: 0.8
372
- });
373
- });
374
- (0, import_vitest.it)("compiles UI-thread worklet function to valid Hermes Bytecode (HBC)", async () => {
375
- const code2 = `
376
- const factor = 10
377
- export function calculate(val: number) {
378
- 'worklet'
379
- return val * factor
380
- }
381
- `;
382
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/calculate.ts", code2, false);
383
- const match = result2.code.match(/code:\s*"([^"]+)"/);
384
- (0, import_vitest.expect)(match).toBeDefined();
385
- const uiCode = JSON.parse(`"${match[1]}"`);
386
- const {
387
- execFileSync
388
- } = await import("node:child_process");
389
- const hermescDir = process.platform === "darwin" ? "osx-bin" : process.platform === "win32" ? "win64-bin" : "linux64-bin";
390
- const hermescPath = import_node_path.default.resolve(__dirname, `../../../node_modules/hermes-compiler/hermesc/${hermescDir}/hermesc${process.platform === "win32" ? ".exe" : ""}`);
391
- if (import_node_fs.default.existsSync(hermescPath)) {
392
- const tempDir = import_node_fs.default.mkdtempSync(import_node_path.default.join(import_node_os.default.tmpdir(), "hermes-worklet-test-"));
393
- const jsPath = import_node_path.default.join(tempDir, "worklet.js");
394
- const hbcPath = import_node_path.default.join(tempDir, "worklet.hbc");
395
- try {
396
- import_node_fs.default.writeFileSync(jsPath, `(${uiCode})`);
397
- execFileSync(hermescPath, ["-emit-binary", "-out", hbcPath, jsPath]);
398
- (0, import_vitest.expect)(import_node_fs.default.existsSync(hbcPath)).toBe(true);
399
- (0, import_vitest.expect)(import_node_fs.default.statSync(hbcPath).size).toBeGreaterThan(0);
400
- } finally {
401
- import_node_fs.default.rmSync(tempDir, {
402
- recursive: true,
403
- force: true
404
- });
405
- }
406
- }
407
- });
408
- (0, import_vitest.it)("transforms multiple worklets in the same file with independent closures", async () => {
409
- const code2 = `
410
- const a = 10
411
- const b = 20
412
- export function first() {
413
- 'worklet'
414
- return a
415
- }
416
- export function second() {
417
- 'worklet'
418
- return b
419
- }
420
- `;
421
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/multiple.ts", code2, false);
422
- (0, import_vitest.expect)(result2.code).toContain("first");
423
- (0, import_vitest.expect)(result2.code).toContain("second");
424
- (0, import_vitest.expect)(result2.code).toContain("__closure = { a: a }");
425
- (0, import_vitest.expect)(result2.code).toContain("__closure = { b: b }");
426
- });
427
- (0, import_vitest.it)("transforms concise arrow functions with useDerivedValue", async () => {
428
- const code2 = `
429
- import { useDerivedValue } from 'react-native-reanimated'
430
- const multiplier = 3
431
- export const derived = useDerivedValue(() => multiplier * 10)
432
- `;
433
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/derived.ts", code2, false);
434
- (0, import_vitest.expect)(result2.code).toContain("__workletHash");
435
- (0, import_vitest.expect)(result2.code).toContain("multiplier");
436
- });
437
- (0, import_vitest.it)("transforms scroll handler with object methods", async () => {
438
- const code2 = `
439
- import { useAnimatedScrollHandler } from 'react-native-reanimated'
440
- const offset = 5
441
- export const handler = useAnimatedScrollHandler({
442
- onScroll(event) {
443
- 'worklet'
444
- return event.contentOffset.y + offset
445
- }
446
- })
447
- `;
448
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/handler.ts", code2, false);
449
- (0, import_vitest.expect)(result2.code).toContain("onScroll:");
450
- (0, import_vitest.expect)(result2.code).toContain("__closure = { offset: offset }");
451
- });
452
- (0, import_vitest.it)("correctly handles default and destructuring parameters in closure capture", async () => {
453
- const code = `
454
- const factor = 4
455
- export function compute({ x, y = 1 }: { x: number; y?: number }, z = 0) {
456
- 'worklet'
457
- return (x + y + z) * factor
458
- }
459
- `;
460
- const result = await (0, import_transformWorklets.transformWorklets)("/app/destructure.ts", code, false);
461
- (0, import_vitest.expect)(result.code).toContain("__closure = { factor: factor }");
462
- const sandbox = {
463
- global: globalThis,
464
- exports: {}
465
- };
466
- const runner = new Function("exports", "global", result.code.replace("export var compute", "exports.compute"));
467
- runner(sandbox.exports, sandbox.global);
468
- const computeFn = sandbox.exports.compute;
469
- (0, import_vitest.expect)(computeFn({
470
- x: 2
471
- })).toBe(12);
472
- const reconstructed = eval(`(${computeFn.__initData.code})`);
473
- const uiRes = reconstructed.call({
474
- __closure: computeFn.__closure
475
- }, {
476
- x: 2,
477
- y: 3
478
- }, 5);
479
- (0, import_vitest.expect)(uiRes).toBe(40);
480
- });
481
- (0, import_vitest.it)("operates with 100% elimination of SWC (zero swc dependencies in transformWorklets)", async () => {
482
- const fs2 = await import("node:fs");
483
- const transformWorkletsSource = fs2.readFileSync(import_node_path.default.resolve(__dirname, "transformWorklets.ts"), "utf-8");
484
- (0, import_vitest.expect)(transformWorkletsSource).not.toContain("@swc/core");
485
- (0, import_vitest.expect)(transformWorkletsSource).not.toContain("@react-native-swc/worklets-plugin");
486
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/zeroSwc.ts", `export function zeroSwc() { 'worklet'; return 999; }`, false);
487
- (0, import_vitest.expect)(result2.code).toContain("__workletHash");
488
- (0, import_vitest.expect)(result2.code).toContain("__pluginVersion");
489
- (0, import_vitest.expect)(result2.code).toContain("999");
490
- });
491
- (0, import_vitest.it)("correctly handles lexical block scope shadowing without losing outer bindings", async () => {
492
- const code = `
493
- const x = 7
494
- export function f() {
495
- 'worklet'
496
- {
497
- const x = 1
498
- }
499
- return x
500
- }
501
- `;
502
- const result = await (0, import_transformWorklets.transformWorklets)("/app/scope-shadow.ts", code, false);
503
- (0, import_vitest.expect)(result.code).toContain("__closure = { x: x }");
504
- const sandbox = {
505
- global: globalThis,
506
- exports: {}
507
- };
508
- const runner = new Function("exports", "global", result.code.replace("export var f", "exports.f"));
509
- runner(sandbox.exports, sandbox.global);
510
- const f = sandbox.exports.f;
511
- (0, import_vitest.expect)(f()).toBe(7);
512
- const reconstructed = eval(`(${f.__initData.code})`);
513
- const uiRes = reconstructed.call({
514
- __closure: f.__closure
515
- });
516
- (0, import_vitest.expect)(uiRes).toBe(7);
517
- });
518
- (0, import_vitest.it)("transforms nested worklets with bottom-up composition", async () => {
519
- const code2 = `
520
- export function outer() {
521
- 'worklet'
522
- function inner() {
523
- 'worklet'
524
- return 1
525
- }
526
- return inner()
527
- }
528
- `;
529
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/nested.ts", code2, false);
530
- (0, import_vitest.expect)(result2.code).toContain("export var outer");
531
- (0, import_vitest.expect)(result2.code).toContain("inner.__workletHash");
532
- (0, import_vitest.expect)(result2.code).toContain("outer.__workletHash");
533
- const sandbox2 = {
534
- global: globalThis,
535
- exports: {}
536
- };
537
- const runner2 = new Function("exports", "global", result2.code.replace("export var outer", "exports.outer"));
538
- runner2(sandbox2.exports, sandbox2.global);
539
- const outerFn = sandbox2.exports.outer;
540
- (0, import_vitest.expect)(outerFn()).toBe(1);
541
- });
542
- (0, import_vitest.it)("transforms modules with >50 sibling worklets without truncation", async () => {
543
- const fnCount = 60;
544
- const code2 = Array.from({
545
- length: fnCount
546
- }, (_, i) => `export function f${i}() { 'worklet'; return ${i}; }`).join("\n");
547
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/many.ts", code2, false);
548
- for (let i = 0; i < fnCount; i++) {
549
- (0, import_vitest.expect)(result2.code).toContain(`export var f${i} =`);
550
- (0, import_vitest.expect)(result2.code).toContain(`f${i}.__workletHash`);
551
- }
552
- const sandbox2 = {
553
- global: globalThis,
554
- exports: {}
555
- };
556
- const runner2 = new Function("exports", "global", result2.code.replace(/export var /g, "exports."));
557
- runner2(sandbox2.exports, sandbox2.global);
558
- (0, import_vitest.expect)(sandbox2.exports.f0()).toBe(0);
559
- (0, import_vitest.expect)(sandbox2.exports.f50()).toBe(50);
560
- (0, import_vitest.expect)(sandbox2.exports.f59()).toBe(59);
561
- });
562
- (0, import_vitest.it)("transforms deeply nested worklets (3 levels) bottom-up", async () => {
563
- const code2 = `
564
- export function level1() {
565
- 'worklet';
566
- function level2() {
567
- 'worklet';
568
- function level3() {
569
- 'worklet';
570
- return 42;
571
- }
572
- return level3();
573
- }
574
- return level2();
575
- }
576
- `;
577
- const result2 = await (0, import_transformWorklets.transformWorklets)("/app/deep.ts", code2, false);
578
- (0, import_vitest.expect)(result2.code).toContain("level3.__workletHash");
579
- (0, import_vitest.expect)(result2.code).toContain("level2.__workletHash");
580
- (0, import_vitest.expect)(result2.code).toContain("level1.__workletHash");
581
- const sandbox2 = {
582
- global: globalThis,
583
- exports: {}
584
- };
585
- const runner2 = new Function("exports", "global", result2.code.replace("export var level1", "exports.level1"));
586
- runner2(sandbox2.exports, sandbox2.global);
587
- (0, import_vitest.expect)(sandbox2.exports.level1()).toBe(42);
588
- });
589
- });
590
- (0, import_vitest.describe)("worklet detection gate", () => {
591
- (0, import_vitest.it)("accepts files whose only worklets come from gesture callbacks", () => {
592
- (0, import_configure.configureVXRNCompilerPlugin)({
593
- enableReanimated: true,
594
- enableNativeWorklets: true
595
- });
596
- const code2 = `
597
- const tap = Gesture.Tap().onBegin((e) => { setState(e) }).onTouchesMove((e) => {}).onEnd(() => {})
598
- `;
599
- (0, import_vitest.expect)((0, import_transformWorklets.shouldTransformWorklets)({
600
- id: "/app/gesture.native.js",
601
- code: code2
602
- })).toBe(true);
603
- });
604
- });