@vxrn/compiler 1.25.16 → 1.26.0
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/dist/cjs/cache.cjs +4 -2
- package/dist/cjs/configure.cjs +9 -1
- package/dist/cjs/index.cjs +87 -5
- package/dist/cjs/reactNativeCodegen.cjs +140 -0
- package/dist/cjs/reactNativeViewConfig.cjs +489 -0
- package/dist/cjs/transformBabel.cjs +15 -20
- package/dist/cjs/transformBabel.test.cjs +260 -0
- package/dist/cjs/transformHermesAsync.cjs +61 -0
- package/dist/cjs/transformHermesLoops.cjs +319 -0
- package/dist/cjs/transformHermesLoops.test.cjs +257 -0
- package/dist/cjs/transformSWC.cjs +110 -138
- package/dist/cjs/transformSWC.test.cjs +117 -0
- package/dist/cjs/transformWorklets.cjs +123 -0
- package/dist/cjs/transformWorklets.test.cjs +604 -0
- package/dist/cjs/worklets/autoworklet.cjs +265 -0
- package/dist/cjs/worklets/globals.cjs +73 -0
- package/dist/cjs/worklets/hash.cjs +38 -0
- package/dist/cjs/worklets/scope.cjs +370 -0
- package/dist/cjs/worklets/serialize.cjs +101 -0
- package/dist/cjs/worklets/transform.cjs +266 -0
- package/dist/cjs/worklets/types.cjs +18 -0
- package/dist/esm/cache.mjs +5 -3
- package/dist/esm/cache.mjs.map +1 -1
- package/dist/esm/configure.mjs +8 -1
- package/dist/esm/configure.mjs.map +1 -1
- package/dist/esm/index.js +75 -6
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/index.mjs +75 -6
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/reactNativeCodegen.mjs +104 -0
- package/dist/esm/reactNativeCodegen.mjs.map +1 -0
- package/dist/esm/reactNativeViewConfig.mjs +460 -0
- package/dist/esm/reactNativeViewConfig.mjs.map +1 -0
- package/dist/esm/transformBabel.mjs +14 -19
- package/dist/esm/transformBabel.mjs.map +1 -1
- package/dist/esm/transformBabel.test.mjs +261 -1
- package/dist/esm/transformBabel.test.mjs.map +1 -1
- package/dist/esm/transformHermesAsync.mjs +25 -0
- package/dist/esm/transformHermesAsync.mjs.map +1 -0
- package/dist/esm/transformHermesLoops.mjs +283 -0
- package/dist/esm/transformHermesLoops.mjs.map +1 -0
- package/dist/esm/transformHermesLoops.test.mjs +234 -0
- package/dist/esm/transformHermesLoops.test.mjs.map +1 -0
- package/dist/esm/transformSWC.mjs +96 -139
- package/dist/esm/transformSWC.mjs.map +1 -1
- package/dist/esm/transformSWC.test.mjs +118 -0
- package/dist/esm/transformSWC.test.mjs.map +1 -0
- package/dist/esm/transformWorklets.mjs +82 -0
- package/dist/esm/transformWorklets.mjs.map +1 -0
- package/dist/esm/transformWorklets.test.mjs +581 -0
- package/dist/esm/transformWorklets.test.mjs.map +1 -0
- package/dist/esm/worklets/autoworklet.mjs +234 -0
- package/dist/esm/worklets/autoworklet.mjs.map +1 -0
- package/dist/esm/worklets/globals.mjs +46 -0
- package/dist/esm/worklets/globals.mjs.map +1 -0
- package/dist/esm/worklets/hash.mjs +13 -0
- package/dist/esm/worklets/hash.mjs.map +1 -0
- package/dist/esm/worklets/scope.mjs +345 -0
- package/dist/esm/worklets/scope.mjs.map +1 -0
- package/dist/esm/worklets/serialize.mjs +75 -0
- package/dist/esm/worklets/serialize.mjs.map +1 -0
- package/dist/esm/worklets/transform.mjs +229 -0
- package/dist/esm/worklets/transform.mjs.map +1 -0
- package/dist/esm/worklets/types.mjs +2 -0
- package/dist/esm/worklets/types.mjs.map +1 -0
- package/package.json +11 -6
- package/src/cache.ts +10 -3
- package/src/configure.ts +12 -0
- package/src/index.ts +96 -13
- package/src/reactNativeCodegen.ts +181 -0
- package/src/reactNativeViewConfig.ts +593 -0
- package/src/transformBabel.test.ts +268 -1
- package/src/transformBabel.ts +21 -31
- package/src/transformHermesAsync.ts +30 -0
- package/src/transformHermesLoops.test.ts +341 -0
- package/src/transformHermesLoops.ts +435 -0
- package/src/transformSWC.test.ts +132 -0
- package/src/transformSWC.ts +121 -161
- package/src/transformWorklets.test.ts +708 -0
- package/src/transformWorklets.ts +160 -0
- package/src/types.ts +4 -4
- package/src/worklets/autoworklet.ts +391 -0
- package/src/worklets/globals.ts +129 -0
- package/src/worklets/hash.ts +14 -0
- package/src/worklets/scope.ts +401 -0
- package/src/worklets/serialize.ts +114 -0
- package/src/worklets/transform.ts +319 -0
- package/src/worklets/types.ts +26 -0
- package/types/cache.d.ts +1 -0
- package/types/cache.d.ts.map +1 -1
- package/types/configure.d.ts +2 -0
- package/types/configure.d.ts.map +1 -1
- package/types/index.d.ts +6 -0
- package/types/index.d.ts.map +1 -1
- package/types/reactNativeCodegen.d.ts +5 -0
- package/types/reactNativeCodegen.d.ts.map +1 -0
- package/types/reactNativeViewConfig.d.ts +35 -0
- package/types/reactNativeViewConfig.d.ts.map +1 -0
- package/types/transformBabel.d.ts +3 -3
- package/types/transformBabel.d.ts.map +1 -1
- package/types/transformHermesAsync.d.ts +5 -0
- package/types/transformHermesAsync.d.ts.map +1 -0
- package/types/transformHermesLoops.d.ts +5 -0
- package/types/transformHermesLoops.d.ts.map +1 -0
- package/types/transformHermesLoops.test.d.ts +2 -0
- package/types/transformHermesLoops.test.d.ts.map +1 -0
- package/types/transformSWC.d.ts +13 -3
- package/types/transformSWC.d.ts.map +1 -1
- package/types/transformSWC.test.d.ts +2 -0
- package/types/transformSWC.test.d.ts.map +1 -0
- package/types/transformWorklets.d.ts +23 -0
- package/types/transformWorklets.d.ts.map +1 -0
- package/types/transformWorklets.test.d.ts +2 -0
- package/types/transformWorklets.test.d.ts.map +1 -0
- package/types/types.d.ts +4 -4
- package/types/types.d.ts.map +1 -1
- package/types/worklets/autoworklet.d.ts +14 -0
- package/types/worklets/autoworklet.d.ts.map +1 -0
- package/types/worklets/globals.d.ts +4 -0
- package/types/worklets/globals.d.ts.map +1 -0
- package/types/worklets/hash.d.ts +5 -0
- package/types/worklets/hash.d.ts.map +1 -0
- package/types/worklets/scope.d.ts +7 -0
- package/types/worklets/scope.d.ts.map +1 -0
- package/types/worklets/serialize.d.ts +12 -0
- package/types/worklets/serialize.d.ts.map +1 -0
- package/types/worklets/transform.d.ts +10 -0
- package/types/worklets/transform.d.ts.map +1 -0
- package/types/worklets/types.d.ts +21 -0
- package/types/worklets/types.d.ts.map +1 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import MagicString from 'magic-string'
|
|
2
|
+
import {
|
|
3
|
+
generateViewConfig,
|
|
4
|
+
parseSpec,
|
|
5
|
+
renderViewConfigModule,
|
|
6
|
+
} from './reactNativeViewConfig'
|
|
7
|
+
|
|
8
|
+
// vite's normalizePath, inlined so this stays usable from the metro transformer
|
|
9
|
+
// worker, which must not pull vite into a worker process.
|
|
10
|
+
function normalizePath(id: string) {
|
|
11
|
+
return id.replace(/\\/g, '/')
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const CODEGEN_FILE_RE = /(?:NativeComponent\.[cm]?[jt]sx?$|[/\\]specs?[/\\])/
|
|
15
|
+
const CODEGEN_CALL_RE = /codegenNativeComponent\s*[<(]/
|
|
16
|
+
|
|
17
|
+
// oxc reports positions as start/end, hermes-parser (the flow specs) as range
|
|
18
|
+
const startOf = (node: any): number => node.start ?? node.range[0]
|
|
19
|
+
const endOf = (node: any): number => node.end ?? node.range[1]
|
|
20
|
+
|
|
21
|
+
function unwrapExpression(node: any): any {
|
|
22
|
+
while (node) {
|
|
23
|
+
if (
|
|
24
|
+
node.type === 'TSAsExpression' ||
|
|
25
|
+
node.type === 'TSSatisfiesExpression' ||
|
|
26
|
+
node.type === 'TSTypeAssertion' ||
|
|
27
|
+
node.type === 'TypeCastExpression' ||
|
|
28
|
+
node.type === 'AsExpression' ||
|
|
29
|
+
node.type === 'ParenthesizedExpression'
|
|
30
|
+
) {
|
|
31
|
+
node = node.expression
|
|
32
|
+
} else if (
|
|
33
|
+
node.type === 'SequenceExpression' &&
|
|
34
|
+
Array.isArray(node.expressions) &&
|
|
35
|
+
node.expressions.length > 0
|
|
36
|
+
) {
|
|
37
|
+
node = node.expressions[node.expressions.length - 1]
|
|
38
|
+
} else {
|
|
39
|
+
break
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return node
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function isCodegenDeclaration(declaration: any): boolean {
|
|
46
|
+
if (!declaration) return false
|
|
47
|
+
|
|
48
|
+
if (
|
|
49
|
+
declaration.left &&
|
|
50
|
+
declaration.left.left &&
|
|
51
|
+
declaration.left.left.name === 'codegenNativeComponent'
|
|
52
|
+
) {
|
|
53
|
+
return true
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const unwrapped = unwrapExpression(declaration)
|
|
57
|
+
return (
|
|
58
|
+
unwrapped?.type === 'CallExpression' &&
|
|
59
|
+
unwrapped.callee?.type === 'Identifier' &&
|
|
60
|
+
unwrapped.callee.name === 'codegenNativeComponent'
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function isCodegenNativeCommandsDeclaration(declaration: any): boolean {
|
|
65
|
+
if (!declaration) return false
|
|
66
|
+
const unwrapped = unwrapExpression(declaration)
|
|
67
|
+
return (
|
|
68
|
+
unwrapped?.type === 'CallExpression' &&
|
|
69
|
+
unwrapped.callee?.type === 'Identifier' &&
|
|
70
|
+
unwrapped.callee.name === 'codegenNativeCommands'
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function transformReactNativeCodegen(
|
|
75
|
+
code: string,
|
|
76
|
+
id: string,
|
|
77
|
+
projectRoot?: string
|
|
78
|
+
): { code: string; map: any } | null | undefined {
|
|
79
|
+
const cleanId = normalizePath(id.split('?')[0])
|
|
80
|
+
|
|
81
|
+
if (!CODEGEN_FILE_RE.test(cleanId)) {
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// the transform runs more than once over the same module, so it must not
|
|
86
|
+
// reject the output it just produced. matching the bare name matches the
|
|
87
|
+
// surviving `import codegenNativeComponent from ...` too; only a remaining
|
|
88
|
+
// CALL means the file still needs generating. a comment marker cannot do
|
|
89
|
+
// this job because oxc drops leading comments on the next pass.
|
|
90
|
+
if (!CODEGEN_CALL_RE.test(code)) {
|
|
91
|
+
return
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const program = parseSpec(code, cleanId, projectRoot)
|
|
95
|
+
const body = program?.body
|
|
96
|
+
if (!body) {
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let defaultExport: any = null
|
|
101
|
+
let commandsExport: any = null
|
|
102
|
+
|
|
103
|
+
for (const node of body) {
|
|
104
|
+
if (node.type === 'ExportDefaultDeclaration') {
|
|
105
|
+
if (isCodegenDeclaration(node.declaration)) {
|
|
106
|
+
defaultExport = node
|
|
107
|
+
}
|
|
108
|
+
} else if (node.type === 'ExportNamedDeclaration') {
|
|
109
|
+
if (node.declaration && node.declaration.declarations) {
|
|
110
|
+
const firstDeclaration = node.declaration.declarations[0]
|
|
111
|
+
if (firstDeclaration?.type === 'VariableDeclarator') {
|
|
112
|
+
const isValidCommandsExport = isCodegenNativeCommandsDeclaration(
|
|
113
|
+
firstDeclaration.init
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
if (isValidCommandsExport) {
|
|
117
|
+
if (
|
|
118
|
+
firstDeclaration.id?.type === 'Identifier' &&
|
|
119
|
+
firstDeclaration.id.name !== 'Commands'
|
|
120
|
+
) {
|
|
121
|
+
throw new Error("Native commands must be exported with the name 'Commands'")
|
|
122
|
+
}
|
|
123
|
+
commandsExport = node
|
|
124
|
+
} else {
|
|
125
|
+
if (
|
|
126
|
+
firstDeclaration.id?.type === 'Identifier' &&
|
|
127
|
+
firstDeclaration.id.name === 'Commands'
|
|
128
|
+
) {
|
|
129
|
+
throw new Error(
|
|
130
|
+
"'Commands' is a reserved export and may only be used to export the result of codegenNativeCommands."
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
} else if (node.specifiers && node.specifiers.length > 0) {
|
|
136
|
+
for (const specifier of node.specifiers) {
|
|
137
|
+
if (
|
|
138
|
+
specifier.type === 'ExportSpecifier' &&
|
|
139
|
+
((specifier.local?.type === 'Identifier' &&
|
|
140
|
+
specifier.local.name === 'Commands') ||
|
|
141
|
+
(specifier.exported?.type === 'Identifier' &&
|
|
142
|
+
specifier.exported.name === 'Commands'))
|
|
143
|
+
) {
|
|
144
|
+
throw new Error(
|
|
145
|
+
"'Commands' is a reserved export and may only be used to export the result of codegenNativeCommands."
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (!defaultExport) {
|
|
154
|
+
return
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const generated = generateViewConfig(program)
|
|
158
|
+
if (!generated) {
|
|
159
|
+
return
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const s = new MagicString(code)
|
|
163
|
+
|
|
164
|
+
if (commandsExport) {
|
|
165
|
+
let removeEnd = endOf(commandsExport)
|
|
166
|
+
if (code[removeEnd] === '\r') removeEnd++
|
|
167
|
+
if (code[removeEnd] === '\n') removeEnd++
|
|
168
|
+
s.remove(startOf(commandsExport), removeEnd)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
s.overwrite(
|
|
172
|
+
startOf(defaultExport),
|
|
173
|
+
endOf(defaultExport),
|
|
174
|
+
renderViewConfigModule(generated)
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
code: s.toString(),
|
|
179
|
+
map: s.generateMap({ hires: true, source: cleanId }),
|
|
180
|
+
}
|
|
181
|
+
}
|