@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
@@ -9,450 +9,445 @@ import { debug, runtimePublicPath, validParsers } from "./constants.mjs";
9
9
  import { getBabelOptions, transformBabel, transformOxcReactCompiler } from "./transformBabel.mjs";
10
10
  import { shouldSourceMap } from "./transformSWC.mjs";
11
11
  import { prepareWorkletsForReactCompiler, shouldTransformWorklets, transformWorklets } from "./transformWorklets.mjs";
12
- import { getCachedTransform, logCacheStats, setCachedTransform } from "./cache.mjs";
13
- export * from "./configure.mjs";
14
- export * from "./transformBabel.mjs";
15
- export * from "./transformSWC.mjs";
16
- export * from "./transformWorklets.mjs";
17
- export * from "./reactNativeCodegen.mjs";
18
- export * from "./transformHermesLoops.mjs";
19
- export * from "./transformHermesAsync.mjs";
20
- import { clearTransformCache } from "./cache.mjs";
12
+ import { clearTransformCache, getCachedTransform, logCacheStats, setCachedTransform } from "./cache.mjs";
21
13
  import { JS_GLOBALS } from "./worklets/globals.mjs";
22
14
  import { getClosureVariables } from "./worklets/scope.mjs";
15
+
16
+ export * from "./configure.mjs"
17
+
18
+ export * from "./transformBabel.mjs"
19
+
20
+ export * from "./transformSWC.mjs"
21
+
22
+ export * from "./transformWorklets.mjs"
23
+
24
+ export * from "./reactNativeCodegen.mjs"
25
+
26
+ export * from "./transformHermesLoops.mjs"
27
+
28
+ export * from "./transformHermesAsync.mjs"
29
+
23
30
  const perfStats = {
24
- babel: {
25
- totalCalls: 0,
26
- totalTransforms: 0,
27
- totalTime: 0,
28
- byEnvironment: {}
29
- },
30
- optimizeDeps: {
31
- byEnvironment: {}
32
- }
31
+ babel: {
32
+ totalCalls: 0,
33
+ totalTransforms: 0,
34
+ totalTime: 0,
35
+ byEnvironment: {}
36
+ },
37
+ optimizeDeps: { byEnvironment: {} }
33
38
  };
34
39
  function logPerfSummary() {
35
- if (!process.env.DEBUG_COMPILER_PERF) {
36
- return;
37
- }
38
- console.info("\n\u{1F4CA} [Compiler Performance Summary]");
39
- console.info(`Babel: ${perfStats.babel.totalTransforms} transforms / ${perfStats.babel.totalCalls} calls (${(perfStats.babel.totalTransforms / Math.max(perfStats.babel.totalCalls, 1) * 100).toFixed(1)}% transform rate)`);
40
- console.info(`Babel total time: ${perfStats.babel.totalTime}ms`);
41
- for (const [env, stats] of Object.entries(perfStats.babel.byEnvironment)) {
42
- if (stats.transforms > 0) {
43
- console.info(` ${env}: ${stats.transforms} transforms, ${stats.time}ms (${(stats.time / stats.transforms).toFixed(1)}ms avg)`);
44
- }
45
- }
46
- for (const [env, stats] of Object.entries(perfStats.optimizeDeps.byEnvironment)) {
47
- const elapsed = Date.now() - stats.startTime;
48
- console.info(`optimizeDeps ${env}: checked ${stats.filesChecked} files, transformed ${stats.filesTransformed} (${elapsed}ms)`);
49
- }
40
+ if (!process.env.DEBUG_COMPILER_PERF) {
41
+ return;
42
+ }
43
+ console.info("\n📊 [Compiler Performance Summary]");
44
+ console.info(`Babel: ${perfStats.babel.totalTransforms} transforms / ${perfStats.babel.totalCalls} calls (${(perfStats.babel.totalTransforms / Math.max(perfStats.babel.totalCalls, 1) * 100).toFixed(1)}% transform rate)`);
45
+ console.info(`Babel total time: ${perfStats.babel.totalTime}ms`);
46
+ for (const [env, stats] of Object.entries(perfStats.babel.byEnvironment)) {
47
+ if (stats.transforms > 0) {
48
+ console.info(` ${env}: ${stats.transforms} transforms, ${stats.time}ms (${(stats.time / stats.transforms).toFixed(1)}ms avg)`);
49
+ }
50
+ }
51
+ for (const [env, stats] of Object.entries(perfStats.optimizeDeps.byEnvironment)) {
52
+ const elapsed = Date.now() - stats.startTime;
53
+ console.info(`optimizeDeps ${env}: checked ${stats.filesChecked} files, transformed ${stats.filesTransformed} (${elapsed}ms)`);
54
+ }
50
55
  }
51
56
  const compilerLog = {
52
- compiled: 0,
53
- cached: 0,
54
- time: 0,
55
- // typed loose because the dom lib types setTimeout as number, but node gives
56
- // us a Timeout we need to unref so the debounce never holds the process open
57
- timer: null
57
+ compiled: 0,
58
+ cached: 0,
59
+ time: 0,
60
+ timer: null
58
61
  };
59
62
  function logCompiled(cached, ms) {
60
- if (cached) {
61
- compilerLog.cached++;
62
- } else {
63
- compilerLog.compiled++;
64
- compilerLog.time += ms;
65
- }
66
- if (compilerLog.timer) clearTimeout(compilerLog.timer);
67
- compilerLog.timer = setTimeout(() => {
68
- const total = compilerLog.compiled + compilerLog.cached;
69
- const detail = compilerLog.compiled ? `${compilerLog.compiled} compiled in ${compilerLog.time}ms${compilerLog.cached ? `, ${compilerLog.cached} cached` : ""}` : `all cached`;
70
- console.info(` \u{1FA84} [compiler] ${total} file${total === 1 ? "" : "s"} (${detail})`);
71
- compilerLog.compiled = 0;
72
- compilerLog.cached = 0;
73
- compilerLog.time = 0;
74
- compilerLog.timer = null;
75
- }, 500);
76
- compilerLog.timer.unref?.();
63
+ if (cached) {
64
+ compilerLog.cached++;
65
+ } else {
66
+ compilerLog.compiled++;
67
+ compilerLog.time += ms;
68
+ }
69
+ if (compilerLog.timer) clearTimeout(compilerLog.timer);
70
+ compilerLog.timer = setTimeout(() => {
71
+ const total = compilerLog.compiled + compilerLog.cached;
72
+ const detail = compilerLog.compiled ? `${compilerLog.compiled} compiled in ${compilerLog.time}ms${compilerLog.cached ? `, ${compilerLog.cached} cached` : ""}` : `all cached`;
73
+ console.info(` \u{1FA84} [compiler] ${total} file${total === 1 ? "" : "s"} (${detail})`);
74
+ compilerLog.compiled = 0;
75
+ compilerLog.cached = 0;
76
+ compilerLog.time = 0;
77
+ compilerLog.timer = null;
78
+ }, 500);
79
+ compilerLog.timer.unref?.();
77
80
  }
78
- async function performBabelTransform({
79
- id,
80
- code,
81
- projectRoot,
82
- environment,
83
- production,
84
- reactForRNVersion,
85
- optionsIn
86
- }) {
87
- perfStats.babel.totalCalls++;
88
- if (!perfStats.babel.byEnvironment[environment]) {
89
- perfStats.babel.byEnvironment[environment] = {
90
- calls: 0,
91
- transforms: 0,
92
- time: 0
93
- };
94
- }
95
- perfStats.babel.byEnvironment[environment].calls++;
96
- const transformProps = {
97
- id,
98
- code,
99
- projectRoot,
100
- development: !production,
101
- environment,
102
- reactForRNVersion
103
- };
104
- const userTransform = optionsIn?.transform?.(transformProps);
105
- if (userTransform === false) {
106
- return null;
107
- }
108
- if (userTransform !== "swc" && userTransform !== "oxc") {
109
- const babelOptions = getBabelOptions({
110
- ...transformProps,
111
- userSetting: userTransform
112
- });
113
- if (babelOptions) {
114
- const hasCompilerPlugin = babelOptions.plugins?.some(x => Array.isArray(x) && x[0] === "babel-plugin-react-compiler");
115
- const cached = getCachedTransform(id, code, environment);
116
- if (cached) {
117
- perfStats.babel.byEnvironment[environment].transforms++;
118
- if (hasCompilerPlugin && (cached.code.includes("react/compiler-runtime") || cached.code.includes("react-compiler-runtime"))) {
119
- debug?.(` \u{1FA84} [compiler] ${relative(process.cwd(), id)} (cached)`);
120
- logCompiled(true, 0);
121
- }
122
- debug?.(`[babel/cached] ${id}`);
123
- return cached;
124
- }
125
- const isWorkletPlugin = entry => {
126
- if (!entry) return false;
127
- const name = typeof entry === "string" ? entry : Array.isArray(entry) ? entry[0] : null;
128
- return typeof name === "string" && (name.includes("react-native-worklets") || name.includes("react-native-reanimated"));
129
- };
130
- const workletEntry = babelOptions.plugins?.find(isWorkletPlugin);
131
- const workletPluginOptions = Array.isArray(workletEntry) ? workletEntry[1] : void 0;
132
- const useWorklets = isNativeWorkletsEnabled() && (Boolean(workletEntry) || shouldTransformWorklets({
133
- id,
134
- code
135
- }));
136
- let curCode = code;
137
- let workletPreparation = null;
138
- let compilerOut = null;
139
- const compilerPluginIndex = babelOptions.plugins?.findIndex(x => Array.isArray(x) && x[0] === "babel-plugin-react-compiler") ?? -1;
140
- if (compilerPluginIndex !== -1) {
141
- if (useWorklets) {
142
- workletPreparation = prepareWorkletsForReactCompiler(id, curCode, shouldSourceMap());
143
- if (workletPreparation) curCode = workletPreparation.code;
144
- }
145
- const compilerTarget = babelOptions.plugins[compilerPluginIndex][1]?.target ?? "19";
146
- compilerOut = await transformOxcReactCompiler(id, curCode, compilerTarget, shouldSourceMap());
147
- if (compilerOut) curCode = compilerOut.code;
148
- babelOptions.plugins.splice(compilerPluginIndex, 1);
149
- }
150
- let workletsOut = null;
151
- if (useWorklets) {
152
- workletsOut = await transformWorklets(id, curCode, shouldSourceMap(), {
153
- projectRoot,
154
- ...workletPluginOptions
155
- });
156
- babelOptions.plugins = babelOptions.plugins?.filter(entry => !isWorkletPlugin(entry));
157
- curCode = workletsOut.code;
158
- }
159
- const startTime = Date.now();
160
- let babelOut = null;
161
- if (babelOptions.plugins?.length === 0) {
162
- let finalMap = void 0;
163
- if (shouldSourceMap()) {
164
- const intermediateMaps = [workletPreparation?.map, compilerOut?.map, workletsOut?.map].filter(Boolean);
165
- if (intermediateMaps.length === 1) {
166
- finalMap = intermediateMaps[0];
167
- } else if (intermediateMaps.length > 1) {
168
- const remapping = (await import("@jridgewell/remapping")).default;
169
- finalMap = remapping(intermediateMaps.slice().reverse(), () => null);
170
- }
171
- }
172
- babelOut = {
173
- code: curCode,
174
- map: finalMap
175
- };
176
- } else {
177
- const babelOptsWithMap = shouldSourceMap() && (workletsOut?.map || compilerOut?.map || workletPreparation?.map) ? {
178
- ...babelOptions,
179
- sourceMaps: true
180
- } : babelOptions;
181
- babelOut = await transformBabel(id, curCode, babelOptsWithMap);
182
- if (shouldSourceMap() && babelOut?.map) {
183
- const priorMaps = [workletPreparation?.map, compilerOut?.map, workletsOut?.map].filter(Boolean);
184
- if (priorMaps.length > 0) {
185
- const remapping = (await import("@jridgewell/remapping")).default;
186
- babelOut.map = remapping([babelOut.map, ...priorMaps.slice().reverse()], () => null);
187
- }
188
- }
189
- }
190
- const babelTime = Date.now() - startTime;
191
- if (babelOut?.code) {
192
- perfStats.babel.totalTransforms++;
193
- perfStats.babel.totalTime += babelTime;
194
- perfStats.babel.byEnvironment[environment].transforms++;
195
- perfStats.babel.byEnvironment[environment].time += babelTime;
196
- if (hasCompilerPlugin && (babelOut.code.includes("react/compiler-runtime") || babelOut.code.includes("react-compiler-runtime"))) {
197
- debug?.(` \u{1FA84} [compiler] ${relative(process.cwd(), id)} (${babelTime}ms)`);
198
- logCompiled(false, babelTime);
199
- }
200
- debug?.(`[babel] ${id}`);
201
- const outCode = `${babelOut.code}
81
+ async function performBabelTransform({ id, code, projectRoot, environment, production, reactForRNVersion, optionsIn }) {
82
+ perfStats.babel.totalCalls++;
83
+ if (!perfStats.babel.byEnvironment[environment]) {
84
+ perfStats.babel.byEnvironment[environment] = {
85
+ calls: 0,
86
+ transforms: 0,
87
+ time: 0
88
+ };
89
+ }
90
+ perfStats.babel.byEnvironment[environment].calls++;
91
+ const transformProps = {
92
+ id,
93
+ code,
94
+ projectRoot,
95
+ development: !production,
96
+ environment,
97
+ reactForRNVersion
98
+ };
99
+ const userTransform = optionsIn?.transform?.(transformProps);
100
+ if (userTransform === false) {
101
+ return null;
102
+ }
103
+ if (userTransform !== "swc" && userTransform !== "oxc") {
104
+ const babelOptions = getBabelOptions({
105
+ ...transformProps,
106
+ userSetting: userTransform
107
+ });
108
+ if (babelOptions) {
109
+ const hasCompilerPlugin = babelOptions.plugins?.some((x) => Array.isArray(x) && x[0] === "babel-plugin-react-compiler");
110
+ const userBabelConfigPath = typeof babelOptions.configFile === "string" ? babelOptions.configFile : null;
111
+ const cached = getCachedTransform(id, code, environment, userBabelConfigPath);
112
+ if (cached) {
113
+ perfStats.babel.byEnvironment[environment].transforms++;
114
+ if (hasCompilerPlugin && (cached.code.includes("react/compiler-runtime") || cached.code.includes("react-compiler-runtime"))) {
115
+ debug?.(` \u{1FA84} [compiler] ${relative(process.cwd(), id)} (cached)`);
116
+ logCompiled(true, 0);
117
+ }
118
+ debug?.(`[babel/cached] ${id}`);
119
+ return cached;
120
+ }
121
+ const isWorkletPlugin = (entry) => {
122
+ if (!entry) return false;
123
+ const name = typeof entry === "string" ? entry : Array.isArray(entry) ? entry[0] : null;
124
+ return typeof name === "string" && (name.includes("react-native-worklets") || name.includes("react-native-reanimated"));
125
+ };
126
+ const workletEntry = babelOptions.plugins?.find(isWorkletPlugin);
127
+ const workletPluginOptions = Array.isArray(workletEntry) ? workletEntry[1] : void 0;
128
+ const useWorklets = isNativeWorkletsEnabled() && (Boolean(workletEntry) || shouldTransformWorklets({
129
+ id,
130
+ code
131
+ }));
132
+ let curCode = code;
133
+ let workletPreparation = null;
134
+ let compilerOut = null;
135
+ const compilerPluginIndex = babelOptions.plugins?.findIndex((x) => Array.isArray(x) && x[0] === "babel-plugin-react-compiler") ?? -1;
136
+ if (compilerPluginIndex !== -1) {
137
+ const compilerPluginConfig = babelOptions.plugins[compilerPluginIndex];
138
+ const compilerPluginOptions = compilerPluginConfig[1] || {};
139
+ const useBabelCompiler = compilerPluginOptions.compiler === "babel" || process.env.ONE_REACT_COMPILER === "babel" || process.env.VXRN_REACT_COMPILER === "babel";
140
+ if (!useBabelCompiler) {
141
+ if (useWorklets) {
142
+ workletPreparation = prepareWorkletsForReactCompiler(id, curCode, shouldSourceMap());
143
+ if (workletPreparation) curCode = workletPreparation.code;
144
+ }
145
+ compilerOut = await transformOxcReactCompiler(id, curCode, compilerPluginOptions, shouldSourceMap());
146
+ if (compilerOut) curCode = compilerOut.code;
147
+ babelOptions.plugins.splice(compilerPluginIndex, 1);
148
+ }
149
+ }
150
+ let workletsOut = null;
151
+ if (useWorklets) {
152
+ workletsOut = await transformWorklets(id, curCode, shouldSourceMap(), {
153
+ projectRoot,
154
+ ...workletPluginOptions
155
+ });
156
+ babelOptions.plugins = babelOptions.plugins?.filter((entry) => !isWorkletPlugin(entry));
157
+ curCode = workletsOut.code;
158
+ }
159
+ const startTime = Date.now();
160
+ let babelOut = null;
161
+ if (babelOptions.plugins?.length === 0 && !babelOptions.configFile && !babelOptions.babelrc) {
162
+ let finalMap = void 0;
163
+ if (shouldSourceMap()) {
164
+ const intermediateMaps = [
165
+ workletPreparation?.map,
166
+ compilerOut?.map,
167
+ workletsOut?.map
168
+ ].filter(Boolean);
169
+ if (intermediateMaps.length === 1) {
170
+ finalMap = intermediateMaps[0];
171
+ } else if (intermediateMaps.length > 1) {
172
+ const remapping = (await import("@jridgewell/remapping")).default;
173
+ finalMap = remapping(intermediateMaps.slice().reverse(), () => null);
174
+ }
175
+ }
176
+ babelOut = {
177
+ code: curCode,
178
+ map: finalMap
179
+ };
180
+ } else {
181
+ const babelOptsWithMap = shouldSourceMap() && (workletsOut?.map || compilerOut?.map || workletPreparation?.map) ? {
182
+ ...babelOptions,
183
+ sourceMaps: true
184
+ } : babelOptions;
185
+ babelOut = await transformBabel(id, curCode, babelOptsWithMap);
186
+ if (shouldSourceMap() && babelOut?.map) {
187
+ const priorMaps = [
188
+ workletPreparation?.map,
189
+ compilerOut?.map,
190
+ workletsOut?.map
191
+ ].filter(Boolean);
192
+ if (priorMaps.length > 0) {
193
+ const remapping = (await import("@jridgewell/remapping")).default;
194
+ babelOut.map = remapping([babelOut.map, ...priorMaps.slice().reverse()], () => null);
195
+ }
196
+ }
197
+ }
198
+ const babelTime = Date.now() - startTime;
199
+ if (babelOut?.code) {
200
+ perfStats.babel.totalTransforms++;
201
+ perfStats.babel.totalTime += babelTime;
202
+ perfStats.babel.byEnvironment[environment].transforms++;
203
+ perfStats.babel.byEnvironment[environment].time += babelTime;
204
+ if (hasCompilerPlugin && (babelOut.code.includes("react/compiler-runtime") || babelOut.code.includes("react-compiler-runtime"))) {
205
+ debug?.(` \u{1FA84} [compiler] ${relative(process.cwd(), id)} (${babelTime}ms)`);
206
+ logCompiled(false, babelTime);
207
+ }
208
+ debug?.(`[babel] ${id}`);
209
+ const outCode = `${babelOut.code}
202
210
  // vxrn-did-babel`;
203
- const result = {
204
- code: outCode,
205
- map: babelOut.map
206
- };
207
- setCachedTransform(id, code, result, environment);
208
- return result;
209
- }
210
- }
211
- }
212
- return null;
211
+ const result = {
212
+ code: outCode,
213
+ map: babelOut.map
214
+ };
215
+ setCachedTransform(id, code, result, environment, userBabelConfigPath);
216
+ return result;
217
+ }
218
+ }
219
+ }
220
+ return null;
213
221
  }
214
222
  async function createVXRNCompilerPlugin(optionsIn) {
215
- const reactVersion = await (async () => {
216
- const path = resolvePath("react/package.json");
217
- const json = JSON.parse(await readFile(path, "utf-8"));
218
- return json.version;
219
- })();
220
- const envNames = {
221
- ios: true,
222
- android: true,
223
- client: true,
224
- ssr: true
225
- };
226
- function getEnvName(name) {
227
- if (name === "worker") return "ssr";
228
- if (!envNames[name]) throw new Error(`Invalid env: ${name}`);
229
- return name;
230
- }
231
- const reactForRNVersion = reactVersion.split(".")[0];
232
- const cssTransformCache = /* @__PURE__ */new Map();
233
- const rolldownPath = resolvePath("rolldown");
234
- const rolldownNodeMods = rolldownPath.slice(0, rolldownPath.indexOf(sep + "node_modules"));
235
- let config;
236
- return [{
237
- name: "one:compiler-resolve-refresh-runtime",
238
- apply: "serve",
239
- enforce: "pre",
240
- // Run before Vite default resolve to avoid syscalls
241
- resolveId: id => id === runtimePublicPath || id === `${runtimePublicPath}.map` ? id : void 0,
242
- load: id => {
243
- const basePath = dirname(fileURLToPath(import.meta.url));
244
- if (id === runtimePublicPath) {
245
- return readFileSync(join(basePath, "refresh-runtime.mjs"), "utf-8").replace(/\/\/# sourceMappingURL=.*/, "");
246
- }
247
- if (id === `${runtimePublicPath}.map`) {
248
- return JSON.stringify({
249
- version: 3,
250
- sources: [],
251
- mappings: ""
252
- });
253
- }
254
- return void 0;
255
- }
256
- }, {
257
- name: `one:compiler-css-to-js`,
258
- // only .css files can match, so let rust reject everything else
259
- transform: {
260
- filter: {
261
- id: /\.css$/
262
- },
263
- handler(codeIn, id) {
264
- const environment = getEnvName(this.environment.name);
265
- if (configuration.enableNativeCSS && (environment === "ios" || environment === "android")) {
266
- {
267
- const data = JSON.stringify(cssToReactNativeRuntime(codeIn, {
268
- inlineRem: 16
269
- }));
270
- const code = `require("nativewind/dist/index.js").__require().StyleSheet.registerCompiled(${data})`;
271
- const newId = `${id}.js`;
272
- const cssId = newId.replace(rolldownNodeMods + sep, "");
273
- cssTransformCache.set(cssId, code);
274
- return {
275
- code,
276
- id: newId,
277
- map: null
278
- };
279
- }
280
- }
281
- }
282
- },
283
- generateBundle(_, bundle) {
284
- const environment = getEnvName(this.environment.name);
285
- if (configuration.enableNativeCSS && (environment === "ios" || environment === "android")) {
286
- const rootJSName = Object.keys(bundle).find(i => {
287
- const chunk = bundle[i];
288
- return chunk.type == "chunk" && chunk.fileName.match(/.[cm]?js(?:\?.+)?$/) != null;
289
- });
290
- if (!rootJSName) {
291
- throw new Error(`Can't find root js, internal one error`);
292
- }
293
- const rootJS = bundle[rootJSName];
294
- const cssAssets = Object.keys(bundle).filter(i => bundle[i].fileName.endsWith(".css.js"));
295
- for (const name of cssAssets) {
296
- delete bundle[name];
297
- const jsCSS = cssTransformCache.get(name);
298
- rootJS.code = `
223
+ const reactVersion = await (async () => {
224
+ const path = resolvePath("react/package.json");
225
+ const json = JSON.parse(await readFile(path, "utf-8"));
226
+ return json.version;
227
+ })();
228
+ const envNames = {
229
+ ios: true,
230
+ android: true,
231
+ client: true,
232
+ ssr: true
233
+ };
234
+ function getEnvName(name) {
235
+ if (name === "worker") return "ssr";
236
+ if (!envNames[name]) throw new Error(`Invalid env: ${name}`);
237
+ return name;
238
+ }
239
+ const reactForRNVersion = reactVersion.split(".")[0];
240
+ const cssTransformCache = /* @__PURE__ */ new Map();
241
+ const rolldownPath = resolvePath("rolldown");
242
+ const rolldownNodeMods = rolldownPath.slice(0, rolldownPath.indexOf(sep + "node_modules"));
243
+ let config;
244
+ return [
245
+ {
246
+ name: "one:compiler-resolve-refresh-runtime",
247
+ apply: "serve",
248
+ enforce: "pre",
249
+ resolveId: (id) => id === runtimePublicPath || id === `${runtimePublicPath}.map` ? id : void 0,
250
+ load: (id) => {
251
+ const basePath = dirname(fileURLToPath(import.meta.url));
252
+ if (id === runtimePublicPath) {
253
+ return readFileSync(join(basePath, "refresh-runtime.mjs"), "utf-8").replace(/\/\/# sourceMappingURL=.*/, "");
254
+ }
255
+ if (id === `${runtimePublicPath}.map`) {
256
+ return JSON.stringify({
257
+ version: 3,
258
+ sources: [],
259
+ mappings: ""
260
+ });
261
+ }
262
+ return void 0;
263
+ }
264
+ },
265
+ {
266
+ name: `one:compiler-css-to-js`,
267
+ transform: {
268
+ filter: { id: /\.css$/ },
269
+ handler(codeIn, id) {
270
+ const environment = getEnvName(this.environment.name);
271
+ if (configuration.enableNativeCSS && (environment === "ios" || environment === "android")) {
272
+ {
273
+ const data = JSON.stringify(cssToReactNativeRuntime(codeIn, { inlineRem: 16 }));
274
+ const code = `require("nativewind/dist/index.js").require().StyleSheet.registerCompiled(${data})`;
275
+ const newId = `${id}.js`;
276
+ const cssId = newId.replace(rolldownNodeMods + sep, "");
277
+ cssTransformCache.set(cssId, code);
278
+ return {
279
+ code,
280
+ id: newId,
281
+ map: null
282
+ };
283
+ }
284
+ }
285
+ }
286
+ },
287
+ generateBundle(_, bundle) {
288
+ const environment = getEnvName(this.environment.name);
289
+ if (configuration.enableNativeCSS && (environment === "ios" || environment === "android")) {
290
+ const rootJSName = Object.keys(bundle).find((i) => {
291
+ const chunk = bundle[i];
292
+ return chunk.type == "chunk" && chunk.fileName.match(/.[cm]?js(?:\?.+)?$/) != null;
293
+ });
294
+ if (!rootJSName) {
295
+ throw new Error(`Can't find root js, internal one error`);
296
+ }
297
+ const rootJS = bundle[rootJSName];
298
+ const cssAssets = Object.keys(bundle).filter((i) => bundle[i].fileName.endsWith(".css.js"));
299
+ for (const name of cssAssets) {
300
+ delete bundle[name];
301
+ const jsCSS = cssTransformCache.get(name);
302
+ rootJS.code = `
299
303
  ${jsCSS}
300
304
  ${rootJS.code}
301
305
  `;
302
- }
303
- }
304
- }
305
- }, {
306
- name: "one:compiler",
307
- enforce: "pre",
308
- config: () => {
309
- const nodeModulesFilter = /node_modules[/\\].*\.(tsx?|jsx?|mjs|cjs)$/;
310
- const createEnvironmentConfig = environment => {
311
- if (!perfStats.optimizeDeps.byEnvironment[environment]) {
312
- perfStats.optimizeDeps.byEnvironment[environment] = {
313
- filesChecked: 0,
314
- filesTransformed: 0,
315
- startTime: Date.now()
316
- };
317
- }
318
- return {
319
- optimizeDeps: {
320
- rolldownOptions: {
321
- plugins: [{
322
- name: `transform-before-optimize-deps-${environment}`,
323
- async transform(code, id) {
324
- if (!nodeModulesFilter.test(id)) {
325
- return null;
326
- }
327
- perfStats.optimizeDeps.byEnvironment[environment].filesChecked++;
328
- const production = process.env.NODE_ENV === "production" || process.env.NODE_ENV === "test";
329
- debug?.(`[rolldown optimizeDeps] ${id}`);
330
- const result = await performBabelTransform({
331
- id,
332
- code,
333
- projectRoot: config.root,
334
- environment,
335
- production,
336
- reactForRNVersion,
337
- optionsIn
338
- });
339
- if (!result) {
340
- return null;
341
- }
342
- perfStats.optimizeDeps.byEnvironment[environment].filesTransformed++;
343
- return {
344
- code: result.code,
345
- map: result.map
346
- };
347
- },
348
- buildEnd() {
349
- if (process.env.DEBUG_COMPILER_PERF) {
350
- const stats = perfStats.optimizeDeps.byEnvironment[environment];
351
- const elapsed = Date.now() - stats.startTime;
352
- console.info(`[optimizeDeps ${environment}] Done: ${stats.filesChecked} files checked, ${stats.filesTransformed} transformed (${elapsed}ms)`);
353
- }
354
- const allDone = Object.keys(perfStats.optimizeDeps.byEnvironment).length >= 2;
355
- if (allDone) {
356
- logCacheStats();
357
- logPerfSummary();
358
- }
359
- }
360
- }]
361
- }
362
- },
363
- define: {
364
- "process.env.NATIVEWIND_OS": JSON.stringify(environment === "ios" || environment === "android" ? "native" : "web")
365
- }
366
- };
367
- };
368
- return {
369
- environments: {
370
- ios: createEnvironmentConfig("ios"),
371
- android: createEnvironmentConfig("android"),
372
- client: createEnvironmentConfig("client"),
373
- ssr: createEnvironmentConfig("ssr")
374
- }
375
- };
376
- },
377
- configResolved(resolvedConfig) {
378
- config = resolvedConfig;
379
- },
380
- async transform(codeIn, _id) {
381
- let code = codeIn;
382
- const environment = getEnvName(this.environment.name);
383
- const isNative = environment === "ios" || environment === "android";
384
- const production = config.command === "build" || process.env.NODE_ENV === "production" || JSON.parse(this.environment.config?.define?.["process.env.NODE_ENV"] || '""') === "production";
385
- const isEntry = _id.includes("one-entry-native");
386
- if (isEntry) {
387
- if (isNative && !production) {
388
- code = `import '@vxrn/vite-native-client'
306
+ }
307
+ }
308
+ }
309
+ },
310
+ {
311
+ name: "one:compiler",
312
+ enforce: "pre",
313
+ config: () => {
314
+ const nodeModulesFilter = /node_modules[/\\].*\.(tsx?|jsx?|mjs|cjs)$/;
315
+ const createEnvironmentConfig = (environment) => {
316
+ if (!perfStats.optimizeDeps.byEnvironment[environment]) {
317
+ perfStats.optimizeDeps.byEnvironment[environment] = {
318
+ filesChecked: 0,
319
+ filesTransformed: 0,
320
+ startTime: Date.now()
321
+ };
322
+ }
323
+ return {
324
+ optimizeDeps: { rolldownOptions: { plugins: [{
325
+ name: `transform-before-optimize-deps-${environment}`,
326
+ async transform(code, id) {
327
+ if (!nodeModulesFilter.test(id)) {
328
+ return null;
329
+ }
330
+ perfStats.optimizeDeps.byEnvironment[environment].filesChecked++;
331
+ const production = process.env.NODE_ENV === "production" || process.env.NODE_ENV === "test";
332
+ debug?.(`[rolldown optimizeDeps] ${id}`);
333
+ const result = await performBabelTransform({
334
+ id,
335
+ code,
336
+ projectRoot: config.root,
337
+ environment,
338
+ production,
339
+ reactForRNVersion,
340
+ optionsIn
341
+ });
342
+ if (!result) {
343
+ return null;
344
+ }
345
+ perfStats.optimizeDeps.byEnvironment[environment].filesTransformed++;
346
+ return {
347
+ code: result.code,
348
+ map: result.map
349
+ };
350
+ },
351
+ buildEnd() {
352
+ if (process.env.DEBUG_COMPILER_PERF) {
353
+ const stats = perfStats.optimizeDeps.byEnvironment[environment];
354
+ const elapsed = Date.now() - stats.startTime;
355
+ console.info(`[optimizeDeps ${environment}] Done: ${stats.filesChecked} files checked, ${stats.filesTransformed} transformed (${elapsed}ms)`);
356
+ }
357
+ const allDone = Object.keys(perfStats.optimizeDeps.byEnvironment).length >= 2;
358
+ if (allDone) {
359
+ logCacheStats();
360
+ logPerfSummary();
361
+ }
362
+ }
363
+ }] } },
364
+ define: { "process.env.NATIVEWIND_OS": JSON.stringify(environment === "ios" || environment === "android" ? "native" : "web") }
365
+ };
366
+ };
367
+ return { environments: {
368
+ ios: createEnvironmentConfig("ios"),
369
+ android: createEnvironmentConfig("android"),
370
+ client: createEnvironmentConfig("client"),
371
+ ssr: createEnvironmentConfig("ssr")
372
+ } };
373
+ },
374
+ configResolved(resolvedConfig) {
375
+ config = resolvedConfig;
376
+ },
377
+ async transform(codeIn, _id) {
378
+ let code = codeIn;
379
+ const environment = getEnvName(this.environment.name);
380
+ const isNative = environment === "ios" || environment === "android";
381
+ const production = config.command === "build" || process.env.NODE_ENV === "production" || JSON.parse(this.environment.config?.define?.["process.env.NODE_ENV"] || "\"\"") === "production";
382
+ const isEntry = _id.includes("one-entry-native");
383
+ if (isEntry) {
384
+ if (isNative && !production) {
385
+ code = `import '@vxrn/vite-native-client'
389
386
  ${code}`;
390
- }
391
- if (isNative && configuration.enableNativewind) {
392
- code = `import * as x from 'nativewind'
387
+ }
388
+ if (isNative && configuration.enableNativewind) {
389
+ code = `import * as x from 'nativewind'
393
390
  ${code}`;
394
- }
395
- return code;
396
- }
397
- const id = _id.split("?")[0];
398
- const extension = extname(id);
399
- if (extension === ".css" || !validParsers.has(extension)) {
400
- return;
401
- }
402
- if (id.includes(`virtual:`)) {
403
- return;
404
- }
405
- if (codeIn.endsWith(`// vxrn-did-babel`)) {
406
- debug?.(`[skip babel] ${id}`);
407
- return;
408
- }
409
- return performBabelTransform({
410
- id,
411
- code: codeIn,
412
- projectRoot: config.root,
413
- environment,
414
- production,
415
- reactForRNVersion,
416
- optionsIn
417
- });
418
- }
419
- },
420
- // wraps client-side TSX/JSX with React Refresh preamble + import.meta.hot.accept
421
- // runs after vite:oxc (no enforce:'pre') so it sees the already-transformed code
422
- {
423
- name: "one:react-refresh-web",
424
- apply: "serve",
425
- transform(code, _id) {
426
- if (this.environment.name !== "client") return;
427
- if (code.includes(runtimePublicPath)) return;
428
- const id = _id.split("?")[0];
429
- if (id.includes("node_modules")) return;
430
- if (id.includes("virtual:")) return;
431
- if (id === runtimePublicPath) return;
432
- const ext = extname(id);
433
- if (ext !== ".tsx" && ext !== ".jsx") return;
434
- const hasRefreshCalls = /\$RefreshReg\$\(/.test(code);
435
- let out = `import * as RefreshRuntime from "${runtimePublicPath}";
391
+ }
392
+ return code;
393
+ }
394
+ const id = _id.split("?")[0];
395
+ const extension = extname(id);
396
+ if (extension === ".css" || !validParsers.has(extension)) {
397
+ return;
398
+ }
399
+ if (id.includes(`virtual:`)) {
400
+ return;
401
+ }
402
+ if (codeIn.endsWith(`// vxrn-did-babel`)) {
403
+ debug?.(`[skip babel] ${id}`);
404
+ return;
405
+ }
406
+ return performBabelTransform({
407
+ id,
408
+ code: codeIn,
409
+ projectRoot: config.root,
410
+ environment,
411
+ production,
412
+ reactForRNVersion,
413
+ optionsIn
414
+ });
415
+ }
416
+ },
417
+ {
418
+ name: "one:react-refresh-web",
419
+ apply: "serve",
420
+ transform(code, _id) {
421
+ if (this.environment.name !== "client") return;
422
+ if (code.includes(runtimePublicPath)) return;
423
+ const id = _id.split("?")[0];
424
+ if (id.includes("node_modules")) return;
425
+ if (id.includes("virtual:")) return;
426
+ if (id === runtimePublicPath) return;
427
+ const ext = extname(id);
428
+ if (ext !== ".tsx" && ext !== ".jsx") return;
429
+ const hasRefreshCalls = /\$RefreshReg\$\(/.test(code);
430
+ let out = `import * as RefreshRuntime from "${runtimePublicPath}";
436
431
 
437
432
  `;
438
- if (hasRefreshCalls) {
439
- out += `if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
433
+ if (hasRefreshCalls) {
434
+ out += `if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
440
435
  const prevRefreshReg = window.$RefreshReg$;
441
436
  const prevRefreshSig = window.$RefreshSig$;
442
437
  window.$RefreshReg$ = RefreshRuntime.getRefreshReg("${id}");
443
438
  window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
444
439
 
445
440
  `;
446
- }
447
- out += code;
448
- if (hasRefreshCalls) {
449
- out += `
441
+ }
442
+ out += code;
443
+ if (hasRefreshCalls) {
444
+ out += `
450
445
 
451
446
  window.$RefreshReg$ = prevRefreshReg;
452
447
  window.$RefreshSig$ = prevRefreshSig;
453
448
  `;
454
- }
455
- out += `
449
+ }
450
+ out += `
456
451
  RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
457
452
  RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
458
453
  import.meta.hot.accept((nextExports) => {
@@ -462,12 +457,14 @@ RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
462
457
  });
463
458
  });
464
459
  `;
465
- return {
466
- code: out,
467
- map: null
468
- };
469
- }
470
- }];
460
+ return {
461
+ code: out,
462
+ map: null
463
+ };
464
+ }
465
+ }
466
+ ];
471
467
  }
468
+
472
469
  export { JS_GLOBALS, clearTransformCache, createVXRNCompilerPlugin, getClosureVariables };
473
470
  //# sourceMappingURL=index.mjs.map