@vxrn/compiler 1.1.397
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.
- package/LICENSE +45 -0
- package/dist/cjs/configure.cjs +34 -0
- package/dist/cjs/configure.js +29 -0
- package/dist/cjs/configure.js.map +6 -0
- package/dist/cjs/configure.native.js +35 -0
- package/dist/cjs/configure.native.js.map +6 -0
- package/dist/cjs/constants.cjs +65 -0
- package/dist/cjs/constants.js +34 -0
- package/dist/cjs/constants.js.map +6 -0
- package/dist/cjs/constants.native.js +65 -0
- package/dist/cjs/constants.native.js.map +6 -0
- package/dist/cjs/index.cjs +124 -0
- package/dist/cjs/index.js +113 -0
- package/dist/cjs/index.js.map +6 -0
- package/dist/cjs/index.native.js +123 -0
- package/dist/cjs/index.native.js.map +6 -0
- package/dist/cjs/refresh-runtime.cjs +295 -0
- package/dist/cjs/refresh-runtime.js +296 -0
- package/dist/cjs/refresh-runtime.js.map +6 -0
- package/dist/cjs/refresh-runtime.native.js +316 -0
- package/dist/cjs/refresh-runtime.native.js.map +6 -0
- package/dist/cjs/transformBabel.cjs +111 -0
- package/dist/cjs/transformBabel.js +116 -0
- package/dist/cjs/transformBabel.js.map +6 -0
- package/dist/cjs/transformBabel.native.js +152 -0
- package/dist/cjs/transformBabel.native.js.map +6 -0
- package/dist/cjs/transformSWC.cjs +261 -0
- package/dist/cjs/transformSWC.js +256 -0
- package/dist/cjs/transformSWC.js.map +6 -0
- package/dist/cjs/transformSWC.native.js +275 -0
- package/dist/cjs/transformSWC.native.js.map +6 -0
- package/dist/cjs/types.cjs +16 -0
- package/dist/cjs/types.js +14 -0
- package/dist/cjs/types.js.map +6 -0
- package/dist/cjs/types.native.js +15 -0
- package/dist/cjs/types.native.js.map +6 -0
- package/dist/esm/configure.js +13 -0
- package/dist/esm/configure.js.map +6 -0
- package/dist/esm/configure.mjs +10 -0
- package/dist/esm/configure.mjs.map +1 -0
- package/dist/esm/configure.native.js +13 -0
- package/dist/esm/configure.native.js.map +6 -0
- package/dist/esm/constants.js +18 -0
- package/dist/esm/constants.js.map +6 -0
- package/dist/esm/constants.mjs +38 -0
- package/dist/esm/constants.mjs.map +1 -0
- package/dist/esm/constants.native.js +41 -0
- package/dist/esm/constants.native.js.map +6 -0
- package/dist/esm/index.js +102 -0
- package/dist/esm/index.js.map +6 -0
- package/dist/esm/index.mjs +99 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/index.native.js +104 -0
- package/dist/esm/index.native.js.map +6 -0
- package/dist/esm/refresh-runtime.js +280 -0
- package/dist/esm/refresh-runtime.js.map +6 -0
- package/dist/esm/refresh-runtime.mjs +266 -0
- package/dist/esm/refresh-runtime.mjs.map +1 -0
- package/dist/esm/refresh-runtime.native.js +290 -0
- package/dist/esm/refresh-runtime.native.js.map +6 -0
- package/dist/esm/transformBabel.js +95 -0
- package/dist/esm/transformBabel.js.map +6 -0
- package/dist/esm/transformBabel.mjs +76 -0
- package/dist/esm/transformBabel.mjs.map +1 -0
- package/dist/esm/transformBabel.native.js +125 -0
- package/dist/esm/transformBabel.native.js.map +6 -0
- package/dist/esm/transformSWC.js +246 -0
- package/dist/esm/transformSWC.js.map +6 -0
- package/dist/esm/transformSWC.mjs +237 -0
- package/dist/esm/transformSWC.mjs.map +1 -0
- package/dist/esm/transformSWC.native.js +257 -0
- package/dist/esm/transformSWC.native.js.map +6 -0
- package/dist/esm/types.js +1 -0
- package/dist/esm/types.js.map +6 -0
- package/dist/esm/types.mjs +2 -0
- package/dist/esm/types.mjs.map +1 -0
- package/dist/esm/types.native.js +1 -0
- package/dist/esm/types.native.js.map +6 -0
- package/package.json +54 -0
- package/src/configure.ts +17 -0
- package/src/constants.ts +21 -0
- package/src/index.ts +166 -0
- package/src/refresh-runtime.js +619 -0
- package/src/transformBabel.ts +218 -0
- package/src/transformSWC.ts +381 -0
- package/src/types.ts +36 -0
- package/types/configure.d.ts +10 -0
- package/types/configure.d.ts.map +1 -0
- package/types/constants.d.ts +9 -0
- package/types/constants.d.ts.map +1 -0
- package/types/index.d.ts +12 -0
- package/types/index.d.ts.map +1 -0
- package/types/transformBabel.d.ts +12 -0
- package/types/transformBabel.d.ts.map +1 -0
- package/types/transformSWC.d.ts +7 -0
- package/types/transformSWC.d.ts.map +1 -0
- package/types/types.d.ts +29 -0
- package/types/types.d.ts.map +1 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { transform } from "@swc/core";
|
|
2
|
+
import { extname } from "node:path";
|
|
3
|
+
import { merge } from "ts-deepmerge";
|
|
4
|
+
import { configuration } from "./configure";
|
|
5
|
+
import { asyncGeneratorRegex, debug, parsers, runtimePublicPath } from "./constants";
|
|
6
|
+
async function transformSWC(id, code, options, swcOptions) {
|
|
7
|
+
if (!id.includes(".vite") && (id = id.split("?")[0].replace(process.cwd(), ""), id !== runtimePublicPath)) {
|
|
8
|
+
var parser = getParser(id, options.forceJSX);
|
|
9
|
+
if (parser) {
|
|
10
|
+
var enableNativeCSS = configuration.enableNativeCSS && // temp fix idk why this error:
|
|
11
|
+
// node_modules/react-native-reanimated/src/component/LayoutAnimationConfig.tsx (19:9): "createInteropElement" is not exported by "../../node_modules/react-native-css-interop/dist/runtime/jsx-dev-runtime.js", imported by "node_modules/react-native-reanimated/src/component/LayoutAnimationConfig.tsx
|
|
12
|
+
!id.includes("node_modules"), refresh = options.environment !== "ssr" && !options.production && !options.noHMR && !options.forceJSX, reactConfig = {
|
|
13
|
+
refresh,
|
|
14
|
+
development: !options.forceJSX && !options.production,
|
|
15
|
+
runtime: "automatic",
|
|
16
|
+
importSource: "react",
|
|
17
|
+
...enableNativeCSS ? {
|
|
18
|
+
importSource: "react-native-css-interop",
|
|
19
|
+
pragma: "createInteropElement",
|
|
20
|
+
// swc doesnt actually change the import right
|
|
21
|
+
runtime: "classic"
|
|
22
|
+
} : {}
|
|
23
|
+
}, transformOptions = function() {
|
|
24
|
+
if (options.environment === "client" || options.environment === "ssr")
|
|
25
|
+
return {
|
|
26
|
+
sourceMaps: !0,
|
|
27
|
+
jsc: {
|
|
28
|
+
target: "es2020",
|
|
29
|
+
parser,
|
|
30
|
+
transform: {
|
|
31
|
+
useDefineForClassFields: !0,
|
|
32
|
+
react: reactConfig
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
var shouldEs5Transform = options.es5 || !process.env.VXRN_USE_BABEL_FOR_GENERATORS && asyncGeneratorRegex.test(code), opts = shouldEs5Transform ? {
|
|
37
|
+
jsc: {
|
|
38
|
+
parser,
|
|
39
|
+
target: "es5",
|
|
40
|
+
transform: {
|
|
41
|
+
useDefineForClassFields: !0,
|
|
42
|
+
react: reactConfig
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
} : {
|
|
46
|
+
...!options.forceJSX && {
|
|
47
|
+
env: SWC_ENV
|
|
48
|
+
},
|
|
49
|
+
jsc: {
|
|
50
|
+
...options.forceJSX && {
|
|
51
|
+
target: "esnext"
|
|
52
|
+
},
|
|
53
|
+
parser,
|
|
54
|
+
transform: {
|
|
55
|
+
useDefineForClassFields: !0,
|
|
56
|
+
react: reactConfig
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
return {
|
|
61
|
+
sourceMaps: shouldSourceMap(),
|
|
62
|
+
module: {
|
|
63
|
+
importInterop: "none",
|
|
64
|
+
type: "nodenext"
|
|
65
|
+
},
|
|
66
|
+
...options.mode === "serve-cjs" && {
|
|
67
|
+
module: {
|
|
68
|
+
importInterop: "none",
|
|
69
|
+
type: "commonjs",
|
|
70
|
+
strict: !0
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
...opts
|
|
74
|
+
};
|
|
75
|
+
}(), finalOptions = merge({
|
|
76
|
+
filename: id,
|
|
77
|
+
swcrc: !1,
|
|
78
|
+
configFile: !1,
|
|
79
|
+
...transformOptions
|
|
80
|
+
}, swcOptions || {}), result = await async function() {
|
|
81
|
+
try {
|
|
82
|
+
return debug === null || debug === void 0 || debug(`transformSWC ${id} using options:
|
|
83
|
+
${JSON.stringify(finalOptions, null, 2)}`), await transform(code, finalOptions);
|
|
84
|
+
} catch (e) {
|
|
85
|
+
var message = e.message, fileStartIndex = message.indexOf("\u256D\u2500[");
|
|
86
|
+
if (fileStartIndex !== -1) {
|
|
87
|
+
var match = message.slice(fileStartIndex).match(/:(\d+):(\d+)]/);
|
|
88
|
+
match && (e.line = match[1], e.column = match[2]);
|
|
89
|
+
}
|
|
90
|
+
throw e;
|
|
91
|
+
}
|
|
92
|
+
}();
|
|
93
|
+
enableNativeCSS && result.code.includes("createInteropElement") && (result.code = `import { createInteropElement } from 'react-native-css-interop/jsx-dev-runtime'
|
|
94
|
+
${result.code}`);
|
|
95
|
+
var shouldHMR = refresh && refreshContentRE.test(result.code);
|
|
96
|
+
if (options.fixNonTypeSpecificImports || id.includes("node_modules") && parser.syntax === "typescript") {
|
|
97
|
+
var typeExportsMatch = code.match(/^\s*export\s+type\s+([^\s]+)/gi);
|
|
98
|
+
if (typeExportsMatch) {
|
|
99
|
+
var _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0;
|
|
100
|
+
try {
|
|
101
|
+
for (var _iterator = Array.from(typeExportsMatch)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) {
|
|
102
|
+
var typeExport = _step.value, [_export, _type, name] = typeExport.split(/\s+/);
|
|
103
|
+
if (!name.startsWith("{") && !name.includes("<")) {
|
|
104
|
+
var alreadyExported = new RegExp(`export (const|let|class|function) ${name}\\s+`).test(result.code);
|
|
105
|
+
if (!alreadyExported) {
|
|
106
|
+
var fakeExport = `export let ${name} = {};`;
|
|
107
|
+
console.info(` \u26A0\uFE0F Fixing non-type-specifc import in node_module, this should be fixed upstream: ${fakeExport} in ${id}`), result.code += `
|
|
108
|
+
${fakeExport}
|
|
109
|
+
`;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
} catch (err) {
|
|
114
|
+
_didIteratorError = !0, _iteratorError = err;
|
|
115
|
+
} finally {
|
|
116
|
+
try {
|
|
117
|
+
!_iteratorNormalCompletion && _iterator.return != null && _iterator.return();
|
|
118
|
+
} finally {
|
|
119
|
+
if (_didIteratorError)
|
|
120
|
+
throw _iteratorError;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return !result || options.noHMR || !shouldHMR ? result : (wrapSourceInRefreshRuntime(id, result, options, shouldHMR), {
|
|
126
|
+
code: result.code
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
var SWC_ENV = {
|
|
132
|
+
targets: {
|
|
133
|
+
node: "4"
|
|
134
|
+
},
|
|
135
|
+
// debug: true,
|
|
136
|
+
include: [],
|
|
137
|
+
// this breaks the uniswap app for any file with a ...spread
|
|
138
|
+
exclude: [
|
|
139
|
+
"transform-spread",
|
|
140
|
+
"transform-destructuring",
|
|
141
|
+
"transform-object-rest-spread",
|
|
142
|
+
// `transform-async-to-generator` is relying on `transform-destructuring`.
|
|
143
|
+
// If we exclude `transform-destructuring` but not `transform-async-to-generator`, the SWC binary will panic
|
|
144
|
+
// with error: `called `Option::unwrap()` on a `None` value`.
|
|
145
|
+
// See: https://github.com/swc-project/swc/blob/v1.7.14/crates/swc_ecma_compat_es2015/src/generator.rs#L703-L705
|
|
146
|
+
"transform-async-to-generator",
|
|
147
|
+
"transform-regenerator"
|
|
148
|
+
]
|
|
149
|
+
}, refreshContentRE = /\$Refresh(?:Reg|Sig)\$\(/;
|
|
150
|
+
function shouldSourceMap() {
|
|
151
|
+
return process.env.VXRN_ENABLE_SOURCE_MAP === "1";
|
|
152
|
+
}
|
|
153
|
+
function wrapSourceInRefreshRuntime(id, result, options, shouldHMR) {
|
|
154
|
+
return options.environment === "client" || options.environment === "ssr" ? wrapSourceInRefreshRuntimeWeb(id, result, shouldHMR) : wrapSourceInRefreshRuntimeNative(id, result, options, shouldHMR);
|
|
155
|
+
}
|
|
156
|
+
function wrapSourceInRefreshRuntimeWeb(id, result, shouldHMR) {
|
|
157
|
+
var sourceMap = JSON.parse(result.map);
|
|
158
|
+
sourceMap.mappings = ";;" + sourceMap.mappings, result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
|
|
159
|
+
|
|
160
|
+
${result.code}`, shouldHMR && (sourceMap.mappings = ";;;;;;" + sourceMap.mappings, result.code = `if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
|
|
161
|
+
const prevRefreshReg = window.$RefreshReg$;
|
|
162
|
+
const prevRefreshSig = window.$RefreshSig$;
|
|
163
|
+
window.$RefreshReg$ = RefreshRuntime.getRefreshReg("${id}");
|
|
164
|
+
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
|
165
|
+
|
|
166
|
+
${result.code}
|
|
167
|
+
|
|
168
|
+
window.$RefreshReg$ = prevRefreshReg;
|
|
169
|
+
window.$RefreshSig$ = prevRefreshSig;
|
|
170
|
+
`), result.code += `
|
|
171
|
+
RefreshRuntime.__hmr_import(import.meta.url).then((currentExports) => {
|
|
172
|
+
RefreshRuntime.registerExportsForReactRefresh("${id}", currentExports);
|
|
173
|
+
import.meta.hot.accept((nextExports) => {
|
|
174
|
+
if (!nextExports) return;
|
|
175
|
+
const invalidateMessage = RefreshRuntime.validateRefreshBoundaryAndEnqueueUpdate("${id}", currentExports, nextExports);
|
|
176
|
+
if (invalidateMessage) import.meta.hot.invalidate(invalidateMessage);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
`;
|
|
180
|
+
}
|
|
181
|
+
function wrapSourceInRefreshRuntimeNative(id, result, options, shouldHMR) {
|
|
182
|
+
var prefixCode = options.mode === "build" ? `
|
|
183
|
+
import 'react-native'
|
|
184
|
+
import 'react'
|
|
185
|
+
import '@vxrn/vite-native-client'
|
|
186
|
+
` : "";
|
|
187
|
+
if (options.production)
|
|
188
|
+
return `
|
|
189
|
+
${prefixCode}
|
|
190
|
+
module.url = '${id}'
|
|
191
|
+
${result.code}
|
|
192
|
+
`;
|
|
193
|
+
shouldHMR && (result.code = `const RefreshRuntime = __cachedModules["react-refresh/cjs/react-refresh-runtime.development"];
|
|
194
|
+
const prevRefreshReg = globalThis.$RefreshReg$;
|
|
195
|
+
const prevRefreshSig = globalThis.$RefreshSig$ || (() => {
|
|
196
|
+
console.info("no react refresh setup!")
|
|
197
|
+
return (x) => x
|
|
198
|
+
});
|
|
199
|
+
globalThis.$RefreshReg$ = (type, id) => RefreshRuntime.register(type, "${id}" + " " + id);
|
|
200
|
+
globalThis.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
|
|
201
|
+
|
|
202
|
+
${prefixCode}
|
|
203
|
+
|
|
204
|
+
module.url = '${id}'
|
|
205
|
+
module.hot = createHotContext(module.url)
|
|
206
|
+
|
|
207
|
+
${result.code}
|
|
208
|
+
|
|
209
|
+
if (module.hot) {
|
|
210
|
+
globalThis.$RefreshReg$ = prevRefreshReg;
|
|
211
|
+
globalThis.$RefreshSig$ = prevRefreshSig;
|
|
212
|
+
globalThis['lastHmrExports'] = JSON.stringify(Object.keys(exports))
|
|
213
|
+
}
|
|
214
|
+
`), result.code = `${result.code}
|
|
215
|
+
|
|
216
|
+
if (module.hot) {
|
|
217
|
+
if (module.hot.accept) {
|
|
218
|
+
module.hot.accept((nextExports) => {
|
|
219
|
+
RefreshRuntime.performReactRefresh()
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
}`;
|
|
223
|
+
}
|
|
224
|
+
function getParser(id) {
|
|
225
|
+
var forceJSX = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : !1;
|
|
226
|
+
if (id.endsWith("one-entry-native"))
|
|
227
|
+
return parsers[".tsx"];
|
|
228
|
+
var extension = extname(id), parser = extension ? parsers[extension] : parsers[".js"];
|
|
229
|
+
return extension === ".js" && (forceJSX && (parser = parsers[".jsx"]), id.includes("expo-modules-core") && (parser = parsers[".jsx"])), parser;
|
|
230
|
+
}
|
|
231
|
+
var transformSWCStripJSX = async function(id, code) {
|
|
232
|
+
var parser = getParser(id);
|
|
233
|
+
if (parser)
|
|
234
|
+
return await transform(code, {
|
|
235
|
+
filename: id,
|
|
236
|
+
swcrc: !1,
|
|
237
|
+
configFile: !1,
|
|
238
|
+
sourceMaps: shouldSourceMap(),
|
|
239
|
+
jsc: {
|
|
240
|
+
target: "es2019",
|
|
241
|
+
parser,
|
|
242
|
+
transform: {
|
|
243
|
+
useDefineForClassFields: !0,
|
|
244
|
+
react: {
|
|
245
|
+
development: !0,
|
|
246
|
+
runtime: "automatic",
|
|
247
|
+
refresh: !1
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
};
|
|
253
|
+
export {
|
|
254
|
+
transformSWC,
|
|
255
|
+
transformSWCStripJSX
|
|
256
|
+
};
|
|
257
|
+
//# sourceMappingURL=transformSWC.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/Users/n8/one/packages/compiler/src/transformSWC.ts"],
|
|
4
|
+
"mappings": "AAAA,SACEA,iBAKK;AAEP,SAASC,eAAe;AACxB,SAASC,aAAa;AACtB,SAASC,qBAAqB;AAC9B,SAASC,qBAAqBC,OAAOC,SAASC,yBAAyB;AAGvE,eAAsBC,aACpBC,IACAC,MACAC,SACAC,YAAuB;AAEvB,MAAIH,IAAGI,SAAS,OAAA,MAIhBJ,KAAKA,GACFK,MAAM,GAAA,EAAK,CAAA,EAEXC,QAAQC,QAAQC,IAAG,GAAI,EAAA,GAEtBR,OAAOF,oBAIX;QAAMW,SAASC,UAAUV,IAAIE,QAAQS,QAAQ;AAE7C,QAAKF,QAIL;UAAMG,kBACJlB,cAAckB;;MAGd,CAACZ,GAAGI,SAAS,cAAA,GAETS,UACJX,QAAQY,gBAAgB,SAAS,CAACZ,QAAQa,cAAc,CAACb,Q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AAO;EACTsE,UAAAA;gBACcrF,EAAAA;EACd6C,OAAO5C,IAAI;;AAIX,EAAIuD,cACFX,OAAO5C,OAAO;;;;;;2EAMyDD,EAAAA;;;EAGzEqF,UAAAA;;kBAEgBrF,EAAAA;;;EAGhB6C,OAAO5C,IAAI;;;;;;;IAUX4C,OAAO5C,OAAO,GAAG4C,OAAO5C,IAAI;;;;;;;;;AAS9B;AAEA,SAASS,UAAUV,IAAU;MAAEW,WAAAA,UAAAA,SAAAA,KAAAA,UAAAA,CAAAA,MAAAA,SAAAA,UAAAA,CAAAA,IAAW;AACxC,MAAIX,GAAGsF,SAAS,kBAAA;AACd,WAAOzF,QAAQ,MAAA;AAGjB,MAAM0F,YAAY/F,QAAQQ,EAAAA,GACtBS,SAAwB8E,YAA6B1F,QAAQ0F,SAAAA,IAAzB1F,QAAQ,KAAA;AAEhD,SAAI0F,cAAc,UACZ5E,aACFF,SAASZ,QAAQ,MAAA,IAGfG,GAAGI,SAAS,mBAAA,MACdK,SAASZ,QAAQ,MAAA,KAIdY;AACT;AAEO,IAAM+E,uBAAuB,eAAOxF,IAAYC,MAAAA;AACrD,MAAMQ,SAASC,UAAUV,EAAAA;AACzB,MAAKS;AACL,WAAO,MAAMlB,UAAUU,MAAM;MAC3ByC,UAAU1C;MACV2C,OAAO;MACPC,YAAY;MACZrB,YAAYY,gBAAAA;MACZX,KAAK;QACHC,QAAQ;QACRhB;QACAlB,WAAW;UACTmC,yBAAyB;UACzBC,OAAO;YACLT,aAAa;YACbC,SAAS;YACTN,SAAS;UACX;QACF;MACF;IACF,CAAA;AACF;",
|
|
5
|
+
"names": ["transform", "extname", "merge", "configuration", "asyncGeneratorRegex", "debug", "parsers", "runtimePublicPath", "transformSWC", "id", "code", "options", "swcOptions", "includes", "split", "replace", "process", "cwd", "parser", "getParser", "forceJSX", "enableNativeCSS", "refresh", "environment", "production", "noHMR", "reactConfig", "development", "runtime", "importSource", "pragma", "transformOptions", "sourceMaps", "jsc", "target", "useDefineForClassFields", "react", "shouldEs5Transform", "es5", "env", "VXRN_USE_BABEL_FOR_GENERATORS", "test", "opts", "SWC_ENV", "shouldSourceMap", "module", "importInterop", "type", "mode", "strict", "finalOptions", "filename", "swcrc", "configFile", "result", "JSON", "stringify", "e", "message", "fileStartIndex", "indexOf", "match", "slice", "line", "column", "shouldHMR", "refreshContentRE", "fixNonTypeSpecificImports", "syntax", "typeExportsMatch", "Array", "from", "typeExport", "_export", "_type", "name", "startsWith", "alreadyExported", "RegExp", "fakeExport", "console", "info", "wrapSourceInRefreshRuntime", "targets", "node", "include", "exclude", "VXRN_ENABLE_SOURCE_MAP", "wrapSourceInRefreshRuntimeWeb", "wrapSourceInRefreshRuntimeNative", "sourceMap", "parse", "map", "mappings", "prefixCode", "endsWith", "extension", "transformSWCStripJSX"]
|
|
6
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sources":[],"sourcesContent":[],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=types.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vxrn/compiler",
|
|
3
|
+
"version": "1.1.397",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
"./package.json": "./package.json",
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./types/index.d.ts",
|
|
9
|
+
"import": "./dist/esm/index.mjs",
|
|
10
|
+
"require": "./dist/cjs/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"main": "dist/cjs",
|
|
14
|
+
"module": "dist/esm",
|
|
15
|
+
"types": "./types/index.d.ts",
|
|
16
|
+
"files": [
|
|
17
|
+
"src",
|
|
18
|
+
"types",
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tamagui-build",
|
|
23
|
+
"check": "depcheck",
|
|
24
|
+
"clean": "tamagui-build clean",
|
|
25
|
+
"clean:build": "tamagui-build clean:build",
|
|
26
|
+
"lint": "../../node_modules/.bin/biome check src",
|
|
27
|
+
"lint:fix": "../../node_modules/.bin/biome check --write --unsafe src",
|
|
28
|
+
"watch": "tamagui-build --watch"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@babel/core": "^7.26.0",
|
|
32
|
+
"@babel/plugin-transform-destructuring": "^7.25.9",
|
|
33
|
+
"@babel/plugin-transform-private-methods": "^7.25.9",
|
|
34
|
+
"@babel/plugin-transform-react-jsx": "^7.25.9",
|
|
35
|
+
"@babel/plugin-transform-regenerator": "^7.25.9",
|
|
36
|
+
"@swc/core": "^1.10.4",
|
|
37
|
+
"@vxrn/utils": "1.1.397",
|
|
38
|
+
"@vxrn/vite-native-client": "1.1.397",
|
|
39
|
+
"babel-plugin-react-compiler": "^19.0.0-beta-201e55d-20241215",
|
|
40
|
+
"ts-deepmerge": "^7.0.2",
|
|
41
|
+
"vite": "^6.0.6"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@tamagui/build": "^1.121.8",
|
|
45
|
+
"depcheck": "^1.4.7",
|
|
46
|
+
"react-native": "^0.76.5"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"react-native": "*"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/configure.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Environment } from './types'
|
|
2
|
+
|
|
3
|
+
type Conf = {
|
|
4
|
+
enableReanimated?: boolean
|
|
5
|
+
enableCompiler?: boolean | Environment[]
|
|
6
|
+
enableNativeCSS?: boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const configuration: Conf = {
|
|
10
|
+
enableReanimated: false,
|
|
11
|
+
enableCompiler: false,
|
|
12
|
+
enableNativeCSS: false,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function configureVXRNCompilerPlugin(_: Conf) {
|
|
16
|
+
Object.assign(configuration, _)
|
|
17
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ParserConfig } from '@swc/core'
|
|
2
|
+
|
|
3
|
+
import { createDebugger } from '@vxrn/utils'
|
|
4
|
+
|
|
5
|
+
export const { debug } = createDebugger('vxrn:compiler-plugin')
|
|
6
|
+
|
|
7
|
+
export const runtimePublicPath = '/@react-refresh'
|
|
8
|
+
|
|
9
|
+
export const asyncGeneratorRegex = /(async \*|async function\*|for await)/
|
|
10
|
+
|
|
11
|
+
export const parsers: Record<string, ParserConfig> = {
|
|
12
|
+
'.tsx': { syntax: 'typescript', tsx: true, decorators: true },
|
|
13
|
+
'.ts': { syntax: 'typescript', tsx: false, decorators: true },
|
|
14
|
+
'.jsx': { syntax: 'ecmascript', jsx: true },
|
|
15
|
+
'.js': { syntax: 'ecmascript' },
|
|
16
|
+
'.mjs': { syntax: 'ecmascript' },
|
|
17
|
+
'.cjs': { syntax: 'ecmascript' },
|
|
18
|
+
'.mdx': { syntax: 'ecmascript', jsx: true },
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const validParsers = new Set([...Object.keys(parsers), '.css'])
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adapted from https://github.com/vitejs/vite-plugin-react-swc/blob/main/src/index.ts
|
|
3
|
+
* to work on both native and web, and with reanimated and other babel fallbacks
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { resolvePath } from '@vxrn/utils'
|
|
7
|
+
import { readFileSync } from 'node:fs'
|
|
8
|
+
import { readFile } from 'node:fs/promises'
|
|
9
|
+
import { extname, join } from 'node:path'
|
|
10
|
+
import type { PluginOption, UserConfig } from 'vite'
|
|
11
|
+
import { debug, runtimePublicPath, validParsers } from './constants'
|
|
12
|
+
import { getBabelOptions, transformBabel } from './transformBabel'
|
|
13
|
+
import { transformSWC } from './transformSWC'
|
|
14
|
+
import type { Environment, GetTransformProps, Options } from './types'
|
|
15
|
+
|
|
16
|
+
export * from './configure'
|
|
17
|
+
export * from './transformBabel'
|
|
18
|
+
export * from './transformSWC'
|
|
19
|
+
export type { GetTransform } from './types'
|
|
20
|
+
|
|
21
|
+
export async function createVXRNCompilerPlugin(
|
|
22
|
+
optionsIn?: Partial<Options>
|
|
23
|
+
): Promise<PluginOption[]> {
|
|
24
|
+
const reactVersion = await (async () => {
|
|
25
|
+
const path = resolvePath('react/package.json')
|
|
26
|
+
const json = JSON.parse(await readFile(path, 'utf-8'))
|
|
27
|
+
return json.version as string
|
|
28
|
+
})()
|
|
29
|
+
|
|
30
|
+
const envNames = {
|
|
31
|
+
ios: true,
|
|
32
|
+
android: true,
|
|
33
|
+
client: true,
|
|
34
|
+
ssr: true,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function getEnvName(name: string) {
|
|
38
|
+
if (!envNames[name]) throw new Error(`Invalid env: ${name}`)
|
|
39
|
+
return name as Environment
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return [
|
|
43
|
+
{
|
|
44
|
+
name: 'one:compiler-resolve-refresh-runtime',
|
|
45
|
+
apply: 'serve',
|
|
46
|
+
enforce: 'pre', // Run before Vite default resolve to avoid syscalls
|
|
47
|
+
resolveId: (id) => (id === runtimePublicPath ? id : undefined),
|
|
48
|
+
load: (id) =>
|
|
49
|
+
id === runtimePublicPath
|
|
50
|
+
? readFileSync(join(import.meta.dirname, 'refresh-runtime.js'), 'utf-8')
|
|
51
|
+
: undefined,
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
{
|
|
55
|
+
name: 'one:compiler',
|
|
56
|
+
enforce: 'pre',
|
|
57
|
+
|
|
58
|
+
config: () => {
|
|
59
|
+
const config = {
|
|
60
|
+
esbuild: false,
|
|
61
|
+
optimizeDeps: {
|
|
62
|
+
noDiscovery: true,
|
|
63
|
+
},
|
|
64
|
+
} satisfies UserConfig
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
environments: {
|
|
68
|
+
ios: config,
|
|
69
|
+
android: config,
|
|
70
|
+
},
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
transform: {
|
|
75
|
+
order: 'pre',
|
|
76
|
+
async handler(codeIn, _id) {
|
|
77
|
+
let code = codeIn
|
|
78
|
+
|
|
79
|
+
const shouldDebug =
|
|
80
|
+
process.env.NODE_ENV === 'development' && codeIn.startsWith('// debug')
|
|
81
|
+
|
|
82
|
+
if (shouldDebug) {
|
|
83
|
+
console.info(`[one] ${_id} input:`)
|
|
84
|
+
console.info(codeIn)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const extension = extname(_id)
|
|
88
|
+
if (!validParsers.has(extension)) {
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (extension === '.css') {
|
|
93
|
+
//
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let id = _id.split('?')[0]
|
|
97
|
+
|
|
98
|
+
// pre process = hmr just are removing jsx but leaving imports as esm
|
|
99
|
+
const isPreProcess = id.startsWith(`vxrn-swc-preprocess:`)
|
|
100
|
+
if (isPreProcess) {
|
|
101
|
+
id = id.replace(`vxrn-swc-preprocess:`, '')
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (id.includes(`virtual:`)) {
|
|
105
|
+
return
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const environment = getEnvName(this.environment.name)
|
|
109
|
+
const production = process.env.NODE_ENV === 'production'
|
|
110
|
+
|
|
111
|
+
const transformProps: GetTransformProps = {
|
|
112
|
+
id,
|
|
113
|
+
code,
|
|
114
|
+
development: !production,
|
|
115
|
+
environment,
|
|
116
|
+
reactForRNVersion: reactVersion.split('.')[0] as '18' | '19',
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const userTransform = optionsIn?.transform?.(transformProps)
|
|
120
|
+
|
|
121
|
+
if (userTransform === false) {
|
|
122
|
+
return
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (!isPreProcess && userTransform !== 'swc') {
|
|
126
|
+
const babelOptions = getBabelOptions({
|
|
127
|
+
...transformProps,
|
|
128
|
+
userSetting: userTransform,
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
if (babelOptions) {
|
|
132
|
+
const babelOut = await transformBabel(id, code, babelOptions)
|
|
133
|
+
if (babelOut) {
|
|
134
|
+
debug?.(`[${id}] transformed with babel options: ${JSON.stringify(babelOptions)}`)
|
|
135
|
+
code = babelOut
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// we always go to swc for now to ensure class transforms + react refesh
|
|
140
|
+
// we could make the babel plugin support those if we want to avoid
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const swcOptions = {
|
|
144
|
+
environment: environment,
|
|
145
|
+
mode: 'serve',
|
|
146
|
+
production,
|
|
147
|
+
...optionsIn,
|
|
148
|
+
} satisfies Options
|
|
149
|
+
|
|
150
|
+
const out = await transformSWC(id, code, {
|
|
151
|
+
...swcOptions,
|
|
152
|
+
es5: true,
|
|
153
|
+
noHMR: isPreProcess,
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
if (shouldDebug) {
|
|
157
|
+
console.info(`swcOptions`, swcOptions)
|
|
158
|
+
console.info(`final output:`, out?.code)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return out
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
]
|
|
166
|
+
}
|