@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.
Files changed (124) hide show
  1. package/dist/cjs/cache.cjs +3 -1
  2. package/dist/cjs/configure.cjs +9 -1
  3. package/dist/cjs/index.cjs +82 -4
  4. package/dist/cjs/reactNativeCodegen.cjs +140 -0
  5. package/dist/cjs/reactNativeViewConfig.cjs +489 -0
  6. package/dist/cjs/transformBabel.cjs +15 -20
  7. package/dist/cjs/transformBabel.test.cjs +260 -0
  8. package/dist/cjs/transformHermesLoops.cjs +317 -0
  9. package/dist/cjs/transformHermesLoops.test.cjs +220 -0
  10. package/dist/cjs/transformSWC.cjs +102 -137
  11. package/dist/cjs/transformSWC.test.cjs +74 -0
  12. package/dist/cjs/transformWorklets.cjs +121 -0
  13. package/dist/cjs/transformWorklets.test.cjs +604 -0
  14. package/dist/cjs/worklets/autoworklet.cjs +265 -0
  15. package/dist/cjs/worklets/globals.cjs +71 -0
  16. package/dist/cjs/worklets/hash.cjs +38 -0
  17. package/dist/cjs/worklets/scope.cjs +370 -0
  18. package/dist/cjs/worklets/serialize.cjs +101 -0
  19. package/dist/cjs/worklets/transform.cjs +232 -0
  20. package/dist/cjs/worklets/types.cjs +18 -0
  21. package/dist/esm/cache.mjs +4 -2
  22. package/dist/esm/cache.mjs.map +1 -1
  23. package/dist/esm/configure.mjs +8 -1
  24. package/dist/esm/configure.mjs.map +1 -1
  25. package/dist/esm/index.js +72 -5
  26. package/dist/esm/index.js.map +1 -1
  27. package/dist/esm/index.mjs +72 -5
  28. package/dist/esm/index.mjs.map +1 -1
  29. package/dist/esm/reactNativeCodegen.mjs +104 -0
  30. package/dist/esm/reactNativeCodegen.mjs.map +1 -0
  31. package/dist/esm/reactNativeViewConfig.mjs +460 -0
  32. package/dist/esm/reactNativeViewConfig.mjs.map +1 -0
  33. package/dist/esm/transformBabel.mjs +14 -19
  34. package/dist/esm/transformBabel.mjs.map +1 -1
  35. package/dist/esm/transformBabel.test.mjs +261 -1
  36. package/dist/esm/transformBabel.test.mjs.map +1 -1
  37. package/dist/esm/transformHermesLoops.mjs +281 -0
  38. package/dist/esm/transformHermesLoops.mjs.map +1 -0
  39. package/dist/esm/transformHermesLoops.test.mjs +197 -0
  40. package/dist/esm/transformHermesLoops.test.mjs.map +1 -0
  41. package/dist/esm/transformSWC.mjs +89 -138
  42. package/dist/esm/transformSWC.mjs.map +1 -1
  43. package/dist/esm/transformSWC.test.mjs +75 -0
  44. package/dist/esm/transformSWC.test.mjs.map +1 -0
  45. package/dist/esm/transformWorklets.mjs +81 -0
  46. package/dist/esm/transformWorklets.mjs.map +1 -0
  47. package/dist/esm/transformWorklets.test.mjs +581 -0
  48. package/dist/esm/transformWorklets.test.mjs.map +1 -0
  49. package/dist/esm/worklets/autoworklet.mjs +234 -0
  50. package/dist/esm/worklets/autoworklet.mjs.map +1 -0
  51. package/dist/esm/worklets/globals.mjs +45 -0
  52. package/dist/esm/worklets/globals.mjs.map +1 -0
  53. package/dist/esm/worklets/hash.mjs +13 -0
  54. package/dist/esm/worklets/hash.mjs.map +1 -0
  55. package/dist/esm/worklets/scope.mjs +345 -0
  56. package/dist/esm/worklets/scope.mjs.map +1 -0
  57. package/dist/esm/worklets/serialize.mjs +75 -0
  58. package/dist/esm/worklets/serialize.mjs.map +1 -0
  59. package/dist/esm/worklets/transform.mjs +196 -0
  60. package/dist/esm/worklets/transform.mjs.map +1 -0
  61. package/dist/esm/worklets/types.mjs +2 -0
  62. package/dist/esm/worklets/types.mjs.map +1 -0
  63. package/package.json +10 -6
  64. package/src/cache.ts +9 -2
  65. package/src/configure.ts +12 -0
  66. package/src/index.ts +93 -13
  67. package/src/reactNativeCodegen.ts +181 -0
  68. package/src/reactNativeViewConfig.ts +593 -0
  69. package/src/transformBabel.test.ts +268 -1
  70. package/src/transformBabel.ts +21 -31
  71. package/src/transformHermesLoops.test.ts +282 -0
  72. package/src/transformHermesLoops.ts +433 -0
  73. package/src/transformSWC.test.ts +88 -0
  74. package/src/transformSWC.ts +109 -160
  75. package/src/transformWorklets.test.ts +708 -0
  76. package/src/transformWorklets.ts +160 -0
  77. package/src/types.ts +4 -4
  78. package/src/worklets/autoworklet.ts +391 -0
  79. package/src/worklets/globals.ts +125 -0
  80. package/src/worklets/hash.ts +14 -0
  81. package/src/worklets/scope.ts +401 -0
  82. package/src/worklets/serialize.ts +114 -0
  83. package/src/worklets/transform.ts +282 -0
  84. package/src/worklets/types.ts +26 -0
  85. package/types/cache.d.ts +1 -0
  86. package/types/cache.d.ts.map +1 -1
  87. package/types/configure.d.ts +2 -0
  88. package/types/configure.d.ts.map +1 -1
  89. package/types/index.d.ts +3 -0
  90. package/types/index.d.ts.map +1 -1
  91. package/types/reactNativeCodegen.d.ts +5 -0
  92. package/types/reactNativeCodegen.d.ts.map +1 -0
  93. package/types/reactNativeViewConfig.d.ts +35 -0
  94. package/types/reactNativeViewConfig.d.ts.map +1 -0
  95. package/types/transformBabel.d.ts +3 -3
  96. package/types/transformBabel.d.ts.map +1 -1
  97. package/types/transformHermesLoops.d.ts +5 -0
  98. package/types/transformHermesLoops.d.ts.map +1 -0
  99. package/types/transformHermesLoops.test.d.ts +2 -0
  100. package/types/transformHermesLoops.test.d.ts.map +1 -0
  101. package/types/transformSWC.d.ts +12 -3
  102. package/types/transformSWC.d.ts.map +1 -1
  103. package/types/transformSWC.test.d.ts +2 -0
  104. package/types/transformSWC.test.d.ts.map +1 -0
  105. package/types/transformWorklets.d.ts +22 -0
  106. package/types/transformWorklets.d.ts.map +1 -0
  107. package/types/transformWorklets.test.d.ts +2 -0
  108. package/types/transformWorklets.test.d.ts.map +1 -0
  109. package/types/types.d.ts +4 -4
  110. package/types/types.d.ts.map +1 -1
  111. package/types/worklets/autoworklet.d.ts +14 -0
  112. package/types/worklets/autoworklet.d.ts.map +1 -0
  113. package/types/worklets/globals.d.ts +3 -0
  114. package/types/worklets/globals.d.ts.map +1 -0
  115. package/types/worklets/hash.d.ts +5 -0
  116. package/types/worklets/hash.d.ts.map +1 -0
  117. package/types/worklets/scope.d.ts +7 -0
  118. package/types/worklets/scope.d.ts.map +1 -0
  119. package/types/worklets/serialize.d.ts +12 -0
  120. package/types/worklets/serialize.d.ts.map +1 -0
  121. package/types/worklets/transform.d.ts +6 -0
  122. package/types/worklets/transform.d.ts.map +1 -0
  123. package/types/worklets/types.d.ts +21 -0
  124. package/types/worklets/types.d.ts.map +1 -0
@@ -0,0 +1,220 @@
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
+ (0, import_vitest.it)("gives a C-style loop a per-iteration binding", () => {
43
+ (0, import_vitest.expect)(run(`
44
+ var fns = []
45
+ for (let i = 0; i < 3; i++) fns.push(() => i)
46
+ result = fns.map((f) => f())
47
+ `)).toEqual([0, 1, 2]);
48
+ });
49
+ (0, import_vitest.it)("gives for-of and for-in per-iteration bindings", () => {
50
+ (0, import_vitest.expect)(run(`
51
+ var fns = []
52
+ for (const label of ['a', 'b']) fns.push(() => label)
53
+ for (const key in { x: 1, y: 2 }) fns.push(() => key)
54
+ result = fns.map((f) => f())
55
+ `)).toEqual(["a", "b", "x", "y"]);
56
+ });
57
+ (0, import_vitest.it)("preserves break", () => {
58
+ (0, import_vitest.expect)(run(`
59
+ var fns = []
60
+ for (let i = 0; i < 10; i++) {
61
+ if (i === 3) break
62
+ fns.push(() => i)
63
+ }
64
+ result = fns.map((f) => f())
65
+ `)).toEqual([0, 1, 2]);
66
+ });
67
+ (0, import_vitest.it)("preserves continue", () => {
68
+ (0, import_vitest.expect)(run(`
69
+ var fns = []
70
+ for (let i = 0; i < 5; i++) {
71
+ if (i % 2 === 0) continue
72
+ fns.push(() => i)
73
+ }
74
+ result = fns.map((f) => f())
75
+ `)).toEqual([1, 3]);
76
+ });
77
+ (0, import_vitest.it)("preserves return out of the enclosing function", () => {
78
+ (0, import_vitest.expect)(run(`
79
+ function find() {
80
+ for (const n of [1, 2, 3]) {
81
+ const get = () => n
82
+ if (n === 2) return get()
83
+ }
84
+ return 'none'
85
+ }
86
+ result = find()
87
+ `)).toBe(2);
88
+ });
89
+ (0, import_vitest.it)("preserves a bare return", () => {
90
+ (0, import_vitest.expect)(run(`
91
+ var fns = []
92
+ function go() {
93
+ for (let i = 0; i < 5; i++) {
94
+ if (i === 2) return
95
+ fns.push(() => i)
96
+ }
97
+ }
98
+ go()
99
+ result = fns.map((f) => f())
100
+ `)).toEqual([0, 1]);
101
+ });
102
+ (0, import_vitest.it)("leaves break and continue belonging to a nested loop alone", () => {
103
+ (0, import_vitest.expect)(run(`
104
+ var fns = []
105
+ for (let i = 0; i < 2; i++) {
106
+ for (var j = 0; j < 5; j++) {
107
+ if (j > 0) break
108
+ }
109
+ fns.push(() => i)
110
+ }
111
+ result = fns.map((f) => f())
112
+ `)).toEqual([0, 1]);
113
+ });
114
+ (0, import_vitest.it)("handles nested capturing loops", () => {
115
+ (0, import_vitest.expect)(run(`
116
+ var fns = []
117
+ for (let i = 0; i < 2; i++) {
118
+ for (let j = 0; j < 2; j++) {
119
+ fns.push(() => i + ':' + j)
120
+ }
121
+ }
122
+ result = fns.map((f) => f())
123
+ `)).toEqual(["0:0", "0:1", "1:0", "1:1"]);
124
+ });
125
+ (0, import_vitest.it)("handles await in the loop body", async () => {
126
+ const out = (0, import_transformHermesLoops.transformHermesLoops)(`
127
+ var fns = []
128
+ async function go() {
129
+ for (const n of [1, 2]) {
130
+ await Promise.resolve()
131
+ fns.push(() => n)
132
+ }
133
+ return fns.map((f) => f())
134
+ }
135
+ result = go()
136
+ `, "test.js");
137
+ (0, import_vitest.expect)(out).not.toBeNull();
138
+ await (0, import_vitest.expect)(runAsHermes(out.code)).resolves.toEqual([1, 2]);
139
+ });
140
+ (0, import_vitest.it)("destructures a for-of binding per iteration", () => {
141
+ (0, import_vitest.expect)(run(`
142
+ var fns = []
143
+ for (const { id } of [{ id: 'a' }, { id: 'b' }]) fns.push(() => id)
144
+ result = fns.map((f) => f())
145
+ `)).toEqual(["a", "b"]);
146
+ });
147
+ (0, import_vitest.it)("rewrites the esbuild interop helper that broke every multi-export module", () => {
148
+ (0, import_vitest.expect)(run(`
149
+ var __defProp = Object.defineProperty
150
+ var __getOwnPropNames = Object.getOwnPropertyNames
151
+ var from = { a: 1, b: 2 }
152
+ var to = {}
153
+ for (let key of __getOwnPropNames(from)) {
154
+ __defProp(to, key, { get: () => from[key], enumerable: true })
155
+ }
156
+ result = [to.a, to.b]
157
+ `)).toEqual([1, 2]);
158
+ });
159
+ (0, import_vitest.it)("leaves loops that never capture their binding untouched", () => {
160
+ (0, import_vitest.expect)((0, import_transformHermesLoops.transformHermesLoops)("for (let i = 0; i < 3; i++) sum += i", "test.js")).toBeNull();
161
+ });
162
+ (0, import_vitest.it)("gives a binding declared in the loop body a per-iteration copy", () => {
163
+ (0, import_vitest.expect)(run(`
164
+ var names = ['createAnimatedNode', 'addListener', 'removeListener']
165
+ var wrappers = {}
166
+ for (var ii = 0; ii < names.length; ii++) {
167
+ const methodName = names[ii]
168
+ wrappers[methodName] = () => methodName
169
+ }
170
+ result = names.map((n) => wrappers[n]())
171
+ `)).toEqual(["createAnimatedNode", "addListener", "removeListener"]);
172
+ });
173
+ (0, import_vitest.it)("gives a binding declared in a nested block of the loop body a per-iteration copy", () => {
174
+ (0, import_vitest.expect)(run(`
175
+ var fns = []
176
+ for (var i = 0; i < 3; i++) {
177
+ if (i >= 0) {
178
+ const doubled = i * 2
179
+ fns.push(() => doubled)
180
+ }
181
+ }
182
+ result = fns.map((f) => f())
183
+ `)).toEqual([0, 2, 4]);
184
+ });
185
+ (0, import_vitest.it)("keeps `this` and `arguments` meaning what they meant outside the loop", () => {
186
+ (0, import_vitest.expect)(run(`
187
+ class PropsFilter {
188
+ constructor() { this._map = { a: 'A', b: 'B' } }
189
+ filter(props) {
190
+ var out = []
191
+ for (const key in props) {
192
+ const label = key
193
+ out.push(() => this._map[label] + arguments.length)
194
+ }
195
+ return out
196
+ }
197
+ }
198
+ result = new PropsFilter().filter({ a: 1, b: 2 }).map((f) => f())
199
+ `)).toEqual(["A1", "B1"]);
200
+ });
201
+ (0, import_vitest.it)("leaves a loop with no lexical binding untouched", () => {
202
+ (0, import_vitest.expect)((0, import_transformHermesLoops.transformHermesLoops)("for (var i = 0; i < 3; i++) fns.push(() => i)", "test.js")).toBeNull();
203
+ });
204
+ (0, import_vitest.it)("leaves the head declaration alone so it cannot collide with a sibling", () => {
205
+ const out = (0, import_transformHermesLoops.transformHermesLoops)(`function build(gestures) {
206
+ var handlers = []
207
+ for (const gesture of gestures) { handlers.push(() => gesture) }
208
+ const gesture = gestures[0]
209
+ return gesture
210
+ }`, "Pressable.js");
211
+ (0, import_vitest.expect)(() => new vm.Script(out.code)).not.toThrow();
212
+ });
213
+ (0, import_vitest.it)("rewrites a loop in a .js file that still contains jsx", () => {
214
+ const out = (0, import_transformHermesLoops.transformHermesLoops)("for (const c of items) rows.push(() => <Row key={c} />)", "Rows.js");
215
+ (0, import_vitest.expect)(out?.code).toContain("_hermesLoop0");
216
+ });
217
+ (0, import_vitest.it)("does not rewrite a loop that reassigns its own binding", () => {
218
+ (0, import_vitest.expect)((0, import_transformHermesLoops.transformHermesLoops)("for (let i = 0; i < 3; i++) { i += 1; fns.push(() => i) }", "test.js")).toBeNull();
219
+ });
220
+ });
@@ -1,6 +1,8 @@
1
+ var __create = Object.create;
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
7
  var __export = (target, all) => {
6
8
  for (var name in all) __defProp(target, name, {
@@ -17,18 +19,28 @@ var __copyProps = (to, from, except, desc) => {
17
19
  }
18
20
  return to;
19
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));
20
31
  var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
21
32
  value: true
22
33
  }), mod);
23
34
  var transformSWC_exports = {};
24
35
  __export(transformSWC_exports, {
36
+ shouldSourceMap: () => shouldSourceMap,
37
+ transformOxc: () => transformOxc,
38
+ transformOxcStripJSX: () => transformOxcStripJSX,
25
39
  transformSWC: () => transformSWC,
26
40
  transformSWCStripJSX: () => transformSWCStripJSX
27
41
  });
28
42
  module.exports = __toCommonJS(transformSWC_exports);
29
43
  var import_node_path = require("node:path");
30
- var import_core = require("@swc/core");
31
- var import_ts_deepmerge = require("ts-deepmerge");
32
44
  var import_vite = require("vite");
33
45
  var import_configure = require("./configure.cjs");
34
46
  var import_constants = require("./constants.cjs");
@@ -41,100 +53,53 @@ async function transformSWC(id, code, options, swcOptions) {
41
53
  if (id === import_constants.runtimePublicPath) {
42
54
  return;
43
55
  }
44
- const parser = getParser(id, options.forceJSX);
45
- if (!parser) {
56
+ const lang = getLang(id, options.forceJSX);
57
+ if (!lang) {
46
58
  return;
47
59
  }
48
60
  const refresh = options.environment !== "ssr" && !options.production && !options.noHMR && !options.forceJSX && !id.includes("node_modules");
49
- const reactConfig = {
50
- refresh,
51
- development: !options.forceJSX && !options.production,
52
- runtime: "automatic",
53
- importSource: "react",
54
- ...(import_configure.configuration.enableNativewind && !id.includes("node_modules") ? {
55
- importSource: "nativewind"
56
- // pragma: 'createInteropElement',
57
- // pragmaFrag: '_InteropFragment',
58
- // swc doesnt actually change the import right
59
- // runtime: 'classic',
60
- } : {})
61
- };
62
- const transformOptions = (() => {
63
- if (options.environment === "client" || options.environment === "ssr") {
64
- return {
65
- sourceMaps: shouldSourceMap(),
66
- jsc: {
67
- target: "es2020",
68
- parser,
69
- transform: {
70
- useDefineForClassFields: true,
71
- react: reactConfig
72
- }
73
- }
74
- };
61
+ const importSource = import_configure.configuration.enableNativewind && !id.includes("node_modules") ? "nativewind" : "react";
62
+ const shouldEs5Transform = options.es5 || !process.env.VXRN_USE_BABEL_FOR_GENERATORS && import_constants.asyncGeneratorRegex.test(code);
63
+ const target = shouldEs5Transform ? "es2015" : "es2020";
64
+ const sourceMaps = swcOptions?.sourceMaps !== void 0 ? Boolean(swcOptions.sourceMaps) : shouldSourceMap();
65
+ const oxcOptions = {
66
+ lang,
67
+ target,
68
+ assumptions: {
69
+ setPublicClassFields: true,
70
+ ...(swcOptions?.assumptions || {})
71
+ },
72
+ sourcemap: sourceMaps,
73
+ jsx: {
74
+ runtime: "automatic",
75
+ development: !options.forceJSX && !options.production,
76
+ refresh: Boolean(refresh),
77
+ importSource
75
78
  }
76
- const shouldEs5Transform = options.es5 || !process.env.VXRN_USE_BABEL_FOR_GENERATORS && import_constants.asyncGeneratorRegex.test(code);
77
- const opts = shouldEs5Transform ? {
78
- jsc: {
79
- parser,
80
- target: "es5",
81
- transform: {
82
- useDefineForClassFields: true,
83
- react: reactConfig
84
- }
85
- }
86
- } : {
87
- ...(!options.forceJSX && {
88
- env: SWC_ENV
89
- }),
90
- jsc: {
91
- ...(options.forceJSX && {
92
- target: "esnext"
93
- }),
94
- parser,
95
- transform: {
96
- useDefineForClassFields: true,
97
- react: reactConfig
98
- }
99
- }
100
- };
101
- return {
102
- sourceMaps: shouldSourceMap(),
103
- module: {
104
- importInterop: "none",
105
- type: "nodenext"
106
- },
107
- ...(options.mode === "serve-cjs" && {
108
- module: {
109
- importInterop: "none",
110
- type: "commonjs",
111
- strict: true
112
- }
113
- }),
114
- ...opts
115
- };
116
- })();
117
- const finalOptions = (0, import_ts_deepmerge.merge)({
118
- filename: id,
119
- swcrc: false,
120
- configFile: false,
121
- ...transformOptions
122
- }, swcOptions || {});
123
- const result = await (async () => {
79
+ };
80
+ const {
81
+ transformSync
82
+ } = await import("oxc-transform");
83
+ const result = (() => {
124
84
  try {
125
- (0, import_constants.debug)?.(`transformSWC ${id} using options:
126
- ${JSON.stringify(finalOptions, null, 2)}`);
127
- return await (0, import_core.transform)(code, finalOptions);
128
- } catch (e) {
129
- const message = e.message;
130
- const fileStartIndex = message.indexOf("\u256D\u2500[");
131
- if (fileStartIndex !== -1) {
132
- const match = message.slice(fileStartIndex).match(/:(\d+):(\d+)]/);
133
- if (match) {
134
- e.line = match[1];
135
- e.column = match[2];
85
+ (0, import_constants.debug)?.(`transformSWC (oxc) ${id} using options:
86
+ ${JSON.stringify(oxcOptions, null, 2)}`);
87
+ const res = transformSync(id, code, oxcOptions);
88
+ if (res.errors?.length) {
89
+ const err = res.errors[0];
90
+ const error = new Error(err.message + (err.codeframe ? `
91
+ ${err.codeframe}` : ""));
92
+ if (err.labels?.[0]) {
93
+ error.start = err.labels[0].start;
94
+ error.end = err.labels[0].end;
136
95
  }
96
+ throw error;
137
97
  }
98
+ return {
99
+ code: res.code,
100
+ map: res.map
101
+ };
102
+ } catch (e) {
138
103
  throw e;
139
104
  }
140
105
  })();
@@ -145,7 +110,7 @@ ${result.code}`;
145
110
  }
146
111
  }
147
112
  const hasRefreshRuntime = refresh && refreshContentRE.test(result.code);
148
- if (options.fixNonTypeSpecificImports || id.includes("node_modules") && parser.syntax === "typescript") {
113
+ if (options.fixNonTypeSpecificImports || id.includes("node_modules") && (lang === "ts" || lang === "tsx")) {
149
114
  const typeExportsMatch = code.match(/^\s*export\s+type\s+([^\s]+)/gi);
150
115
  if (typeExportsMatch) {
151
116
  for (const typeExport of Array.from(typeExportsMatch)) {
@@ -168,6 +133,7 @@ ${fakeExport}
168
133
  }
169
134
  return result;
170
135
  }
136
+ const transformOxc = transformSWC;
171
137
  function wrapSourceInRefreshRuntime(id, result, options, hasRefreshRuntime) {
172
138
  if (options.environment === "ssr") {
173
139
  return result;
@@ -178,7 +144,9 @@ function wrapSourceInRefreshRuntime(id, result, options, hasRefreshRuntime) {
178
144
  return wrapSourceInRefreshRuntimeNative(id, result, options, hasRefreshRuntime);
179
145
  }
180
146
  function wrapSourceInRefreshRuntimeWeb(id, result, hasRefreshRuntime) {
181
- const sourceMap = result.map ? JSON.parse(result.map) : void 0;
147
+ const sourceMap = result.map ? typeof result.map === "string" ? JSON.parse(result.map) : {
148
+ ...result.map
149
+ } : void 0;
182
150
  if (sourceMap) {
183
151
  sourceMap.mappings = ";;" + sourceMap.mappings;
184
152
  }
@@ -235,7 +203,9 @@ globalThis.$RefreshReg$ = (type, id) => RefreshRuntime.register(type, "${id}" +
235
203
  globalThis.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
236
204
  module.url = '${id}'
237
205
  module.hot = createHotContext(module.url)`;
238
- const sourceMap = result.map ? JSON.parse(result.map) : void 0;
206
+ const sourceMap = result.map ? typeof result.map === "string" ? JSON.parse(result.map) : {
207
+ ...result.map
208
+ } : void 0;
239
209
  if (sourceMap) {
240
210
  const prefixLen = prefixCode.split("\n").length + 1;
241
211
  sourceMap.mappings = new Array(prefixLen).fill(";").join("") + sourceMap.mappings;
@@ -258,61 +228,56 @@ ${postfixCode}
258
228
  result.code += postfixCode;
259
229
  return result;
260
230
  }
261
- const SWC_ENV = {
262
- targets: {
263
- node: "4"
264
- },
265
- // debug: true,
266
- include: [],
267
- // this breaks the uniswap app for any file with a ...spread
268
- exclude: ["transform-spread", "transform-destructuring", "transform-object-rest-spread",
269
- // `transform-async-to-generator` is relying on `transform-destructuring`.
270
- // If we exclude `transform-destructuring` but not `transform-async-to-generator`, the SWC binary will panic
271
- // with error: `called `Option::unwrap()` on a `None` value`.
272
- // See: https://github.com/swc-project/swc/blob/v1.7.14/crates/swc_ecma_compat_es2015/src/generator.rs#L703-L705
273
- "transform-async-to-generator", "transform-regenerator"
274
- // Similar to above
275
- ]
276
- };
277
231
  const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
278
232
  function shouldSourceMap() {
279
233
  return process.env.VXRN_ENABLE_SOURCE_MAP === "1";
280
234
  }
281
- function getParser(id, forceJSX = false) {
235
+ function getLang(id, forceJSX = false) {
282
236
  if (id.endsWith("one-entry-native")) {
283
- return import_constants.parsers[".tsx"];
237
+ return "tsx";
284
238
  }
285
239
  const extension = (0, import_node_path.extname)(id);
286
- let parser = !extension ? import_constants.parsers[".js"] : import_constants.parsers[extension];
287
- if (extension === ".js" || extension === ".mjs") {
288
- if (forceJSX) {
289
- parser = import_constants.parsers[".jsx"];
290
- }
291
- if (id.includes("expo-modules-core")) {
292
- parser = import_constants.parsers[".jsx"];
240
+ if (extension === ".tsx") return "tsx";
241
+ if (extension === ".ts") return "ts";
242
+ if (extension === ".jsx" || extension === ".mdx") return "jsx";
243
+ if (extension === ".js" || extension === ".mjs" || extension === ".cjs") {
244
+ if (forceJSX || id.includes("expo-modules-core")) {
245
+ return "jsx";
293
246
  }
247
+ return "js";
248
+ }
249
+ if (!extension) {
250
+ return forceJSX ? "jsx" : "js";
294
251
  }
295
- return parser;
252
+ return void 0;
296
253
  }
297
254
  const transformSWCStripJSX = async (id, code) => {
298
- const parser = getParser(id);
299
- if (!parser) return;
300
- return await (0, import_core.transform)(code, {
301
- filename: id,
302
- swcrc: false,
303
- configFile: false,
304
- sourceMaps: shouldSourceMap(),
305
- jsc: {
306
- target: "es2019",
307
- parser,
308
- transform: {
309
- useDefineForClassFields: true,
310
- react: {
311
- development: true,
312
- runtime: "automatic",
313
- refresh: false
314
- }
315
- }
255
+ const lang = getLang(id);
256
+ if (!lang) return;
257
+ const {
258
+ transformSync
259
+ } = await import("oxc-transform");
260
+ const res = transformSync(id, code, {
261
+ lang,
262
+ target: "es2020",
263
+ assumptions: {
264
+ setPublicClassFields: true
265
+ },
266
+ sourcemap: shouldSourceMap(),
267
+ jsx: {
268
+ runtime: "automatic",
269
+ development: true,
270
+ refresh: false
316
271
  }
317
272
  });
318
- };
273
+ if (res.errors?.length) {
274
+ const err = res.errors[0];
275
+ throw new Error(err.message + (err.codeframe ? `
276
+ ${err.codeframe}` : ""));
277
+ }
278
+ return {
279
+ code: res.code,
280
+ map: res.map
281
+ };
282
+ };
283
+ const transformOxcStripJSX = transformSWCStripJSX;
@@ -0,0 +1,74 @@
1
+ var import_vitest = require("vitest");
2
+ var import_transformSWC = require("./transformSWC.cjs");
3
+ (0, import_vitest.describe)("transformSWC / transformOxc with oxc-transform for Hermes lowering", () => {
4
+ (0, import_vitest.it)("lowers class fields, private fields, and static blocks with Hermes target and assumptions", async () => {
5
+ const input = `
6
+ class Sample {
7
+ #secret = 42
8
+ publicField = 100
9
+ static {
10
+ Sample.initialized = true
11
+ }
12
+ getSecret() {
13
+ return this.#secret
14
+ }
15
+ }
16
+ `;
17
+ const res = await (0, import_transformSWC.transformSWC)("Sample.ts", input, {
18
+ environment: "ios",
19
+ mode: "build"
20
+ });
21
+ (0, import_vitest.expect)(res).toBeDefined();
22
+ (0, import_vitest.expect)(res.code).not.toContain("#secret");
23
+ (0, import_vitest.expect)(res.code).not.toMatch(/^\s*publicField\s*=/m);
24
+ (0, import_vitest.expect)(res.code).not.toContain("static {");
25
+ (0, import_vitest.expect)(res.code).toContain("this.publicField = 100");
26
+ });
27
+ (0, import_vitest.it)("lowers JSX to react automatic runtime calls", async () => {
28
+ const input = `
29
+ export function MyComponent() {
30
+ return <div className="test"><span>Hello</span></div>
31
+ }
32
+ `;
33
+ const res = await (0, import_transformSWC.transformSWC)("MyComponent.tsx", input, {
34
+ environment: "client",
35
+ mode: "build"
36
+ });
37
+ (0, import_vitest.expect)(res).toBeDefined();
38
+ (0, import_vitest.expect)(res.code).not.toContain("<div");
39
+ (0, import_vitest.expect)(res.code).not.toContain("<span>");
40
+ (0, import_vitest.expect)(res.code).toContain("_jsx");
41
+ });
42
+ (0, import_vitest.it)("respects sourcemaps option", async () => {
43
+ const input = `
44
+ class Sample {
45
+ val = 1
46
+ }
47
+ `;
48
+ const resWithoutMap = await (0, import_transformSWC.transformSWC)("Sample.ts", input, {
49
+ environment: "ios",
50
+ mode: "build"
51
+ }, {
52
+ sourceMaps: false
53
+ });
54
+ (0, import_vitest.expect)(resWithoutMap?.map).toBeUndefined();
55
+ const resWithMap = await (0, import_transformSWC.transformSWC)("Sample.ts", input, {
56
+ environment: "ios",
57
+ mode: "build"
58
+ }, {
59
+ sourceMaps: true
60
+ });
61
+ (0, import_vitest.expect)(resWithMap?.map).toBeDefined();
62
+ (0, import_vitest.expect)(resWithMap?.map.mappings).toBeDefined();
63
+ });
64
+ (0, import_vitest.it)("provides transformOxc alias", () => {
65
+ (0, import_vitest.expect)(import_transformSWC.transformOxc).toBe(import_transformSWC.transformSWC);
66
+ });
67
+ (0, import_vitest.it)("strips JSX using transformSWCStripJSX", async () => {
68
+ const input = `export const el = <div>Hello</div>`;
69
+ const res = await (0, import_transformSWC.transformSWCStripJSX)("el.tsx", input);
70
+ (0, import_vitest.expect)(res).toBeDefined();
71
+ (0, import_vitest.expect)(res.code).not.toContain("<div>");
72
+ (0, import_vitest.expect)(res.code).toContain("_jsx");
73
+ });
74
+ });