@vxrn/compiler 2.0.0-beta.33.1 → 2.0.0-beta.46.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 (95) 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/src/transformWorklets.test.ts +7 -1
  67. package/types/cache.d.ts +2 -2
  68. package/types/cache.d.ts.map +1 -1
  69. package/types/index.d.ts.map +1 -1
  70. package/types/transformBabel.d.ts +16 -1
  71. package/types/transformBabel.d.ts.map +1 -1
  72. package/types/transformSWC.d.ts +1 -4
  73. package/types/transformSWC.d.ts.map +1 -1
  74. package/dist/cjs/transformBabel.test.cjs +0 -360
  75. package/dist/cjs/transformHermesLoops.test.cjs +0 -257
  76. package/dist/cjs/transformSWC.test.cjs +0 -117
  77. package/dist/cjs/transformWorklets.test.cjs +0 -604
  78. package/dist/esm/transformBabel.test.mjs +0 -337
  79. package/dist/esm/transformBabel.test.mjs.map +0 -1
  80. package/dist/esm/transformHermesLoops.test.mjs +0 -234
  81. package/dist/esm/transformHermesLoops.test.mjs.map +0 -1
  82. package/dist/esm/transformSWC.test.mjs +0 -118
  83. package/dist/esm/transformSWC.test.mjs.map +0 -1
  84. package/dist/esm/transformWorklets.test.mjs +0 -581
  85. package/dist/esm/transformWorklets.test.mjs.map +0 -1
  86. package/dist/esm/types.mjs.map +0 -1
  87. package/dist/esm/worklets/types.mjs.map +0 -1
  88. package/types/transformBabel.test.d.ts +0 -2
  89. package/types/transformBabel.test.d.ts.map +0 -1
  90. package/types/transformHermesLoops.test.d.ts +0 -2
  91. package/types/transformHermesLoops.test.d.ts.map +0 -1
  92. package/types/transformSWC.test.d.ts +0 -2
  93. package/types/transformSWC.test.d.ts.map +0 -1
  94. package/types/transformWorklets.test.d.ts +0 -2
  95. package/types/transformWorklets.test.d.ts.map +0 -1
@@ -3,6 +3,7 @@ import type { GetTransformProps, GetTransformResponse } from './types';
3
3
  type Props = GetTransformProps & {
4
4
  userSetting?: GetTransformResponse;
5
5
  };
6
+ export declare function findUserBabelConfig(projectRoot?: string): string | null;
6
7
  export declare function getBabelOptions(props: Props): babel.TransformOptions | null;
7
8
  /**
8
9
  * Run the react compiler through oxc's rust port instead of babel.
@@ -13,7 +14,9 @@ export declare function getBabelOptions(props: Props): babel.TransformOptions |
13
14
  * still applies the project's jsxImportSource and dev-mode settings, exactly
14
15
  * as it did when babel only stripped types here.
15
16
  */
16
- export declare function transformOxcReactCompiler(id: string, code: string, target: '18' | '19', sourceMap?: boolean): Promise<{
17
+ export declare function transformOxcReactCompiler(id: string, code: string, optionsOrTarget?: '18' | '19' | (Record<string, any> & {
18
+ target?: '18' | '19';
19
+ }), sourceMap?: boolean): Promise<{
17
20
  code: string;
18
21
  map: any;
19
22
  }>;
@@ -21,5 +24,17 @@ export declare function transformOxcReactCompiler(id: string, code: string, targ
21
24
  * Transform input to mostly ES5 compatible code, keep ESM syntax, and transform generators.
22
25
  */
23
26
  export declare function transformBabel(id: string, code: string, options: babel.TransformOptions): Promise<babel.BabelFileResult>;
27
+ export declare function stripFlowTypes(id: string, code: string, sourceMaps?: boolean): Promise<{
28
+ code: string;
29
+ map: {
30
+ version: number;
31
+ sources: string[];
32
+ names: string[];
33
+ sourceRoot?: string | undefined;
34
+ sourcesContent?: string[] | undefined;
35
+ mappings: string;
36
+ file: string;
37
+ } | null | undefined;
38
+ }>;
24
39
  export {};
25
40
  //# sourceMappingURL=transformBabel.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"transformBabel.d.ts","sourceRoot":"","sources":["../src/transformBabel.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,KAAK,KAAK,MAAM,aAAa,CAAA;AAKzC,OAAO,KAAK,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAEtE,KAAK,KAAK,GAAG,iBAAiB,GAAG;IAC/B,WAAW,CAAC,EAAE,oBAAoB,CAAA;CACnC,CAAA;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAAC,gBAAgB,GAAG,IAAI,CAoB3E;AA8CD;;;;;;;;GAQG;AACH,wBAAsB,yBAAyB,CAC7C,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,IAAI,GAAG,IAAI,EACnB,SAAS,UAAQ;;;GA4BlB;AAED;;GAEG;AACH,wBAAsB,cAAc,CAClC,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,KAAK,CAAC,gBAAgB,kCAsDhC"}
1
+ {"version":3,"file":"transformBabel.d.ts","sourceRoot":"","sources":["../src/transformBabel.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,KAAK,KAAK,MAAM,aAAa,CAAA;AAKzC,OAAO,KAAK,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAEtE,KAAK,KAAK,GAAG,iBAAiB,GAAG;IAC/B,WAAW,CAAC,EAAE,oBAAoB,CAAA;CACnC,CAAA;AAeD,wBAAgB,mBAAmB,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAiBvE;AAED,wBAAgB,eAAe,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAAC,gBAAgB,GAAG,IAAI,CA2C3E;AAqDD;;;;;;;;GAQG;AACH,wBAAsB,yBAAyB,CAC7C,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,MAAM,EACZ,eAAe,GAAE,IAAI,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG;IAAE,MAAM,CAAC,EAAE,IAAI,GAAG,IAAI,CAAA;CAAE,CAAQ,EACtF,SAAS,UAAQ;;;GA4ClB;AAED;;GAEG;AACH,wBAAsB,cAAc,CAClC,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,KAAK,CAAC,gBAAgB,kCAgEhC;AAED,wBAAsB,cAAc,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,UAAO;;;;;;;;;;;GAM/E"}
@@ -13,8 +13,5 @@ export declare const transformSWCStripJSX: (id: string, code: string) => Promise
13
13
  code: string;
14
14
  map: any;
15
15
  } | undefined>;
16
- export declare const transformOxcStripJSX: (id: string, code: string) => Promise<{
17
- code: string;
18
- map: any;
19
- } | undefined>;
16
+ export declare const transformOxcStripJSX: typeof transformSWCStripJSX;
20
17
  //# sourceMappingURL=transformSWC.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"transformSWC.d.ts","sourceRoot":"","sources":["../src/transformSWC.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAEtC,MAAM,WAAW,MAAM;IACrB,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,CAAC,EAAE,GAAG,CAAA;CACV;AAKD,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAWjE;AAED,wBAAsB,YAAY,CAChC,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO,GAAG;IAAE,GAAG,CAAC,EAAE,OAAO,CAAA;CAAE,EACpC,UAAU,CAAC,EAAE,GAAG,GACf,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAkI7B;AAED,eAAO,MAAM,YAAY,qBAAe,CAAA;AA+HxC,wBAAgB,eAAe,YAE9B;AAyBD,eAAO,MAAM,oBAAoB,GAAU,IAAI,MAAM,EAAE,MAAM,MAAM;;SAwBxB,GAAG;cAC7C,CAAA;AAED,eAAO,MAAM,oBAAoB,OA3Bc,MAAM,QAAQ,MAAM;;SAwBxB,GAAG;cAGU,CAAA"}
1
+ {"version":3,"file":"transformSWC.d.ts","sourceRoot":"","sources":["../src/transformSWC.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAEtC,MAAM,WAAW,MAAM;IACrB,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,CAAC,EAAE,GAAG,CAAA;CACV;AAKD,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAWjE;AAED,wBAAsB,YAAY,CAChC,EAAE,EAAE,MAAM,EACV,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,OAAO,GAAG;IAAE,GAAG,CAAC,EAAE,OAAO,CAAA;CAAE,EACpC,UAAU,CAAC,EAAE,GAAG,GACf,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAkI7B;AAED,eAAO,MAAM,YAAY,qBAAe,CAAA;AA+HxC,wBAAgB,eAAe,YAE9B;AAyBD,eAAO,MAAM,oBAAoB,OAAc,MAAM,QAAQ,MAAM;;SAwBxB,GAAG;cAC7C,CAAA;AAED,eAAO,MAAM,oBAAoB,6BAAuB,CAAA"}
@@ -1,360 +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_transformBabel = require("./transformBabel.cjs");
31
- (0, import_vitest.afterEach)(() => {
32
- (0, import_configure.configureVXRNCompilerPlugin)({
33
- enableReanimated: false
34
- });
35
- });
36
- (0, import_vitest.describe)("getBabelOptions Worklets resolution", () => {
37
- (0, import_vitest.it)("uses the app-installed Worklets Babel plugin", () => {
38
- const projectRoot = import_node_fs.default.realpathSync(import_node_fs.default.mkdtempSync(import_node_path.default.join(import_node_os.default.tmpdir(), "vxrn-worklets-")));
39
- const packageRoot = import_node_path.default.join(projectRoot, "node_modules", "react-native-worklets");
40
- const pluginPath = import_node_path.default.join(packageRoot, "plugin.js");
41
- import_node_fs.default.mkdirSync(packageRoot, {
42
- recursive: true
43
- });
44
- import_node_fs.default.writeFileSync(import_node_path.default.join(packageRoot, "package.json"), JSON.stringify({
45
- name: "react-native-worklets",
46
- version: "99.0.0"
47
- }));
48
- import_node_fs.default.writeFileSync(pluginPath, "module.exports = () => ({ visitor: {} })");
49
- try {
50
- (0, import_configure.configureVXRNCompilerPlugin)({
51
- enableReanimated: true
52
- });
53
- const options = (0, import_transformBabel.getBabelOptions)({
54
- id: import_node_path.default.join(projectRoot, "input.ts"),
55
- code: `export const worklet = () => {
56
- 'worklet'
57
- }`,
58
- development: true,
59
- environment: "ios",
60
- reactForRNVersion: "19",
61
- projectRoot
62
- });
63
- (0, import_vitest.expect)(options?.plugins).toContain(pluginPath);
64
- } finally {
65
- import_node_fs.default.rmSync(projectRoot, {
66
- recursive: true,
67
- force: true
68
- });
69
- }
70
- });
71
- });
72
- (0, import_vitest.describe)("transformBabel Flow parsing", () => {
73
- (0, import_vitest.it)("parses and strips React Native Flow as-casts", async () => {
74
- const result = await (0, import_transformBabel.transformBabel)("/project/VirtualViewNativeComponent.js", `
75
- // @flow strict-local
76
- import type { HostComponent } from './HostComponent'
77
- import codegenNativeComponent from './codegenNativeComponent'
78
-
79
- type Props = $ReadOnly<{ enabled?: boolean }>
80
-
81
- export default codegenNativeComponent<Props>('VirtualView') as HostComponent<Props>
82
- `, {
83
- plugins: []
84
- });
85
- (0, import_vitest.expect)(result?.code).toContain("codegenNativeComponent('VirtualView')");
86
- (0, import_vitest.expect)(result?.code).not.toContain("HostComponent");
87
- });
88
- (0, import_vitest.it)("rejects a required transform error instead of returning untransformed source", async () => {
89
- const negativeControl = () => ({
90
- visitor: {
91
- Program() {
92
- throw new Error("NATIVE_TRANSFORM_NEGATIVE_CONTROL");
93
- }
94
- }
95
- });
96
- await (0, import_vitest.expect)((0, import_transformBabel.transformBabel)("/project/TransformProbe.ts", "export const marker = 1", {
97
- plugins: [negativeControl]
98
- })).rejects.toThrow("NATIVE_TRANSFORM_NEGATIVE_CONTROL");
99
- });
100
- });
101
- (0, import_vitest.describe)("transformOxcReactCompiler", () => {
102
- const componentCode = `
103
- import { useState } from 'react'
104
-
105
- export function Counter() {
106
- const [count, setCount] = useState(0)
107
- return <button onClick={() => setCount(count + 1)}>{count}</button>
108
- }
109
- `;
110
- (0, import_vitest.it)("compiles and memoizes React components with Rust React Compiler", async () => {
111
- const result = await (0, import_transformBabel.transformOxcReactCompiler)("/project/Counter.tsx", componentCode, "19", false);
112
- (0, import_vitest.expect)(result.code).toContain("_c(");
113
- (0, import_vitest.expect)(result.code).toContain("react/compiler-runtime");
114
- (0, import_vitest.expect)(result.map).toBeUndefined();
115
- });
116
- (0, import_vitest.it)("generates source maps when requested", async () => {
117
- const result = await (0, import_transformBabel.transformOxcReactCompiler)("/project/Counter.tsx", componentCode, "19", true);
118
- (0, import_vitest.expect)(result.code).toContain("_c(");
119
- (0, import_vitest.expect)(result.map).toBeDefined();
120
- (0, import_vitest.expect)(result.map?.mappings).toBeDefined();
121
- });
122
- (0, import_vitest.it)("supports target 18", async () => {
123
- const result = await (0, import_transformBabel.transformOxcReactCompiler)("/project/Counter.tsx", componentCode, "18", false);
124
- (0, import_vitest.expect)(result.code).toContain("_c(");
125
- });
126
- });
127
- (0, import_vitest.describe)("compiler plugin multi-stage source map composition", () => {
128
- (0, import_vitest.it)("composes earlier compiler maps when Babel also runs", async () => {
129
- const {
130
- createVXRNCompilerPlugin
131
- } = await import("./index");
132
- const {
133
- TraceMap,
134
- originalPositionFor
135
- } = await import("@jridgewell/trace-mapping");
136
- const origSourceMap = process.env.VXRN_ENABLE_SOURCE_MAP;
137
- const origBabelGen = process.env.VXRN_USE_BABEL_FOR_GENERATORS;
138
- process.env.VXRN_ENABLE_SOURCE_MAP = "1";
139
- process.env.VXRN_USE_BABEL_FOR_GENERATORS = "1";
140
- (0, import_configure.configureVXRNCompilerPlugin)({
141
- enableCompiler: true
142
- });
143
- const tempFile = import_node_path.default.join(import_node_os.default.tmpdir(), `Counter-${Date.now()}.tsx`);
144
- import_node_fs.default.writeFileSync(tempFile, `import { useState } from 'react'
145
-
146
- export function Counter() {
147
- const [count, setCount] = useState(0)
148
- return <button onClick={() => setCount(count + 1)}>{count}</button>
149
- }
150
-
151
- export async function* generatorProbe() {
152
- throw new Error("MARKER")
153
- }
154
- `);
155
- try {
156
- const plugins = await createVXRNCompilerPlugin();
157
- const plugin = plugins.find(p => p.name === "one:compiler");
158
- await plugin.configResolved({
159
- root: import_node_os.default.tmpdir(),
160
- build: {}
161
- });
162
- const hook = plugin.transform.handler || plugin.transform;
163
- const code = import_node_fs.default.readFileSync(tempFile, "utf8");
164
- const context = {
165
- environment: {
166
- name: "client"
167
- }
168
- };
169
- const result = await hook.call(context, code, tempFile);
170
- (0, import_vitest.expect)(result).toBeDefined();
171
- (0, import_vitest.expect)(result.map).toBeDefined();
172
- const tracer = new TraceMap(result.map);
173
- const lines = result.code.split("\n");
174
- const markerLineIndex = lines.findIndex(l => l.includes("MARKER"));
175
- (0, import_vitest.expect)(markerLineIndex).toBeGreaterThan(0);
176
- const markerCol = lines[markerLineIndex].indexOf("throw");
177
- const pos = originalPositionFor(tracer, {
178
- line: markerLineIndex + 1,
179
- column: markerCol
180
- });
181
- (0, import_vitest.expect)(pos.source).toBe(tempFile);
182
- (0, import_vitest.expect)(pos.line).toBe(9);
183
- } finally {
184
- if (origSourceMap !== void 0) {
185
- process.env.VXRN_ENABLE_SOURCE_MAP = origSourceMap;
186
- } else {
187
- delete process.env.VXRN_ENABLE_SOURCE_MAP;
188
- }
189
- if (origBabelGen !== void 0) {
190
- process.env.VXRN_USE_BABEL_FOR_GENERATORS = origBabelGen;
191
- } else {
192
- delete process.env.VXRN_USE_BABEL_FOR_GENERATORS;
193
- }
194
- (0, import_configure.configureVXRNCompilerPlugin)({
195
- enableCompiler: false
196
- });
197
- import_node_fs.default.rmSync(tempFile, {
198
- force: true
199
- });
200
- }
201
- });
202
- });
203
- (0, import_vitest.describe)("shared compiler worklets backend selection", () => {
204
- (0, import_vitest.it)("retains Babel for worklets by default when enableNativeWorklets is false", async () => {
205
- const {
206
- vi
207
- } = await import("vitest");
208
- const {
209
- createVXRNCompilerPlugin
210
- } = await import("./index");
211
- const workletsModule = await import("./transformWorklets");
212
- const workletSpy = vi.spyOn(workletsModule, "transformWorklets");
213
- (0, import_configure.configureVXRNCompilerPlugin)({
214
- enableReanimated: true,
215
- enableNativeWorklets: false
216
- });
217
- const tempFile = import_node_path.default.join(process.cwd(), `test-default-worklet-${Date.now()}.tsx`);
218
- const inputCode = `export function fn() { 'worklet'; return 1 }`;
219
- import_node_fs.default.writeFileSync(tempFile, inputCode);
220
- try {
221
- const plugins = await createVXRNCompilerPlugin();
222
- const plugin = plugins.find(p => p.name === "one:compiler");
223
- await plugin.configResolved({
224
- root: process.cwd(),
225
- build: {}
226
- });
227
- const hook = plugin.transform.handler || plugin.transform;
228
- const context = {
229
- environment: {
230
- name: "ios"
231
- }
232
- };
233
- const result = await hook.call(context, inputCode, tempFile);
234
- (0, import_vitest.expect)(result).toBeDefined();
235
- (0, import_vitest.expect)(workletSpy).not.toHaveBeenCalled();
236
- } finally {
237
- workletSpy.mockRestore();
238
- (0, import_configure.configureVXRNCompilerPlugin)({
239
- enableReanimated: false,
240
- enableNativeWorklets: false
241
- });
242
- import_node_fs.default.rmSync(tempFile, {
243
- force: true
244
- });
245
- }
246
- });
247
- (0, import_vitest.it)("uses native SWC for worklets when enableNativeWorklets is true", async () => {
248
- const compiler = await import("./index");
249
- const {
250
- vi
251
- } = await import("vitest");
252
- const workletsModule = await import("./transformWorklets");
253
- const workletSpy = vi.spyOn(workletsModule, "transformWorklets");
254
- (0, import_configure.configureVXRNCompilerPlugin)({
255
- enableReanimated: true,
256
- enableNativeWorklets: true
257
- });
258
- const tempFile = import_node_path.default.join(process.cwd(), `test-native-worklet-${Date.now()}.tsx`);
259
- const inputCode = `export function fn() { 'worklet'; return 1 }`;
260
- import_node_fs.default.writeFileSync(tempFile, inputCode);
261
- try {
262
- const plugins = await compiler.createVXRNCompilerPlugin();
263
- const plugin = plugins.find(p => p.name === "one:compiler");
264
- await plugin.configResolved({
265
- root: process.cwd(),
266
- build: {}
267
- });
268
- const hook = plugin.transform.handler || plugin.transform;
269
- const context = {
270
- environment: {
271
- name: "ios"
272
- }
273
- };
274
- const result = await hook.call(context, inputCode, tempFile);
275
- (0, import_vitest.expect)(result).toBeDefined();
276
- (0, import_vitest.expect)(workletSpy).toHaveBeenCalled();
277
- } finally {
278
- workletSpy.mockRestore();
279
- (0, import_configure.configureVXRNCompilerPlugin)({
280
- enableReanimated: false,
281
- enableNativeWorklets: false
282
- });
283
- import_node_fs.default.rmSync(tempFile, {
284
- force: true
285
- });
286
- }
287
- });
288
- (0, import_vitest.it)("invalidates cache when toggling worklets backend on the same unchanged file", async () => {
289
- const compiler = await import("./index");
290
- const {
291
- vi
292
- } = await import("vitest");
293
- const workletsModule = await import("./transformWorklets");
294
- const workletSpy = vi.spyOn(workletsModule, "transformWorklets");
295
- const tempFile = import_node_path.default.join(process.cwd(), `test-cache-toggle-${Date.now()}.tsx`);
296
- const inputCode = `export function fn() { 'worklet'; return 1 }`;
297
- import_node_fs.default.writeFileSync(tempFile, inputCode);
298
- try {
299
- (0, import_configure.configureVXRNCompilerPlugin)({
300
- enableReanimated: true,
301
- enableNativeWorklets: false
302
- });
303
- const plugins1 = await compiler.createVXRNCompilerPlugin();
304
- const plugin1 = plugins1.find(p => p.name === "one:compiler");
305
- await plugin1.configResolved({
306
- root: process.cwd(),
307
- build: {}
308
- });
309
- const hook1 = plugin1.transform.handler || plugin1.transform;
310
- const context = {
311
- environment: {
312
- name: "ios"
313
- }
314
- };
315
- const res1 = await hook1.call(context, inputCode, tempFile);
316
- (0, import_vitest.expect)(res1).toBeDefined();
317
- (0, import_vitest.expect)(workletSpy).toHaveBeenCalledTimes(0);
318
- (0, import_configure.configureVXRNCompilerPlugin)({
319
- enableReanimated: true,
320
- enableNativeWorklets: true
321
- });
322
- const plugins2 = await compiler.createVXRNCompilerPlugin();
323
- const plugin2 = plugins2.find(p => p.name === "one:compiler");
324
- await plugin2.configResolved({
325
- root: process.cwd(),
326
- build: {}
327
- });
328
- const hook2 = plugin2.transform.handler || plugin2.transform;
329
- const res2 = await hook2.call(context, inputCode, tempFile);
330
- (0, import_vitest.expect)(res2).toBeDefined();
331
- (0, import_vitest.expect)(workletSpy).toHaveBeenCalledTimes(1);
332
- const res3 = await hook2.call(context, inputCode, tempFile);
333
- (0, import_vitest.expect)(res3).toBeDefined();
334
- (0, import_vitest.expect)(workletSpy).toHaveBeenCalledTimes(1);
335
- (0, import_configure.configureVXRNCompilerPlugin)({
336
- enableReanimated: true,
337
- enableNativeWorklets: false
338
- });
339
- const plugins3 = await compiler.createVXRNCompilerPlugin();
340
- const plugin3 = plugins3.find(p => p.name === "one:compiler");
341
- await plugin3.configResolved({
342
- root: process.cwd(),
343
- build: {}
344
- });
345
- const hook3 = plugin3.transform.handler || plugin3.transform;
346
- const res4 = await hook3.call(context, inputCode, tempFile);
347
- (0, import_vitest.expect)(res4).toBeDefined();
348
- (0, import_vitest.expect)(workletSpy).toHaveBeenCalledTimes(1);
349
- } finally {
350
- workletSpy.mockRestore();
351
- (0, import_configure.configureVXRNCompilerPlugin)({
352
- enableReanimated: false,
353
- enableNativeWorklets: false
354
- });
355
- import_node_fs.default.rmSync(tempFile, {
356
- force: true
357
- });
358
- }
359
- });
360
- });
@@ -1,257 +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_vitest = require("vitest");
26
- var vm = __toESM(require("node:vm"), 1);
27
- var import_transformHermesLoops = require("./transformHermesLoops.cjs");
28
- function runAsHermes(code) {
29
- const asHermesWouldSeeIt = code.replace(/\b(?:let|const)\b/g, "var");
30
- const sandbox = {
31
- result: void 0,
32
- console
33
- };
34
- vm.runInNewContext(asHermesWouldSeeIt, sandbox);
35
- return sandbox.result;
36
- }
37
- function run(source) {
38
- const out = (0, import_transformHermesLoops.transformHermesLoops)(source, "test.js");
39
- return runAsHermes(out ? out.code : source);
40
- }
41
- (0, import_vitest.describe)("transformHermesLoops", () => {
42
- import_vitest.it.each([["if (false)", []], ["if (true)", [1, 2]], ["for (var pass = 0; pass < 2; pass++)", [1, 2, 1, 2]], ["while (passes++ < 2)", [1, 2, 1, 2]]])("keeps a captured loop inside a single-statement %s body", (parent, expected) => {
43
- (0, import_vitest.expect)(run(`
44
- var fns = []
45
- var passes = 0
46
- ${parent} for (const n of [1, 2]) fns.push(() => n)
47
- result = fns.map((f) => f())
48
- `)).toEqual(expected);
49
- });
50
- import_vitest.it.each([true, false])("preserves if/else selection when the condition is %s", condition => {
51
- (0, import_vitest.expect)(run(`
52
- var fns = []
53
- if (${condition}) for (const n of [1, 2]) fns.push(() => n)
54
- else for (const n of [3, 4]) fns.push(() => n)
55
- result = fns.map((f) => f())
56
- `)).toEqual(condition ? [1, 2] : [3, 4]);
57
- });
58
- (0, import_vitest.it)("preserves a do/while with a single-statement loop body", () => {
59
- (0, import_vitest.expect)(run(`
60
- var fns = []
61
- var passes = 0
62
- do for (const n of [1, 2]) fns.push(() => n)
63
- while (passes++ < 1)
64
- result = fns.map((f) => f())
65
- `)).toEqual([1, 2, 1, 2]);
66
- });
67
- (0, import_vitest.it)("guards optional Zero query patches through nested loop transforms", () => {
68
- (0, import_vitest.expect)(run(`
69
- var fns = []
70
- var parts = [{}, { desiredQueriesPatches: { a: ['x', 'y'], b: ['z'] } }]
71
- for (const part of parts) {
72
- if (part.desiredQueriesPatches)
73
- for (const [clientID, queriesPatch] of Object.entries(part.desiredQueriesPatches))
74
- for (const op of queriesPatch) fns.push(() => clientID + ':' + op)
75
- }
76
- result = fns.map((f) => f())
77
- `)).toEqual(["a:x", "a:y", "b:z"]);
78
- });
79
- (0, import_vitest.it)("gives a C-style loop a per-iteration binding", () => {
80
- (0, import_vitest.expect)(run(`
81
- var fns = []
82
- for (let i = 0; i < 3; i++) fns.push(() => i)
83
- result = fns.map((f) => f())
84
- `)).toEqual([0, 1, 2]);
85
- });
86
- (0, import_vitest.it)("gives for-of and for-in per-iteration bindings", () => {
87
- (0, import_vitest.expect)(run(`
88
- var fns = []
89
- for (const label of ['a', 'b']) fns.push(() => label)
90
- for (const key in { x: 1, y: 2 }) fns.push(() => key)
91
- result = fns.map((f) => f())
92
- `)).toEqual(["a", "b", "x", "y"]);
93
- });
94
- (0, import_vitest.it)("preserves break", () => {
95
- (0, import_vitest.expect)(run(`
96
- var fns = []
97
- for (let i = 0; i < 10; i++) {
98
- if (i === 3) break
99
- fns.push(() => i)
100
- }
101
- result = fns.map((f) => f())
102
- `)).toEqual([0, 1, 2]);
103
- });
104
- (0, import_vitest.it)("preserves continue", () => {
105
- (0, import_vitest.expect)(run(`
106
- var fns = []
107
- for (let i = 0; i < 5; i++) {
108
- if (i % 2 === 0) continue
109
- fns.push(() => i)
110
- }
111
- result = fns.map((f) => f())
112
- `)).toEqual([1, 3]);
113
- });
114
- (0, import_vitest.it)("preserves return out of the enclosing function", () => {
115
- (0, import_vitest.expect)(run(`
116
- function find() {
117
- for (const n of [1, 2, 3]) {
118
- const get = () => n
119
- if (n === 2) return get()
120
- }
121
- return 'none'
122
- }
123
- result = find()
124
- `)).toBe(2);
125
- });
126
- (0, import_vitest.it)("preserves a bare return", () => {
127
- (0, import_vitest.expect)(run(`
128
- var fns = []
129
- function go() {
130
- for (let i = 0; i < 5; i++) {
131
- if (i === 2) return
132
- fns.push(() => i)
133
- }
134
- }
135
- go()
136
- result = fns.map((f) => f())
137
- `)).toEqual([0, 1]);
138
- });
139
- (0, import_vitest.it)("leaves break and continue belonging to a nested loop alone", () => {
140
- (0, import_vitest.expect)(run(`
141
- var fns = []
142
- for (let i = 0; i < 2; i++) {
143
- for (var j = 0; j < 5; j++) {
144
- if (j > 0) break
145
- }
146
- fns.push(() => i)
147
- }
148
- result = fns.map((f) => f())
149
- `)).toEqual([0, 1]);
150
- });
151
- (0, import_vitest.it)("handles nested capturing loops", () => {
152
- (0, import_vitest.expect)(run(`
153
- var fns = []
154
- for (let i = 0; i < 2; i++) {
155
- for (let j = 0; j < 2; j++) {
156
- fns.push(() => i + ':' + j)
157
- }
158
- }
159
- result = fns.map((f) => f())
160
- `)).toEqual(["0:0", "0:1", "1:0", "1:1"]);
161
- });
162
- (0, import_vitest.it)("handles await in the loop body", async () => {
163
- const out = (0, import_transformHermesLoops.transformHermesLoops)(`
164
- var fns = []
165
- async function go() {
166
- for (const n of [1, 2]) {
167
- await Promise.resolve()
168
- fns.push(() => n)
169
- }
170
- return fns.map((f) => f())
171
- }
172
- result = go()
173
- `, "test.js");
174
- (0, import_vitest.expect)(out).not.toBeNull();
175
- await (0, import_vitest.expect)(runAsHermes(out.code)).resolves.toEqual([1, 2]);
176
- });
177
- (0, import_vitest.it)("destructures a for-of binding per iteration", () => {
178
- (0, import_vitest.expect)(run(`
179
- var fns = []
180
- for (const { id } of [{ id: 'a' }, { id: 'b' }]) fns.push(() => id)
181
- result = fns.map((f) => f())
182
- `)).toEqual(["a", "b"]);
183
- });
184
- (0, import_vitest.it)("rewrites the esbuild interop helper that broke every multi-export module", () => {
185
- (0, import_vitest.expect)(run(`
186
- var __defProp = Object.defineProperty
187
- var __getOwnPropNames = Object.getOwnPropertyNames
188
- var from = { a: 1, b: 2 }
189
- var to = {}
190
- for (let key of __getOwnPropNames(from)) {
191
- __defProp(to, key, { get: () => from[key], enumerable: true })
192
- }
193
- result = [to.a, to.b]
194
- `)).toEqual([1, 2]);
195
- });
196
- (0, import_vitest.it)("leaves loops that never capture their binding untouched", () => {
197
- (0, import_vitest.expect)((0, import_transformHermesLoops.transformHermesLoops)("for (let i = 0; i < 3; i++) sum += i", "test.js")).toBeNull();
198
- });
199
- (0, import_vitest.it)("gives a binding declared in the loop body a per-iteration copy", () => {
200
- (0, import_vitest.expect)(run(`
201
- var names = ['createAnimatedNode', 'addListener', 'removeListener']
202
- var wrappers = {}
203
- for (var ii = 0; ii < names.length; ii++) {
204
- const methodName = names[ii]
205
- wrappers[methodName] = () => methodName
206
- }
207
- result = names.map((n) => wrappers[n]())
208
- `)).toEqual(["createAnimatedNode", "addListener", "removeListener"]);
209
- });
210
- (0, import_vitest.it)("gives a binding declared in a nested block of the loop body a per-iteration copy", () => {
211
- (0, import_vitest.expect)(run(`
212
- var fns = []
213
- for (var i = 0; i < 3; i++) {
214
- if (i >= 0) {
215
- const doubled = i * 2
216
- fns.push(() => doubled)
217
- }
218
- }
219
- result = fns.map((f) => f())
220
- `)).toEqual([0, 2, 4]);
221
- });
222
- (0, import_vitest.it)("keeps `this` and `arguments` meaning what they meant outside the loop", () => {
223
- (0, import_vitest.expect)(run(`
224
- class PropsFilter {
225
- constructor() { this._map = { a: 'A', b: 'B' } }
226
- filter(props) {
227
- var out = []
228
- for (const key in props) {
229
- const label = key
230
- out.push(() => this._map[label] + arguments.length)
231
- }
232
- return out
233
- }
234
- }
235
- result = new PropsFilter().filter({ a: 1, b: 2 }).map((f) => f())
236
- `)).toEqual(["A1", "B1"]);
237
- });
238
- (0, import_vitest.it)("leaves a loop with no lexical binding untouched", () => {
239
- (0, import_vitest.expect)((0, import_transformHermesLoops.transformHermesLoops)("for (var i = 0; i < 3; i++) fns.push(() => i)", "test.js")).toBeNull();
240
- });
241
- (0, import_vitest.it)("leaves the head declaration alone so it cannot collide with a sibling", () => {
242
- const out = (0, import_transformHermesLoops.transformHermesLoops)(`function build(gestures) {
243
- var handlers = []
244
- for (const gesture of gestures) { handlers.push(() => gesture) }
245
- const gesture = gestures[0]
246
- return gesture
247
- }`, "Pressable.js");
248
- (0, import_vitest.expect)(() => new vm.Script(out.code)).not.toThrow();
249
- });
250
- (0, import_vitest.it)("rewrites a loop in a .js file that still contains jsx", () => {
251
- const out = (0, import_transformHermesLoops.transformHermesLoops)("for (const c of items) rows.push(() => <Row key={c} />)", "Rows.js");
252
- (0, import_vitest.expect)(out?.code).toContain("_hermesLoop0");
253
- });
254
- (0, import_vitest.it)("does not rewrite a loop that reassigns its own binding", () => {
255
- (0, import_vitest.expect)((0, import_transformHermesLoops.transformHermesLoops)("for (let i = 0; i < 3; i++) { i += 1; fns.push(() => i) }", "test.js")).toBeNull();
256
- });
257
- });