@builder.io/mitosis 0.3.0 → 0.3.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.
- package/dist/src/generators/react/blocks.js +10 -6
- package/dist/src/generators/react/generator.js +1 -1
- package/dist/src/generators/react-native/index.js +7 -2
- package/dist/src/parsers/jsx/element-parser.js +65 -54
- package/dist/src/parsers/svelte/helpers/mitosis-node.js +1 -0
- package/dist/src/parsers/svelte/html/index.js +1 -0
- package/package.json +1 -1
|
@@ -130,7 +130,7 @@ var NATIVE_EVENT_MAPPER = {
|
|
|
130
130
|
onClick: 'onPress',
|
|
131
131
|
};
|
|
132
132
|
var blockToReact = function (json, options, component, parentSlots) {
|
|
133
|
-
var _a, _b, _c;
|
|
133
|
+
var _a, _b, _c, _d;
|
|
134
134
|
if (parentSlots === void 0) { parentSlots = []; }
|
|
135
135
|
if (NODE_MAPPERS[json.name]) {
|
|
136
136
|
return NODE_MAPPERS[json.name](json, options, component, parentSlots);
|
|
@@ -161,7 +161,7 @@ var blockToReact = function (json, options, component, parentSlots) {
|
|
|
161
161
|
else if (BINDING_MAPPERS[key]) {
|
|
162
162
|
var mapper = BINDING_MAPPERS[key];
|
|
163
163
|
if (typeof mapper === 'function') {
|
|
164
|
-
var
|
|
164
|
+
var _e = mapper(key, value, options), newKey = _e[0], newValue = _e[1];
|
|
165
165
|
str += " ".concat(newKey, "={").concat(newValue, "} ");
|
|
166
166
|
}
|
|
167
167
|
else {
|
|
@@ -175,16 +175,20 @@ var blockToReact = function (json, options, component, parentSlots) {
|
|
|
175
175
|
}
|
|
176
176
|
}
|
|
177
177
|
for (var key in json.bindings) {
|
|
178
|
-
|
|
178
|
+
// ignore duplicate slot attribute
|
|
179
|
+
if ((_b = json.slots) === null || _b === void 0 ? void 0 : _b[key]) {
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
var value = String((_c = json.bindings[key]) === null || _c === void 0 ? void 0 : _c.code);
|
|
179
183
|
if (key === 'css' && value.trim() === '{}') {
|
|
180
184
|
continue;
|
|
181
185
|
}
|
|
182
186
|
var useBindingValue = (0, helpers_1.processBinding)(value, options);
|
|
183
|
-
if (((
|
|
187
|
+
if (((_d = json.bindings[key]) === null || _d === void 0 ? void 0 : _d.type) === 'spread') {
|
|
184
188
|
str += " {...(".concat(value, ")} ");
|
|
185
189
|
}
|
|
186
190
|
else if (key.startsWith('on')) {
|
|
187
|
-
var
|
|
191
|
+
var _f = json.bindings[key].arguments, cusArgs = _f === void 0 ? ['event'] : _f;
|
|
188
192
|
var eventName = options.type === 'native' ? NATIVE_EVENT_MAPPER[key] || key : key;
|
|
189
193
|
str += " ".concat(eventName, "={(").concat(cusArgs.join(','), ") => ").concat((0, state_1.updateStateSettersInCode)(useBindingValue, options), " } ");
|
|
190
194
|
}
|
|
@@ -198,7 +202,7 @@ var blockToReact = function (json, options, component, parentSlots) {
|
|
|
198
202
|
else if (BINDING_MAPPERS[key]) {
|
|
199
203
|
var mapper = BINDING_MAPPERS[key];
|
|
200
204
|
if (typeof mapper === 'function') {
|
|
201
|
-
var
|
|
205
|
+
var _g = mapper(key, useBindingValue, options), newKey = _g[0], newValue = _g[1];
|
|
202
206
|
str += " ".concat(newKey, "={").concat(newValue, "} ");
|
|
203
207
|
}
|
|
204
208
|
else {
|
|
@@ -241,7 +241,7 @@ var getDefaultImport = function (json, options) {
|
|
|
241
241
|
return "\n /** @jsx h */\n import { h, Fragment } from 'preact';\n ";
|
|
242
242
|
}
|
|
243
243
|
if (type === 'native') {
|
|
244
|
-
return "\n import * as React from 'react';\n import { FlatList, ScrollView, View, StyleSheet, Image, Text, Pressable } from 'react-native';\n ";
|
|
244
|
+
return "\n import * as React from 'react';\n import { FlatList, ScrollView, View, StyleSheet, Image, Text, Pressable, TextInput } from 'react-native';\n ";
|
|
245
245
|
}
|
|
246
246
|
if (type === 'taro') {
|
|
247
247
|
return "\n import * as React from 'react';\n ";
|
|
@@ -116,10 +116,15 @@ var PROCESS_REACT_NATIVE_PLUGIN = function () { return ({
|
|
|
116
116
|
node.name = '';
|
|
117
117
|
}
|
|
118
118
|
else if (node.name.toLowerCase() === node.name && html_tags_1.VALID_HTML_TAGS.includes(node.name)) {
|
|
119
|
-
node.name
|
|
120
|
-
|
|
119
|
+
if (node.name === 'input') {
|
|
120
|
+
node.name = 'TextInput';
|
|
121
|
+
}
|
|
122
|
+
else if (node.bindings.onClick) {
|
|
121
123
|
node.name = 'Pressable';
|
|
122
124
|
}
|
|
125
|
+
else {
|
|
126
|
+
node.name = 'View';
|
|
127
|
+
}
|
|
123
128
|
}
|
|
124
129
|
else if (((_a = node.properties._text) === null || _a === void 0 ? void 0 : _a.trim().length) ||
|
|
125
130
|
((_d = (_c = (_b = node.bindings._text) === null || _b === void 0 ? void 0 : _b.code) === null || _c === void 0 ? void 0 : _c.trim()) === null || _d === void 0 ? void 0 : _d.length)) {
|
|
@@ -189,66 +189,77 @@ var jsxElementToJson = function (node) {
|
|
|
189
189
|
}
|
|
190
190
|
}
|
|
191
191
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
memo[key] = value.expression.value;
|
|
204
|
-
return memo;
|
|
205
|
-
}
|
|
192
|
+
// const properties: MitosisNode['properties'] = {}
|
|
193
|
+
// const bindings: MitosisNode['bindings'] = {}
|
|
194
|
+
// const slots: MitosisNode['slots'] = {}
|
|
195
|
+
var _a = node.openingElement.attributes.reduce(function (memo, item) {
|
|
196
|
+
if (types.isJSXAttribute(item)) {
|
|
197
|
+
var key = (0, helpers_1.transformAttributeName)(item.name.name);
|
|
198
|
+
var value = item.value;
|
|
199
|
+
// <Foo myProp />
|
|
200
|
+
if (value === null) {
|
|
201
|
+
memo.bindings[key] = (0, bindings_1.createSingleBinding)({ code: 'true' });
|
|
202
|
+
return memo;
|
|
206
203
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
memo[key] = (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(expression).code });
|
|
234
|
-
}
|
|
204
|
+
// <Foo myProp="hello" />
|
|
205
|
+
if (types.isStringLiteral(value)) {
|
|
206
|
+
memo.properties[key] = value.value;
|
|
207
|
+
return memo;
|
|
208
|
+
}
|
|
209
|
+
if (!types.isJSXExpressionContainer(value))
|
|
210
|
+
return memo;
|
|
211
|
+
var expression = value.expression;
|
|
212
|
+
if (types.isStringLiteral(expression)) {
|
|
213
|
+
// <Foo myProp={"hello"} />
|
|
214
|
+
memo.properties[key] = expression.value;
|
|
215
|
+
}
|
|
216
|
+
else if (types.isArrowFunctionExpression(expression)) {
|
|
217
|
+
// <Foo myProp={() => {}} />
|
|
218
|
+
var args = key.startsWith('on')
|
|
219
|
+
? expression.params.map(function (node) { return node === null || node === void 0 ? void 0 : node.name; })
|
|
220
|
+
: [];
|
|
221
|
+
memo.bindings[key] = (0, bindings_1.createSingleBinding)({
|
|
222
|
+
code: (0, generator_1.default)(expression.body).code,
|
|
223
|
+
arguments: args.length ? args : undefined,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
else if (types.isJSXElement(expression)) {
|
|
227
|
+
// <Foo myProp={<MoreMitosisNode><div /></MoreMitosisNode>} />
|
|
228
|
+
var slotNode = (0, exports.jsxElementToJson)(expression);
|
|
229
|
+
if (!slotNode)
|
|
235
230
|
return memo;
|
|
236
|
-
|
|
231
|
+
memo.slots[key] = [slotNode];
|
|
232
|
+
// Temporarily keep the slot as a binding until we migrate generators to use the slots.
|
|
233
|
+
memo.bindings[key] = (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(expression).code });
|
|
237
234
|
}
|
|
238
|
-
else
|
|
239
|
-
|
|
240
|
-
// too so can do this accurately when order matters. Also tempting to not support spread,
|
|
241
|
-
// as some frameworks do not support it (e.g. Angular) tho Angular may be the only one
|
|
242
|
-
var key = (0, generator_1.default)(item.argument).code;
|
|
243
|
-
memo[key] = {
|
|
244
|
-
code: types.stringLiteral((0, generator_1.default)(item.argument).code).value,
|
|
245
|
-
type: 'spread',
|
|
246
|
-
spreadType: 'normal',
|
|
247
|
-
};
|
|
235
|
+
else {
|
|
236
|
+
memo.bindings[key] = (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(expression).code });
|
|
248
237
|
}
|
|
249
238
|
return memo;
|
|
250
|
-
}
|
|
239
|
+
}
|
|
240
|
+
else if (types.isJSXSpreadAttribute(item)) {
|
|
241
|
+
// TODO: potentially like Vue store bindings and properties as array of key value pairs
|
|
242
|
+
// too so can do this accurately when order matters. Also tempting to not support spread,
|
|
243
|
+
// as some frameworks do not support it (e.g. Angular) tho Angular may be the only one
|
|
244
|
+
var key = (0, generator_1.default)(item.argument).code;
|
|
245
|
+
memo.bindings[key] = {
|
|
246
|
+
code: types.stringLiteral((0, generator_1.default)(item.argument).code).value,
|
|
247
|
+
type: 'spread',
|
|
248
|
+
spreadType: 'normal',
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
return memo;
|
|
252
|
+
}, {
|
|
253
|
+
bindings: {},
|
|
254
|
+
properties: {},
|
|
255
|
+
slots: {},
|
|
256
|
+
}), bindings = _a.bindings, properties = _a.properties, slots = _a.slots;
|
|
257
|
+
return (0, create_mitosis_node_1.createMitosisNode)({
|
|
258
|
+
name: nodeName,
|
|
259
|
+
properties: properties,
|
|
260
|
+
bindings: bindings,
|
|
251
261
|
children: node.children.map(exports.jsxElementToJson).filter(nullable_1.checkIsDefined),
|
|
262
|
+
slots: Object.keys(slots).length > 0 ? slots : undefined,
|
|
252
263
|
});
|
|
253
264
|
};
|
|
254
265
|
exports.jsxElementToJson = jsxElementToJson;
|