@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
@@ -1,17 +1,14 @@
1
1
  import { extname } from 'node:path'
2
- import {
3
- type Output,
4
- type ParserConfig,
5
- type Options as SWCOptions,
6
- type TransformConfig,
7
- transform,
8
- } from '@swc/core'
9
- import { merge } from 'ts-deepmerge'
10
2
  import { normalizePath } from 'vite'
11
3
  import { configuration } from './configure'
12
- import { asyncGeneratorRegex, debug, parsers, runtimePublicPath } from './constants'
4
+ import { asyncGeneratorRegex, debug, runtimePublicPath } from './constants'
13
5
  import type { Options } from './types'
14
6
 
7
+ export interface Output {
8
+ code: string
9
+ map?: any
10
+ }
11
+
15
12
  // posix-only — id is normalized below
16
13
  const ignoreId = /node_modules\/(\.vite|vite)\//
17
14
 
@@ -19,8 +16,8 @@ export async function transformSWC(
19
16
  id: string,
20
17
  code: string,
21
18
  options: Options & { es5?: boolean },
22
- swcOptions?: SWCOptions
23
- ) {
19
+ swcOptions?: any
20
+ ): Promise<Output | undefined> {
24
21
  // unify caller contracts (Vite plugin: POSIX id; patches.ts: native id)
25
22
  id = normalizePath(id.split('?')[0]).replace(normalizePath(process.cwd()), '')
26
23
 
@@ -32,9 +29,8 @@ export async function transformSWC(
32
29
  return
33
30
  }
34
31
 
35
- const parser = getParser(id, options.forceJSX)
36
-
37
- if (!parser) {
32
+ const lang = getLang(id, options.forceJSX)
33
+ if (!lang) {
38
34
  return
39
35
  }
40
36
 
@@ -45,108 +41,62 @@ export async function transformSWC(
45
41
  !options.forceJSX &&
46
42
  !id.includes('node_modules')
47
43
 
48
- const reactConfig = {
49
- refresh,
50
- development: !options.forceJSX && !options.production,
51
- runtime: 'automatic',
52
- importSource: 'react',
53
- ...(configuration.enableNativewind && !id.includes('node_modules')
54
- ? {
55
- importSource: 'nativewind',
56
- // pragma: 'createInteropElement',
57
- // pragmaFrag: '_InteropFragment',
58
- // swc doesnt actually change the import right
59
- // runtime: 'classic',
60
- }
61
- : {}),
62
- } satisfies TransformConfig['react']
63
-
64
- const transformOptions = ((): SWCOptions => {
65
- if (options.environment === 'client' || options.environment === 'ssr') {
66
- return {
67
- sourceMaps: shouldSourceMap(),
68
- jsc: {
69
- target: 'es2020',
70
- parser,
71
- transform: {
72
- useDefineForClassFields: true,
73
- react: reactConfig,
74
- },
75
- },
76
- }
77
- }
78
-
79
- const shouldEs5Transform =
80
- options.es5 ||
81
- (!process.env.VXRN_USE_BABEL_FOR_GENERATORS && asyncGeneratorRegex.test(code))
82
-
83
- const opts: SWCOptions = shouldEs5Transform
84
- ? {
85
- jsc: {
86
- parser,
87
- target: 'es5',
88
- transform: {
89
- useDefineForClassFields: true,
90
- react: reactConfig,
91
- },
92
- },
93
- }
94
- : {
95
- ...(!options.forceJSX && { env: SWC_ENV }),
96
- jsc: {
97
- ...(options.forceJSX && { target: 'esnext' }),
98
- parser,
99
- transform: {
100
- useDefineForClassFields: true,
101
- react: reactConfig,
102
- },
103
- },
104
- }
105
-
106
- return {
107
- sourceMaps: shouldSourceMap(),
108
- module: {
109
- importInterop: 'none',
110
- type: 'nodenext',
111
- },
112
- ...(options.mode === 'serve-cjs' && {
113
- module: {
114
- importInterop: 'none',
115
- type: 'commonjs',
116
- strict: true,
117
- },
118
- }),
119
- ...opts,
120
- }
121
- })()
122
-
123
- const finalOptions = merge(
124
- {
125
- filename: id,
126
- swcrc: false,
127
- configFile: false,
128
- ...transformOptions,
44
+ const importSource =
45
+ configuration.enableNativewind && !id.includes('node_modules')
46
+ ? 'nativewind'
47
+ : 'react'
48
+
49
+ const shouldEs5Transform =
50
+ options.es5 ||
51
+ (!process.env.VXRN_USE_BABEL_FOR_GENERATORS && asyncGeneratorRegex.test(code))
52
+
53
+ const target = shouldEs5Transform ? 'es2015' : 'es2020'
54
+ const sourceMaps =
55
+ swcOptions?.sourceMaps !== undefined
56
+ ? Boolean(swcOptions.sourceMaps)
57
+ : shouldSourceMap()
58
+
59
+ const oxcOptions: import('oxc-transform').TransformOptions = {
60
+ lang,
61
+ target,
62
+ assumptions: {
63
+ setPublicClassFields: true,
64
+ ...(swcOptions?.assumptions || {}),
129
65
  },
130
- swcOptions || {}
131
- ) satisfies SWCOptions
66
+ sourcemap: sourceMaps,
67
+ jsx: {
68
+ runtime: 'automatic',
69
+ development: !options.forceJSX && !options.production,
70
+ refresh: Boolean(refresh),
71
+ importSource,
72
+ },
73
+ }
132
74
 
133
- const result: Output = await (async () => {
75
+ const { transformSync } = await import('oxc-transform')
76
+
77
+ const result: Output = (() => {
134
78
  try {
135
79
  debug?.(
136
- `transformSWC ${id} using options:\n${JSON.stringify(finalOptions, null, 2)}`
80
+ `transformSWC (oxc) ${id} using options:\n${JSON.stringify(oxcOptions, null, 2)}`
137
81
  )
138
82
 
139
- return await transform(code, finalOptions)
140
- } catch (e: any) {
141
- const message: string = e.message
142
- const fileStartIndex = message.indexOf('╭─[')
143
- if (fileStartIndex !== -1) {
144
- const match = message.slice(fileStartIndex).match(/:(\d+):(\d+)]/)
145
- if (match) {
146
- e.line = match[1]
147
- e.column = match[2]
83
+ const res = transformSync(id, code, oxcOptions)
84
+ if (res.errors?.length) {
85
+ const err = res.errors[0]
86
+ const error: any = new Error(
87
+ err.message + (err.codeframe ? `\n${err.codeframe}` : '')
88
+ )
89
+ if (err.labels?.[0]) {
90
+ error.start = err.labels[0].start
91
+ error.end = err.labels[0].end
148
92
  }
93
+ throw error
149
94
  }
95
+ return {
96
+ code: res.code,
97
+ map: res.map,
98
+ }
99
+ } catch (e: any) {
150
100
  throw e
151
101
  }
152
102
  })()
@@ -163,7 +113,7 @@ export async function transformSWC(
163
113
  // fix for node_modules that ship tsx but don't use type-specific imports
164
114
  if (
165
115
  options.fixNonTypeSpecificImports ||
166
- (id.includes('node_modules') && parser.syntax === 'typescript')
116
+ (id.includes('node_modules') && (lang === 'ts' || lang === 'tsx'))
167
117
  ) {
168
118
  // we need to keep fake objects for type exports
169
119
  const typeExportsMatch = code.match(/^\s*export\s+type\s+([^\s]+)/gi)
@@ -199,6 +149,8 @@ export async function transformSWC(
199
149
  return result
200
150
  }
201
151
 
152
+ export const transformOxc = transformSWC
153
+
202
154
  function wrapSourceInRefreshRuntime(
203
155
  id: string,
204
156
  result: Output,
@@ -219,7 +171,11 @@ function wrapSourceInRefreshRuntimeWeb(
219
171
  result: Output,
220
172
  hasRefreshRuntime: boolean
221
173
  ) {
222
- const sourceMap = result.map ? JSON.parse(result.map) : undefined
174
+ const sourceMap = result.map
175
+ ? typeof result.map === 'string'
176
+ ? JSON.parse(result.map)
177
+ : { ...result.map }
178
+ : undefined
223
179
  if (sourceMap) {
224
180
  sourceMap.mappings = ';;' + sourceMap.mappings
225
181
  }
@@ -286,7 +242,11 @@ globalThis.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
286
242
  module.url = '${id}'
287
243
  module.hot = createHotContext(module.url)`
288
244
 
289
- const sourceMap = result.map ? JSON.parse(result.map) : undefined
245
+ const sourceMap = result.map
246
+ ? typeof result.map === 'string'
247
+ ? JSON.parse(result.map)
248
+ : { ...result.map }
249
+ : undefined
290
250
 
291
251
  if (sourceMap) {
292
252
  // we need ";" equal to number of lines added to the top
@@ -314,72 +274,61 @@ ${postfixCode}
314
274
  return result
315
275
  }
316
276
 
317
- const SWC_ENV = {
318
- targets: {
319
- node: '4',
320
- },
321
- // debug: true,
322
- include: [],
323
- // this breaks the uniswap app for any file with a ...spread
324
- exclude: [
325
- 'transform-spread',
326
- 'transform-destructuring',
327
- 'transform-object-rest-spread',
328
- // `transform-async-to-generator` is relying on `transform-destructuring`.
329
- // If we exclude `transform-destructuring` but not `transform-async-to-generator`, the SWC binary will panic
330
- // with error: `called `Option::unwrap()` on a `None` value`.
331
- // See: https://github.com/swc-project/swc/blob/v1.7.14/crates/swc_ecma_compat_es2015/src/generator.rs#L703-L705
332
- 'transform-async-to-generator',
333
- 'transform-regenerator', // Similar to above
334
- ],
335
- } satisfies SWCOptions['env']
336
-
337
277
  const refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/
338
278
 
339
- function shouldSourceMap() {
279
+ export function shouldSourceMap() {
340
280
  return process.env.VXRN_ENABLE_SOURCE_MAP === '1'
341
281
  }
342
282
 
343
- function getParser(id: string, forceJSX = false) {
283
+ function getLang(id: string, forceJSX = false): 'js' | 'jsx' | 'ts' | 'tsx' | undefined {
344
284
  if (id.endsWith('one-entry-native')) {
345
- return parsers['.tsx']
285
+ return 'tsx'
346
286
  }
347
287
 
348
288
  const extension = extname(id)
349
- let parser: ParserConfig = !extension ? parsers['.js'] : parsers[extension]
350
289
 
351
- if (extension === '.js' || extension === '.mjs') {
352
- if (forceJSX) {
353
- parser = parsers['.jsx']
290
+ if (extension === '.tsx') return 'tsx'
291
+ if (extension === '.ts') return 'ts'
292
+ if (extension === '.jsx' || extension === '.mdx') return 'jsx'
293
+ if (extension === '.js' || extension === '.mjs' || extension === '.cjs') {
294
+ if (forceJSX || id.includes('expo-modules-core')) {
295
+ return 'jsx'
354
296
  }
297
+ return 'js'
298
+ }
355
299
 
356
- if (id.includes('expo-modules-core')) {
357
- parser = parsers['.jsx']
358
- }
300
+ if (!extension) {
301
+ return forceJSX ? 'jsx' : 'js'
359
302
  }
360
303
 
361
- return parser
304
+ return undefined
362
305
  }
363
306
 
364
307
  export const transformSWCStripJSX = async (id: string, code: string) => {
365
- const parser = getParser(id)
366
- if (!parser) return
367
- return await transform(code, {
368
- filename: id,
369
- swcrc: false,
370
- configFile: false,
371
- sourceMaps: shouldSourceMap(),
372
- jsc: {
373
- target: 'es2019',
374
- parser,
375
- transform: {
376
- useDefineForClassFields: true,
377
- react: {
378
- development: true,
379
- runtime: 'automatic',
380
- refresh: false,
381
- },
382
- },
308
+ const lang = getLang(id)
309
+ if (!lang) return
310
+
311
+ const { transformSync } = await import('oxc-transform')
312
+ const res = transformSync(id, code, {
313
+ lang,
314
+ target: 'es2020',
315
+ assumptions: {
316
+ setPublicClassFields: true,
317
+ },
318
+ sourcemap: shouldSourceMap(),
319
+ jsx: {
320
+ runtime: 'automatic',
321
+ development: true,
322
+ refresh: false,
383
323
  },
384
324
  })
325
+
326
+ if (res.errors?.length) {
327
+ const err = res.errors[0]
328
+ throw new Error(err.message + (err.codeframe ? `\n${err.codeframe}` : ''))
329
+ }
330
+
331
+ return { code: res.code, map: res.map as any }
385
332
  }
333
+
334
+ export const transformOxcStripJSX = transformSWCStripJSX