@builder.io/mitosis 0.0.88 → 0.0.90
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/alpine/generate.js +8 -2
- package/dist/src/generators/react/blocks.d.ts +4 -0
- package/dist/src/generators/react/blocks.js +228 -0
- package/dist/src/generators/react/generator.d.ts +0 -3
- package/dist/src/generators/react/generator.js +7 -225
- 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/stencil/collect-class-string.js +1 -1
- package/dist/src/types/config.d.ts +4 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -4
|
@@ -28,6 +28,7 @@ var get_refs_1 = require("../../helpers/get-refs");
|
|
|
28
28
|
var render_update_hooks_1 = require("./render-update-hooks");
|
|
29
29
|
var render_mount_hook_1 = require("./render-mount-hook");
|
|
30
30
|
var babel_transform_1 = require("../../helpers/babel-transform");
|
|
31
|
+
var replace_identifiers_1 = require("../../helpers/replace-identifiers");
|
|
31
32
|
var checkIsComponentNode = function (node) {
|
|
32
33
|
return node.name === '@builder.io/mitosis/component';
|
|
33
34
|
};
|
|
@@ -57,7 +58,6 @@ var isValidAlpineBinding = function (str) {
|
|
|
57
58
|
};
|
|
58
59
|
exports.isValidAlpineBinding = isValidAlpineBinding;
|
|
59
60
|
var removeOnFromEventName = function (str) { return str.replace(/^on/, ''); };
|
|
60
|
-
var prefixEvent = function (str) { return str.replace(/(?<=[\s]|^)event/gm, '$event'); };
|
|
61
61
|
var removeTrailingSemicolon = function (str) { return str.replace(/;$/, ''); };
|
|
62
62
|
var trim = function (str) { return str.trim(); };
|
|
63
63
|
var replaceInputRefs = (0, lodash_1.curry)(function (json, str) {
|
|
@@ -71,7 +71,13 @@ var getStateObjectString = function (json) {
|
|
|
71
71
|
return (0, lodash_1.flow)(get_state_object_string_1.getStateObjectStringFromComponent, trim, replaceInputRefs(json), (0, render_mount_hook_1.renderMountHook)(json), (0, render_update_hooks_1.renderUpdateHooks)(json), replaceStateWithThis)(json);
|
|
72
72
|
};
|
|
73
73
|
var bindEventHandlerKey = (0, lodash_1.flowRight)(dash_case_1.dashCase, removeOnFromEventName);
|
|
74
|
-
var bindEventHandlerValue = (0, lodash_1.flowRight)(
|
|
74
|
+
var bindEventHandlerValue = (0, lodash_1.flowRight)(function (x) {
|
|
75
|
+
return (0, replace_identifiers_1.replaceIdentifiers)({
|
|
76
|
+
code: x,
|
|
77
|
+
from: 'event',
|
|
78
|
+
to: '$event',
|
|
79
|
+
});
|
|
80
|
+
}, removeTrailingSemicolon, trim, remove_surrounding_block_1.removeSurroundingBlock, strip_state_and_props_refs_1.stripStateAndPropsRefs);
|
|
75
81
|
var bindEventHandler = function (_a) {
|
|
76
82
|
var useShorthandSyntax = _a.useShorthandSyntax;
|
|
77
83
|
return function (eventName, code) {
|
|
@@ -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,228 @@
|
|
|
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 {
|
|
195
|
+
if ((0, is_valid_attribute_name_1.isValidAttributeName)(key)) {
|
|
196
|
+
str += " ".concat(key, "={").concat(useBindingValue, "} ");
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (jsx_1.selfClosingTags.has(json.name)) {
|
|
201
|
+
return str + ' />';
|
|
202
|
+
}
|
|
203
|
+
// Self close by default if no children
|
|
204
|
+
if (!json.children.length) {
|
|
205
|
+
str += ' />';
|
|
206
|
+
return str;
|
|
207
|
+
}
|
|
208
|
+
// TODO: update MitosisNode for simple code
|
|
209
|
+
var needsToRenderSlots = [];
|
|
210
|
+
var childrenNodes = '';
|
|
211
|
+
if (json.children) {
|
|
212
|
+
childrenNodes = json.children
|
|
213
|
+
.map(function (item) { return (0, exports.blockToReact)(item, options, component, needsToRenderSlots); })
|
|
214
|
+
.join('\n');
|
|
215
|
+
}
|
|
216
|
+
if (needsToRenderSlots.length) {
|
|
217
|
+
needsToRenderSlots.forEach(function (_a) {
|
|
218
|
+
var key = _a.key, value = _a.value;
|
|
219
|
+
str += " ".concat(key, "={").concat(value, "} ");
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
str = str.trim() + '>';
|
|
223
|
+
if (json.children) {
|
|
224
|
+
str += childrenNodes;
|
|
225
|
+
}
|
|
226
|
+
return str + "</".concat(json.name, ">");
|
|
227
|
+
};
|
|
228
|
+
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 blockToReact: (json: MitosisNode, options: ToReactOptions, component: MitosisComponent, parentSlots?: any[]) => string;
|
|
7
4
|
export declare const componentToPreact: TranspilerGenerator<ToReactOptions>;
|
|
8
5
|
export declare const componentToReact: TranspilerGenerator<ToReactOptions>;
|
|
@@ -18,21 +18,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
18
18
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
19
|
};
|
|
20
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
-
exports.componentToReact = exports.componentToPreact = exports.
|
|
21
|
+
exports.componentToReact = exports.componentToPreact = exports.contextPropDrillingKey = void 0;
|
|
22
22
|
var dedent_1 = __importDefault(require("dedent"));
|
|
23
23
|
var json5_1 = __importDefault(require("json5"));
|
|
24
|
-
var lodash_1 = require("lodash");
|
|
25
24
|
var standalone_1 = require("prettier/standalone");
|
|
26
25
|
var collect_css_1 = require("../../helpers/styles/collect-css");
|
|
27
26
|
var create_mitosis_node_1 = require("../../helpers/create-mitosis-node");
|
|
28
27
|
var fast_clone_1 = require("../../helpers/fast-clone");
|
|
29
|
-
var filter_empty_text_nodes_1 = require("../../helpers/filter-empty-text-nodes");
|
|
30
28
|
var get_refs_1 = require("../../helpers/get-refs");
|
|
31
29
|
var get_props_ref_1 = require("../../helpers/get-props-ref");
|
|
32
30
|
var get_state_object_string_1 = require("../../helpers/get-state-object-string");
|
|
33
31
|
var getters_to_functions_1 = require("../../helpers/getters-to-functions");
|
|
34
32
|
var handle_missing_state_1 = require("../../helpers/handle-missing-state");
|
|
35
|
-
var is_valid_attribute_name_1 = require("../../helpers/is-valid-attribute-name");
|
|
36
33
|
var map_refs_1 = require("../../helpers/map-refs");
|
|
37
34
|
var process_http_requests_1 = require("../../helpers/process-http-requests");
|
|
38
35
|
var process_tag_references_1 = require("../../helpers/process-tag-references");
|
|
@@ -40,7 +37,6 @@ var render_imports_1 = require("../../helpers/render-imports");
|
|
|
40
37
|
var replace_new_lines_in_strings_1 = require("../../helpers/replace-new-lines-in-strings");
|
|
41
38
|
var strip_meta_properties_1 = require("../../helpers/strip-meta-properties");
|
|
42
39
|
var plugins_1 = require("../../modules/plugins");
|
|
43
|
-
var jsx_1 = require("../../parsers/jsx");
|
|
44
40
|
var context_1 = require("../helpers/context");
|
|
45
41
|
var react_native_1 = require("../react-native");
|
|
46
42
|
var collect_styled_components_1 = require("../../helpers/styles/collect-styled-components");
|
|
@@ -49,15 +45,9 @@ var state_1 = require("../../helpers/state");
|
|
|
49
45
|
var state_2 = require("./state");
|
|
50
46
|
var helpers_2 = require("./helpers");
|
|
51
47
|
var hash_sum_1 = __importDefault(require("hash-sum"));
|
|
52
|
-
var for_1 = require("../../helpers/nodes/for");
|
|
53
48
|
var bindings_1 = require("../../helpers/bindings");
|
|
49
|
+
var blocks_1 = require("./blocks");
|
|
54
50
|
exports.contextPropDrillingKey = '_context';
|
|
55
|
-
var openFrag = function (options) { return getFragment('open', options); };
|
|
56
|
-
var closeFrag = function (options) { return getFragment('close', options); };
|
|
57
|
-
function getFragment(type, options) {
|
|
58
|
-
var tagName = options.preact ? 'Fragment' : '';
|
|
59
|
-
return type === 'open' ? "<".concat(tagName, ">") : "</".concat(tagName, ">");
|
|
60
|
-
}
|
|
61
51
|
/**
|
|
62
52
|
* If the root Mitosis component only has 1 child, and it is a `Show`/`For` node, then we need to wrap it in a fragment.
|
|
63
53
|
* Otherwise, we end up with invalid React render code.
|
|
@@ -66,214 +56,6 @@ function getFragment(type, options) {
|
|
|
66
56
|
var isRootSpecialNode = function (json) {
|
|
67
57
|
return json.children.length === 1 && ['Show', 'For'].includes(json.children[0].name);
|
|
68
58
|
};
|
|
69
|
-
var wrapInFragment = function (json) { return json.children.length !== 1; };
|
|
70
|
-
var NODE_MAPPERS = {
|
|
71
|
-
Slot: function (json, options, component, parentSlots) {
|
|
72
|
-
var _a;
|
|
73
|
-
var _b, _c;
|
|
74
|
-
var slotName = ((_b = json.bindings.name) === null || _b === void 0 ? void 0 : _b.code) || json.properties.name;
|
|
75
|
-
var hasChildren = json.children.length;
|
|
76
|
-
var renderChildren = function () {
|
|
77
|
-
var _a;
|
|
78
|
-
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();
|
|
79
|
-
/**
|
|
80
|
-
* Ad-hoc way of figuring out if the children defaultProp is:
|
|
81
|
-
* - a JSX element, e.g. `<div>foo</div>`
|
|
82
|
-
* - a JS expression, e.g. `true`, `false`
|
|
83
|
-
* - a string, e.g. `'Default text'`
|
|
84
|
-
*
|
|
85
|
-
* and correctly wrapping it in quotes when appropriate.
|
|
86
|
-
*/
|
|
87
|
-
if (childrenStr.startsWith("<") && childrenStr.endsWith(">")) {
|
|
88
|
-
return childrenStr;
|
|
89
|
-
}
|
|
90
|
-
else if (['false', 'true', 'null', 'undefined'].includes(childrenStr)) {
|
|
91
|
-
return childrenStr;
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
return "\"".concat(childrenStr, "\"");
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
if (!slotName) {
|
|
98
|
-
// TODO: update MitosisNode for simple code
|
|
99
|
-
var key = Object.keys(json.bindings).find(Boolean);
|
|
100
|
-
if (key && parentSlots) {
|
|
101
|
-
var propKey = (0, lodash_1.camelCase)('Slot' + key[0].toUpperCase() + key.substring(1));
|
|
102
|
-
parentSlots.push({ key: propKey, value: (_c = json.bindings[key]) === null || _c === void 0 ? void 0 : _c.code });
|
|
103
|
-
return '';
|
|
104
|
-
}
|
|
105
|
-
if (hasChildren) {
|
|
106
|
-
component.defaultProps = __assign(__assign({}, (component.defaultProps || {})), { children: {
|
|
107
|
-
code: renderChildren(),
|
|
108
|
-
type: 'property',
|
|
109
|
-
} });
|
|
110
|
-
}
|
|
111
|
-
return "{".concat((0, helpers_2.processBinding)('props.children', options), "}");
|
|
112
|
-
}
|
|
113
|
-
var slotProp = (0, helpers_2.processBinding)(slotName, options).replace('name=', '');
|
|
114
|
-
if (!slotProp.startsWith('props.slot')) {
|
|
115
|
-
slotProp = "props.slot".concat((0, lodash_1.upperFirst)((0, lodash_1.camelCase)(slotProp)));
|
|
116
|
-
}
|
|
117
|
-
if (hasChildren) {
|
|
118
|
-
component.defaultProps = __assign(__assign({}, (component.defaultProps || {})), (_a = {}, _a[slotProp.replace('props.', '')] = {
|
|
119
|
-
code: renderChildren(),
|
|
120
|
-
type: 'property',
|
|
121
|
-
}, _a));
|
|
122
|
-
}
|
|
123
|
-
return "{".concat(slotProp, "}");
|
|
124
|
-
},
|
|
125
|
-
Fragment: function (json, options, component) {
|
|
126
|
-
var wrap = wrapInFragment(json);
|
|
127
|
-
return "".concat(wrap ? getFragment('open', options) : '').concat(json.children
|
|
128
|
-
.map(function (item) { return (0, exports.blockToReact)(item, options, component); })
|
|
129
|
-
.join('\n')).concat(wrap ? getFragment('close', options) : '');
|
|
130
|
-
},
|
|
131
|
-
For: function (_json, options, component) {
|
|
132
|
-
var _a;
|
|
133
|
-
var json = _json;
|
|
134
|
-
var wrap = wrapInFragment(json);
|
|
135
|
-
var forArguments = (0, for_1.getForArguments)(json).join(', ');
|
|
136
|
-
return "{".concat((0, helpers_2.processBinding)((_a = json.bindings.each) === null || _a === void 0 ? void 0 : _a.code, options), "?.map((").concat(forArguments, ") => (\n ").concat(wrap ? openFrag(options) : '').concat(json.children
|
|
137
|
-
.filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
|
|
138
|
-
.map(function (item) { return (0, exports.blockToReact)(item, options, component); })
|
|
139
|
-
.join('\n')).concat(wrap ? closeFrag(options) : '', "\n ))}");
|
|
140
|
-
},
|
|
141
|
-
Show: function (json, options, component) {
|
|
142
|
-
var _a;
|
|
143
|
-
var wrap = wrapInFragment(json);
|
|
144
|
-
return "{".concat((0, helpers_2.processBinding)((_a = json.bindings.when) === null || _a === void 0 ? void 0 : _a.code, options), " ? (\n ").concat(wrap ? openFrag(options) : '').concat(json.children
|
|
145
|
-
.filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
|
|
146
|
-
.map(function (item) { return (0, exports.blockToReact)(item, options, component); })
|
|
147
|
-
.join('\n')).concat(wrap ? closeFrag(options) : '', "\n ) : ").concat(!json.meta.else ? 'null' : (0, exports.blockToReact)(json.meta.else, options, component), "}");
|
|
148
|
-
},
|
|
149
|
-
};
|
|
150
|
-
var ATTTRIBUTE_MAPPERS = {
|
|
151
|
-
spellcheck: 'spellCheck',
|
|
152
|
-
autocapitalize: 'autoCapitalize',
|
|
153
|
-
autocomplete: 'autoComplete',
|
|
154
|
-
for: 'htmlFor',
|
|
155
|
-
};
|
|
156
|
-
// TODO: Maybe in the future allow defining `string | function` as values
|
|
157
|
-
var BINDING_MAPPERS = __assign({ ref: function (ref, value, options) {
|
|
158
|
-
var regexp = /(.+)?props\.(.+)( |\)|;|\()?$/m;
|
|
159
|
-
if (regexp.test(value)) {
|
|
160
|
-
var match = regexp.exec(value);
|
|
161
|
-
var prop = match === null || match === void 0 ? void 0 : match[2];
|
|
162
|
-
if (prop) {
|
|
163
|
-
return [ref, prop];
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
return [ref, value];
|
|
167
|
-
}, innerHTML: function (_key, value) {
|
|
168
|
-
return ['dangerouslySetInnerHTML', "{__html: ".concat(value.replace(/\s+/g, ' '), "}")];
|
|
169
|
-
} }, ATTTRIBUTE_MAPPERS);
|
|
170
|
-
var blockToReact = function (json, options, component, parentSlots) {
|
|
171
|
-
var _a, _b, _c;
|
|
172
|
-
if (NODE_MAPPERS[json.name]) {
|
|
173
|
-
return NODE_MAPPERS[json.name](json, options, component, parentSlots);
|
|
174
|
-
}
|
|
175
|
-
if (json.properties._text) {
|
|
176
|
-
var text = json.properties._text;
|
|
177
|
-
if (options.type === 'native' && text.trim().length) {
|
|
178
|
-
return "<Text>".concat(text, "</Text>");
|
|
179
|
-
}
|
|
180
|
-
return text;
|
|
181
|
-
}
|
|
182
|
-
if ((_a = json.bindings._text) === null || _a === void 0 ? void 0 : _a.code) {
|
|
183
|
-
var processed = (0, helpers_2.processBinding)(json.bindings._text.code, options);
|
|
184
|
-
if (options.type === 'native') {
|
|
185
|
-
return "<Text>{".concat(processed, "}</Text>");
|
|
186
|
-
}
|
|
187
|
-
return "{".concat(processed, "}");
|
|
188
|
-
}
|
|
189
|
-
var str = '';
|
|
190
|
-
str += "<".concat(json.name, " ");
|
|
191
|
-
for (var key in json.properties) {
|
|
192
|
-
var value = (json.properties[key] || '').replace(/"/g, '"').replace(/\n/g, '\\n');
|
|
193
|
-
if (key === 'class') {
|
|
194
|
-
str = "".concat(str.trim(), " className=\"").concat(value, "\" ");
|
|
195
|
-
}
|
|
196
|
-
else if (BINDING_MAPPERS[key]) {
|
|
197
|
-
var mapper = BINDING_MAPPERS[key];
|
|
198
|
-
if (typeof mapper === 'function') {
|
|
199
|
-
var _d = mapper(key, value, options), newKey = _d[0], newValue = _d[1];
|
|
200
|
-
str += " ".concat(newKey, "={").concat(newValue, "} ");
|
|
201
|
-
}
|
|
202
|
-
else {
|
|
203
|
-
str += " ".concat(BINDING_MAPPERS[key], "=\"").concat(value, "\" ");
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
if ((0, is_valid_attribute_name_1.isValidAttributeName)(key)) {
|
|
208
|
-
str += " ".concat(key, "=\"").concat(value.replace(/"/g, '"'), "\" ");
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
for (var key in json.bindings) {
|
|
213
|
-
var value = String((_b = json.bindings[key]) === null || _b === void 0 ? void 0 : _b.code);
|
|
214
|
-
if (key === 'css' && value.trim() === '{}') {
|
|
215
|
-
continue;
|
|
216
|
-
}
|
|
217
|
-
var useBindingValue = (0, helpers_2.processBinding)(value, options);
|
|
218
|
-
if (((_c = json.bindings[key]) === null || _c === void 0 ? void 0 : _c.type) === 'spread') {
|
|
219
|
-
str += " {...(".concat(value, ")} ");
|
|
220
|
-
}
|
|
221
|
-
else if (key.startsWith('on')) {
|
|
222
|
-
var _e = json.bindings[key].arguments, cusArgs = _e === void 0 ? ['event'] : _e;
|
|
223
|
-
str += " ".concat(key, "={(").concat(cusArgs.join(','), ") => ").concat((0, state_2.updateStateSettersInCode)(useBindingValue, options), " } ");
|
|
224
|
-
}
|
|
225
|
-
else if (key.startsWith('slot')) {
|
|
226
|
-
// <Component slotProjected={<AnotherComponent />} />
|
|
227
|
-
str += " ".concat(key, "={").concat(value, "} ");
|
|
228
|
-
}
|
|
229
|
-
else if (key === 'class') {
|
|
230
|
-
str += " className={".concat(useBindingValue, "} ");
|
|
231
|
-
}
|
|
232
|
-
else if (BINDING_MAPPERS[key]) {
|
|
233
|
-
var mapper = BINDING_MAPPERS[key];
|
|
234
|
-
if (typeof mapper === 'function') {
|
|
235
|
-
var _f = mapper(key, useBindingValue, options), newKey = _f[0], newValue = _f[1];
|
|
236
|
-
str += " ".concat(newKey, "={").concat(newValue, "} ");
|
|
237
|
-
}
|
|
238
|
-
else {
|
|
239
|
-
str += " ".concat(BINDING_MAPPERS[key], "={").concat(useBindingValue, "} ");
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
else {
|
|
243
|
-
if ((0, is_valid_attribute_name_1.isValidAttributeName)(key)) {
|
|
244
|
-
str += " ".concat(key, "={").concat(useBindingValue, "} ");
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
if (jsx_1.selfClosingTags.has(json.name)) {
|
|
249
|
-
return str + ' />';
|
|
250
|
-
}
|
|
251
|
-
// Self close by default if no children
|
|
252
|
-
if (!json.children.length) {
|
|
253
|
-
str += ' />';
|
|
254
|
-
return str;
|
|
255
|
-
}
|
|
256
|
-
// TODO: update MitosisNode for simple code
|
|
257
|
-
var needsToRenderSlots = [];
|
|
258
|
-
var childrenNodes = '';
|
|
259
|
-
if (json.children) {
|
|
260
|
-
childrenNodes = json.children
|
|
261
|
-
.map(function (item) { return (0, exports.blockToReact)(item, options, component, needsToRenderSlots); })
|
|
262
|
-
.join('\n');
|
|
263
|
-
}
|
|
264
|
-
if (needsToRenderSlots.length) {
|
|
265
|
-
needsToRenderSlots.forEach(function (_a) {
|
|
266
|
-
var key = _a.key, value = _a.value;
|
|
267
|
-
str += " ".concat(key, "={").concat(value, "} ");
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
str = str.trim() + '>';
|
|
271
|
-
if (json.children) {
|
|
272
|
-
str += childrenNodes;
|
|
273
|
-
}
|
|
274
|
-
return str + "</".concat(json.name, ">");
|
|
275
|
-
};
|
|
276
|
-
exports.blockToReact = blockToReact;
|
|
277
59
|
var getRefsString = function (json, refs, options) {
|
|
278
60
|
var _a, _b;
|
|
279
61
|
var hasStateArgument = false;
|
|
@@ -460,7 +242,7 @@ var _componentToReact = function (json, options, isSubComponent) {
|
|
|
460
242
|
((_f = json.hooks.onInit) === null || _f === void 0 ? void 0 : _f.code)) {
|
|
461
243
|
reactLibImports.add('useEffect');
|
|
462
244
|
}
|
|
463
|
-
var wrap = wrapInFragment(json) ||
|
|
245
|
+
var wrap = (0, helpers_2.wrapInFragment)(json) ||
|
|
464
246
|
(componentHasStyles && (stylesType === 'styled-jsx' || stylesType === 'style-tag')) ||
|
|
465
247
|
isRootSpecialNode(json);
|
|
466
248
|
var _o = getRefsString(json, allRefs, options), hasStateArgument = _o[0], refsString = _o[1];
|
|
@@ -486,7 +268,7 @@ var _componentToReact = function (json, options, isSubComponent) {
|
|
|
486
268
|
? "\n /** @jsx h */\n import { h, Fragment } from 'preact';\n "
|
|
487
269
|
: options.type !== 'native'
|
|
488
270
|
? "import * as React from 'react';"
|
|
489
|
-
: "\n import * as React from 'react';\n import { View, StyleSheet, Image, Text } from 'react-native';\n ", styledComponentsCode ? "import styled from 'styled-components';\n" : '', reactLibImports.size
|
|
271
|
+
: "\n import * as React from 'react';\n import { FlatList, ScrollView, View, StyleSheet, Image, Text } from 'react-native';\n ", styledComponentsCode ? "import styled from 'styled-components';\n" : '', reactLibImports.size
|
|
490
272
|
? "import { ".concat(Array.from(reactLibImports).join(', '), " } from '").concat(options.preact ? 'preact/hooks' : 'react', "'")
|
|
491
273
|
: '', componentHasStyles && stylesType === 'emotion' && options.format !== 'lite'
|
|
492
274
|
? "/** @jsx jsx */\n import { jsx } from '@emotion/react'".trim()
|
|
@@ -515,7 +297,7 @@ var _componentToReact = function (json, options, isSubComponent) {
|
|
|
515
297
|
? "\n useEffect(() => {\n ".concat((0, state_2.processHookCode)({
|
|
516
298
|
str: json.hooks.onInit.code,
|
|
517
299
|
options: options,
|
|
518
|
-
}), "\n })\n ")
|
|
300
|
+
}), "\n }, [])\n ")
|
|
519
301
|
: '', ((_h = json.hooks.onMount) === null || _h === void 0 ? void 0 : _h.code)
|
|
520
302
|
? "useEffect(() => {\n ".concat((0, state_2.processHookCode)({
|
|
521
303
|
str: json.hooks.onMount.code,
|
|
@@ -526,11 +308,11 @@ var _componentToReact = function (json, options, isSubComponent) {
|
|
|
526
308
|
str: json.hooks.onUnMount.code,
|
|
527
309
|
options: options,
|
|
528
310
|
}), "\n }\n }, [])")
|
|
529
|
-
: '', wrap ? openFrag(options) : '', json.children.map(function (item) { return (0,
|
|
311
|
+
: '', wrap ? (0, helpers_2.openFrag)(options) : '', json.children.map(function (item) { return (0, blocks_1.blockToReact)(item, options, json, []); }).join('\n'), componentHasStyles && stylesType === 'styled-jsx'
|
|
530
312
|
? "<style jsx>{`".concat(css, "`}</style>")
|
|
531
313
|
: componentHasStyles && stylesType === 'style-tag'
|
|
532
314
|
? "<style>{`".concat(css, "`}</style>")
|
|
533
|
-
: '', wrap ? closeFrag(options) : '', isForwardRef ? ')' : '', getPropsDefinition({ json: json }), !nativeStyles
|
|
315
|
+
: '', wrap ? (0, helpers_2.closeFrag)(options) : '', isForwardRef ? ')' : '', getPropsDefinition({ json: json }), !nativeStyles
|
|
534
316
|
? ''
|
|
535
317
|
: "\n const styles = StyleSheet.create(".concat(json5_1.default.stringify(nativeStyles), ");\n "), styledComponentsCode ? styledComponentsCode : '', stateType === 'mobx'
|
|
536
318
|
? "\n const observed".concat(json.name || 'MyComponent', " = observer(").concat(json.name || 'MyComponent', ");\n export default observed").concat(json.name || 'MyComponent', ";\n ")
|
|
@@ -1,2 +1,8 @@
|
|
|
1
|
+
import { MitosisComponent } from '../../types/mitosis-component';
|
|
2
|
+
import { MitosisNode } from '../../types/mitosis-node';
|
|
1
3
|
import { ToReactOptions } from './types';
|
|
2
4
|
export declare const processBinding: (str: string, options: ToReactOptions) => string;
|
|
5
|
+
export declare const openFrag: (options: ToReactOptions) => string;
|
|
6
|
+
export declare const closeFrag: (options: ToReactOptions) => string;
|
|
7
|
+
export declare function getFragment(type: 'open' | 'close', options: ToReactOptions): string;
|
|
8
|
+
export declare const wrapInFragment: (json: MitosisComponent | MitosisNode) => boolean;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.processBinding = void 0;
|
|
3
|
+
exports.wrapInFragment = exports.getFragment = exports.closeFrag = exports.openFrag = exports.processBinding = void 0;
|
|
4
4
|
var strip_state_and_props_refs_1 = require("../../helpers/strip-state-and-props-refs");
|
|
5
5
|
var processBinding = function (str, options) {
|
|
6
6
|
if (options.stateType !== 'useState') {
|
|
@@ -12,3 +12,14 @@ var processBinding = function (str, options) {
|
|
|
12
12
|
});
|
|
13
13
|
};
|
|
14
14
|
exports.processBinding = processBinding;
|
|
15
|
+
var openFrag = function (options) { return getFragment('open', options); };
|
|
16
|
+
exports.openFrag = openFrag;
|
|
17
|
+
var closeFrag = function (options) { return getFragment('close', options); };
|
|
18
|
+
exports.closeFrag = closeFrag;
|
|
19
|
+
function getFragment(type, options) {
|
|
20
|
+
var tagName = options.preact ? 'Fragment' : '';
|
|
21
|
+
return type === 'open' ? "<".concat(tagName, ">") : "</".concat(tagName, ">");
|
|
22
|
+
}
|
|
23
|
+
exports.getFragment = getFragment;
|
|
24
|
+
var wrapInFragment = function (json) { return json.children.length !== 1; };
|
|
25
|
+
exports.wrapInFragment = wrapInFragment;
|
|
@@ -14,5 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.blockToReact = void 0;
|
|
17
18
|
__exportStar(require("./generator"), exports);
|
|
18
19
|
__exportStar(require("./types"), exports);
|
|
20
|
+
var blocks_1 = require("./blocks");
|
|
21
|
+
Object.defineProperty(exports, "blockToReact", { enumerable: true, get: function () { return blocks_1.blockToReact; } });
|
|
@@ -8,7 +8,6 @@ function collectClassString(json, bindingOpenChar, bindingCloseChar) {
|
|
|
8
8
|
if (bindingOpenChar === void 0) { bindingOpenChar = '{'; }
|
|
9
9
|
if (bindingCloseChar === void 0) { bindingCloseChar = '}'; }
|
|
10
10
|
var staticClasses = [];
|
|
11
|
-
var hasStaticClasses = Boolean(staticClasses.length);
|
|
12
11
|
if (json.properties.class) {
|
|
13
12
|
staticClasses.push(json.properties.class);
|
|
14
13
|
delete json.properties.class;
|
|
@@ -28,6 +27,7 @@ function collectClassString(json, bindingOpenChar, bindingCloseChar) {
|
|
|
28
27
|
}
|
|
29
28
|
var staticClassesString = staticClasses.join(' ');
|
|
30
29
|
var dynamicClassesString = dynamicClasses.join(" + ' ' + ");
|
|
30
|
+
var hasStaticClasses = Boolean(staticClasses.length);
|
|
31
31
|
var hasDynamicClasses = Boolean(dynamicClasses.length);
|
|
32
32
|
if (hasStaticClasses && !hasDynamicClasses) {
|
|
33
33
|
return "\"".concat(staticClassesString, "\"");
|
|
@@ -24,6 +24,10 @@ export declare type MitosisConfig = {
|
|
|
24
24
|
* globs of files to transpile. Defaults to `src/*`.
|
|
25
25
|
*/
|
|
26
26
|
files?: string | string[];
|
|
27
|
+
/**
|
|
28
|
+
* Optional list of globs to exclude from transpilation.
|
|
29
|
+
*/
|
|
30
|
+
exclude?: string[];
|
|
27
31
|
/**
|
|
28
32
|
* The directory where overrides are stored. The structure of the override directory must match that of the source code,
|
|
29
33
|
* with each target having its own sub-directory: `${overridesDir}/${target}/*`
|