@builder.io/mitosis 0.0.89 → 0.0.91
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/mitosis.js +1 -0
- package/dist/src/generators/react/blocks.d.ts +4 -0
- package/dist/src/generators/react/blocks.js +232 -0
- package/dist/src/generators/react/generator.d.ts +2 -5
- package/dist/src/generators/react/generator.js +100 -289
- package/dist/src/generators/react/helpers.d.ts +6 -0
- package/dist/src/generators/react/helpers.js +12 -1
- package/dist/src/generators/react/index.d.ts +1 -0
- package/dist/src/generators/react/index.js +3 -0
- package/dist/src/generators/react/types.d.ts +3 -3
- package/dist/src/generators/react-native.d.ts +3 -3
- package/dist/src/generators/react-native.js +3 -11
- package/dist/src/generators/rsc.d.ts +4 -4
- package/dist/src/generators/rsc.js +6 -21
- package/dist/src/generators/stencil/collect-class-string.js +1 -1
- package/dist/src/generators/taro.d.ts +9 -0
- package/dist/src/generators/taro.js +172 -0
- package/dist/src/helpers/merge-options.d.ts +1 -1
- package/dist/src/helpers/merge-options.js +2 -2
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/targets.d.ts +5 -4
- package/dist/src/targets.js +2 -0
- package/dist/src/types/config.d.ts +4 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { MitosisComponent } from '../../types/mitosis-component';
|
|
2
|
+
import { MitosisNode } from '../../types/mitosis-node';
|
|
3
|
+
import { ToReactOptions } from './types';
|
|
4
|
+
export declare const blockToReact: (json: MitosisNode, options: ToReactOptions, component: MitosisComponent, parentSlots?: any[]) => string;
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.blockToReact = void 0;
|
|
15
|
+
var lodash_1 = require("lodash");
|
|
16
|
+
var filter_empty_text_nodes_1 = require("../../helpers/filter-empty-text-nodes");
|
|
17
|
+
var is_valid_attribute_name_1 = require("../../helpers/is-valid-attribute-name");
|
|
18
|
+
var for_1 = require("../../helpers/nodes/for");
|
|
19
|
+
var jsx_1 = require("../../parsers/jsx");
|
|
20
|
+
var helpers_1 = require("./helpers");
|
|
21
|
+
var state_1 = require("./state");
|
|
22
|
+
var NODE_MAPPERS = {
|
|
23
|
+
Slot: function (json, options, component, parentSlots) {
|
|
24
|
+
var _a;
|
|
25
|
+
var _b, _c;
|
|
26
|
+
var slotName = ((_b = json.bindings.name) === null || _b === void 0 ? void 0 : _b.code) || json.properties.name;
|
|
27
|
+
var hasChildren = json.children.length;
|
|
28
|
+
var renderChildren = function () {
|
|
29
|
+
var _a;
|
|
30
|
+
var childrenStr = (_a = json.children) === null || _a === void 0 ? void 0 : _a.map(function (item) { return (0, exports.blockToReact)(item, options, component); }).join('\n').trim();
|
|
31
|
+
/**
|
|
32
|
+
* Ad-hoc way of figuring out if the children defaultProp is:
|
|
33
|
+
* - a JSX element, e.g. `<div>foo</div>`
|
|
34
|
+
* - a JS expression, e.g. `true`, `false`
|
|
35
|
+
* - a string, e.g. `'Default text'`
|
|
36
|
+
*
|
|
37
|
+
* and correctly wrapping it in quotes when appropriate.
|
|
38
|
+
*/
|
|
39
|
+
if (childrenStr.startsWith("<") && childrenStr.endsWith(">")) {
|
|
40
|
+
return childrenStr;
|
|
41
|
+
}
|
|
42
|
+
else if (['false', 'true', 'null', 'undefined'].includes(childrenStr)) {
|
|
43
|
+
return childrenStr;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
return "\"".concat(childrenStr, "\"");
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
if (!slotName) {
|
|
50
|
+
// TODO: update MitosisNode for simple code
|
|
51
|
+
var key = Object.keys(json.bindings).find(Boolean);
|
|
52
|
+
if (key && parentSlots) {
|
|
53
|
+
var propKey = (0, lodash_1.camelCase)('Slot' + key[0].toUpperCase() + key.substring(1));
|
|
54
|
+
parentSlots.push({ key: propKey, value: (_c = json.bindings[key]) === null || _c === void 0 ? void 0 : _c.code });
|
|
55
|
+
return '';
|
|
56
|
+
}
|
|
57
|
+
if (hasChildren) {
|
|
58
|
+
component.defaultProps = __assign(__assign({}, (component.defaultProps || {})), { children: {
|
|
59
|
+
code: renderChildren(),
|
|
60
|
+
type: 'property',
|
|
61
|
+
} });
|
|
62
|
+
}
|
|
63
|
+
return "{".concat((0, helpers_1.processBinding)('props.children', options), "}");
|
|
64
|
+
}
|
|
65
|
+
var slotProp = (0, helpers_1.processBinding)(slotName, options).replace('name=', '');
|
|
66
|
+
if (!slotProp.startsWith('props.slot')) {
|
|
67
|
+
slotProp = "props.slot".concat((0, lodash_1.upperFirst)((0, lodash_1.camelCase)(slotProp)));
|
|
68
|
+
}
|
|
69
|
+
if (hasChildren) {
|
|
70
|
+
component.defaultProps = __assign(__assign({}, (component.defaultProps || {})), (_a = {}, _a[slotProp.replace('props.', '')] = {
|
|
71
|
+
code: renderChildren(),
|
|
72
|
+
type: 'property',
|
|
73
|
+
}, _a));
|
|
74
|
+
}
|
|
75
|
+
return "{".concat(slotProp, "}");
|
|
76
|
+
},
|
|
77
|
+
Fragment: function (json, options, component) {
|
|
78
|
+
var wrap = (0, helpers_1.wrapInFragment)(json);
|
|
79
|
+
return "".concat(wrap ? (0, helpers_1.getFragment)('open', options) : '').concat(json.children
|
|
80
|
+
.map(function (item) { return (0, exports.blockToReact)(item, options, component); })
|
|
81
|
+
.join('\n')).concat(wrap ? (0, helpers_1.getFragment)('close', options) : '');
|
|
82
|
+
},
|
|
83
|
+
For: function (_json, options, component) {
|
|
84
|
+
var _a;
|
|
85
|
+
var json = _json;
|
|
86
|
+
var wrap = (0, helpers_1.wrapInFragment)(json);
|
|
87
|
+
var forArguments = (0, for_1.getForArguments)(json).join(', ');
|
|
88
|
+
return "{".concat((0, helpers_1.processBinding)((_a = json.bindings.each) === null || _a === void 0 ? void 0 : _a.code, options), "?.map((").concat(forArguments, ") => (\n ").concat(wrap ? (0, helpers_1.openFrag)(options) : '').concat(json.children
|
|
89
|
+
.filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
|
|
90
|
+
.map(function (item) { return (0, exports.blockToReact)(item, options, component); })
|
|
91
|
+
.join('\n')).concat(wrap ? (0, helpers_1.closeFrag)(options) : '', "\n ))}");
|
|
92
|
+
},
|
|
93
|
+
Show: function (json, options, component) {
|
|
94
|
+
var _a;
|
|
95
|
+
var wrap = (0, helpers_1.wrapInFragment)(json);
|
|
96
|
+
return "{".concat((0, helpers_1.processBinding)((_a = json.bindings.when) === null || _a === void 0 ? void 0 : _a.code, options), " ? (\n ").concat(wrap ? (0, helpers_1.openFrag)(options) : '').concat(json.children
|
|
97
|
+
.filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
|
|
98
|
+
.map(function (item) { return (0, exports.blockToReact)(item, options, component); })
|
|
99
|
+
.join('\n')).concat(wrap ? (0, helpers_1.closeFrag)(options) : '', "\n ) : ").concat(!json.meta.else ? 'null' : (0, exports.blockToReact)(json.meta.else, options, component), "}");
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
var ATTTRIBUTE_MAPPERS = {
|
|
103
|
+
spellcheck: 'spellCheck',
|
|
104
|
+
autocapitalize: 'autoCapitalize',
|
|
105
|
+
autocomplete: 'autoComplete',
|
|
106
|
+
for: 'htmlFor',
|
|
107
|
+
};
|
|
108
|
+
// TODO: Maybe in the future allow defining `string | function` as values
|
|
109
|
+
var BINDING_MAPPERS = __assign({ ref: function (ref, value, options) {
|
|
110
|
+
var regexp = /(.+)?props\.(.+)( |\)|;|\()?$/m;
|
|
111
|
+
if (regexp.test(value)) {
|
|
112
|
+
var match = regexp.exec(value);
|
|
113
|
+
var prop = match === null || match === void 0 ? void 0 : match[2];
|
|
114
|
+
if (prop) {
|
|
115
|
+
return [ref, prop];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return [ref, value];
|
|
119
|
+
}, innerHTML: function (_key, value) {
|
|
120
|
+
return ['dangerouslySetInnerHTML', "{__html: ".concat(value.replace(/\s+/g, ' '), "}")];
|
|
121
|
+
} }, ATTTRIBUTE_MAPPERS);
|
|
122
|
+
var blockToReact = function (json, options, component, parentSlots) {
|
|
123
|
+
var _a, _b, _c;
|
|
124
|
+
if (NODE_MAPPERS[json.name]) {
|
|
125
|
+
return NODE_MAPPERS[json.name](json, options, component, parentSlots);
|
|
126
|
+
}
|
|
127
|
+
if (json.properties._text) {
|
|
128
|
+
var text = json.properties._text;
|
|
129
|
+
if (options.type === 'native' && text.trim().length) {
|
|
130
|
+
return "<Text>".concat(text, "</Text>");
|
|
131
|
+
}
|
|
132
|
+
return text;
|
|
133
|
+
}
|
|
134
|
+
if ((_a = json.bindings._text) === null || _a === void 0 ? void 0 : _a.code) {
|
|
135
|
+
var processed = (0, helpers_1.processBinding)(json.bindings._text.code, options);
|
|
136
|
+
if (options.type === 'native') {
|
|
137
|
+
return "<Text>{".concat(processed, "}</Text>");
|
|
138
|
+
}
|
|
139
|
+
return "{".concat(processed, "}");
|
|
140
|
+
}
|
|
141
|
+
var str = '';
|
|
142
|
+
str += "<".concat(json.name, " ");
|
|
143
|
+
for (var key in json.properties) {
|
|
144
|
+
var value = (json.properties[key] || '').replace(/"/g, '"').replace(/\n/g, '\\n');
|
|
145
|
+
if (key === 'class') {
|
|
146
|
+
str = "".concat(str.trim(), " className=\"").concat(value, "\" ");
|
|
147
|
+
}
|
|
148
|
+
else if (BINDING_MAPPERS[key]) {
|
|
149
|
+
var mapper = BINDING_MAPPERS[key];
|
|
150
|
+
if (typeof mapper === 'function') {
|
|
151
|
+
var _d = mapper(key, value, options), newKey = _d[0], newValue = _d[1];
|
|
152
|
+
str += " ".concat(newKey, "={").concat(newValue, "} ");
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
str += " ".concat(BINDING_MAPPERS[key], "=\"").concat(value, "\" ");
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
if ((0, is_valid_attribute_name_1.isValidAttributeName)(key)) {
|
|
160
|
+
str += " ".concat(key, "=\"").concat(value.replace(/"/g, '"'), "\" ");
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
for (var key in json.bindings) {
|
|
165
|
+
var value = String((_b = json.bindings[key]) === null || _b === void 0 ? void 0 : _b.code);
|
|
166
|
+
if (key === 'css' && value.trim() === '{}') {
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
var useBindingValue = (0, helpers_1.processBinding)(value, options);
|
|
170
|
+
if (((_c = json.bindings[key]) === null || _c === void 0 ? void 0 : _c.type) === 'spread') {
|
|
171
|
+
str += " {...(".concat(value, ")} ");
|
|
172
|
+
}
|
|
173
|
+
else if (key.startsWith('on')) {
|
|
174
|
+
var _e = json.bindings[key].arguments, cusArgs = _e === void 0 ? ['event'] : _e;
|
|
175
|
+
str += " ".concat(key, "={(").concat(cusArgs.join(','), ") => ").concat((0, state_1.updateStateSettersInCode)(useBindingValue, options), " } ");
|
|
176
|
+
}
|
|
177
|
+
else if (key.startsWith('slot')) {
|
|
178
|
+
// <Component slotProjected={<AnotherComponent />} />
|
|
179
|
+
str += " ".concat(key, "={").concat(value, "} ");
|
|
180
|
+
}
|
|
181
|
+
else if (key === 'class') {
|
|
182
|
+
str += " className={".concat(useBindingValue, "} ");
|
|
183
|
+
}
|
|
184
|
+
else if (BINDING_MAPPERS[key]) {
|
|
185
|
+
var mapper = BINDING_MAPPERS[key];
|
|
186
|
+
if (typeof mapper === 'function') {
|
|
187
|
+
var _f = mapper(key, useBindingValue, options), newKey = _f[0], newValue = _f[1];
|
|
188
|
+
str += " ".concat(newKey, "={").concat(newValue, "} ");
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
str += " ".concat(BINDING_MAPPERS[key], "={").concat(useBindingValue, "} ");
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
else if (key === 'style' && options.type === 'native' && json.name === 'ScrollView') {
|
|
195
|
+
// React Native's ScrollView has a different prop for styles: `contentContainerStyle`
|
|
196
|
+
str += " contentContainerStyle={".concat(useBindingValue, "} ");
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
if ((0, is_valid_attribute_name_1.isValidAttributeName)(key)) {
|
|
200
|
+
str += " ".concat(key, "={").concat(useBindingValue, "} ");
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (jsx_1.selfClosingTags.has(json.name)) {
|
|
205
|
+
return str + ' />';
|
|
206
|
+
}
|
|
207
|
+
// Self close by default if no children
|
|
208
|
+
if (!json.children.length) {
|
|
209
|
+
str += ' />';
|
|
210
|
+
return str;
|
|
211
|
+
}
|
|
212
|
+
// TODO: update MitosisNode for simple code
|
|
213
|
+
var needsToRenderSlots = [];
|
|
214
|
+
var childrenNodes = '';
|
|
215
|
+
if (json.children) {
|
|
216
|
+
childrenNodes = json.children
|
|
217
|
+
.map(function (item) { return (0, exports.blockToReact)(item, options, component, needsToRenderSlots); })
|
|
218
|
+
.join('\n');
|
|
219
|
+
}
|
|
220
|
+
if (needsToRenderSlots.length) {
|
|
221
|
+
needsToRenderSlots.forEach(function (_a) {
|
|
222
|
+
var key = _a.key, value = _a.value;
|
|
223
|
+
str += " ".concat(key, "={").concat(value, "} ");
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
str = str.trim() + '>';
|
|
227
|
+
if (json.children) {
|
|
228
|
+
str += childrenNodes;
|
|
229
|
+
}
|
|
230
|
+
return str + "</".concat(json.name, ">");
|
|
231
|
+
};
|
|
232
|
+
exports.blockToReact = blockToReact;
|
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import { TranspilerGenerator } from '../../types/transpiler';
|
|
2
|
-
import { MitosisComponent } from '../../types/mitosis-component';
|
|
3
|
-
import { MitosisNode } from '../../types/mitosis-node';
|
|
4
2
|
import { ToReactOptions } from './types';
|
|
5
3
|
export declare const contextPropDrillingKey = "_context";
|
|
6
|
-
export declare const
|
|
7
|
-
export declare const
|
|
8
|
-
export declare const componentToReact: TranspilerGenerator<ToReactOptions>;
|
|
4
|
+
export declare const componentToPreact: TranspilerGenerator<Partial<ToReactOptions>>;
|
|
5
|
+
export declare const componentToReact: TranspilerGenerator<Partial<ToReactOptions>>;
|