@builder.io/mitosis 0.9.5 → 0.10.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/src/generators/builder/generator.js +9 -0
- package/dist/src/generators/mitosis/generator.js +31 -0
- package/dist/src/parsers/builder/builder.d.ts +7 -0
- package/dist/src/parsers/builder/builder.js +38 -7
- package/dist/src/parsers/jsx/element-parser.d.ts +2 -1
- package/dist/src/parsers/jsx/element-parser.js +92 -14
- package/dist/src/parsers/jsx/function-parser.d.ts +2 -2
- package/dist/src/parsers/jsx/function-parser.js +2 -2
- package/dist/src/parsers/jsx/jsx.js +2 -2
- package/dist/src/parsers/jsx/types.d.ts +7 -0
- package/dist/src/parsers/svelte/html/text.d.ts +3 -0
- package/dist/src/types/mitosis-node.d.ts +8 -0
- package/package.json +1 -1
|
@@ -541,6 +541,15 @@ const blockToBuilder = (json, options = {}, _internalOptions = {}) => {
|
|
|
541
541
|
for (const key in json.slots) {
|
|
542
542
|
componentOptions[key] = json.slots[key].map((node) => (0, exports.blockToBuilder)(node, options));
|
|
543
543
|
}
|
|
544
|
+
for (const key in json.blocksSlots) {
|
|
545
|
+
const value = json.blocksSlots[key];
|
|
546
|
+
(0, legacy_1.default)(value).forEach(function (v) {
|
|
547
|
+
if ((0, is_mitosis_node_1.isMitosisNode)(v)) {
|
|
548
|
+
this.update((0, exports.blockToBuilder)(v, options, _internalOptions));
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
componentOptions[key] = value;
|
|
552
|
+
}
|
|
544
553
|
const hasCss = !!((_l = bindings.css) === null || _l === void 0 ? void 0 : _l.code);
|
|
545
554
|
let responsiveStyles = {
|
|
546
555
|
large: {},
|
|
@@ -20,6 +20,7 @@ const state_1 = require("../../helpers/state");
|
|
|
20
20
|
const plugins_1 = require("../../modules/plugins");
|
|
21
21
|
const mitosis_node_1 = require("../../types/mitosis-node");
|
|
22
22
|
const json5_1 = __importDefault(require("json5"));
|
|
23
|
+
const legacy_1 = __importDefault(require("neotraverse/legacy"));
|
|
23
24
|
const standalone_1 = require("prettier/standalone");
|
|
24
25
|
const react_1 = require("../react");
|
|
25
26
|
exports.DEFAULT_FORMAT = 'legacy';
|
|
@@ -197,6 +198,15 @@ const blockToMitosis = (json, toMitosisOptions = {}, component, insideJsx) => {
|
|
|
197
198
|
}
|
|
198
199
|
str += `}`;
|
|
199
200
|
}
|
|
201
|
+
for (const key in json.blocksSlots) {
|
|
202
|
+
const value = json.blocksSlots[key];
|
|
203
|
+
(0, legacy_1.default)(value).forEach(function (v) {
|
|
204
|
+
if ((0, is_mitosis_node_1.isMitosisNode)(v)) {
|
|
205
|
+
this.update((0, exports.blockToMitosis)(v, toMitosisOptions, component, insideJsx));
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
str += `${key}={${generateBlockSlotsCode(value)}}`;
|
|
209
|
+
}
|
|
200
210
|
if (html_tags_1.SELF_CLOSING_HTML_TAGS.has(json.name)) {
|
|
201
211
|
return str + ' />';
|
|
202
212
|
}
|
|
@@ -213,6 +223,27 @@ const blockToMitosis = (json, toMitosisOptions = {}, component, insideJsx) => {
|
|
|
213
223
|
return str;
|
|
214
224
|
};
|
|
215
225
|
exports.blockToMitosis = blockToMitosis;
|
|
226
|
+
const generateBlockSlotsCode = (blockSlot) => {
|
|
227
|
+
let code = '';
|
|
228
|
+
// generate array props (foo=[...])
|
|
229
|
+
if (Array.isArray(blockSlot)) {
|
|
230
|
+
code += `[${blockSlot.map(generateBlockSlotsCode).join(',')}]`;
|
|
231
|
+
// generate object props (foo={{ ... }})
|
|
232
|
+
}
|
|
233
|
+
else if (typeof blockSlot === 'object' && blockSlot !== null) {
|
|
234
|
+
code += '{';
|
|
235
|
+
for (const key in blockSlot) {
|
|
236
|
+
if (blockSlot.hasOwnProperty(key)) {
|
|
237
|
+
code += `${key}: ${generateBlockSlotsCode(blockSlot[key])},`;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
code += '}';
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
code += blockSlot;
|
|
244
|
+
}
|
|
245
|
+
return code;
|
|
246
|
+
};
|
|
216
247
|
const getRefsString = (json, refs = Array.from((0, get_refs_1.getRefs)(json))) => {
|
|
217
248
|
var _a, _b;
|
|
218
249
|
let str = '';
|
|
@@ -19,6 +19,13 @@ type BuilderToMitosisOptions = {
|
|
|
19
19
|
* Defaults to `false`.
|
|
20
20
|
*/
|
|
21
21
|
escapeInvalidCode?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* When `true`, the `blocksSlots` field on Mitosis Nodes will be used to transform
|
|
24
|
+
* deeply nested Builder elements found in component options. Note that not every
|
|
25
|
+
* generator supports parsing `blocksSlots`.
|
|
26
|
+
* Defaults to `false`.
|
|
27
|
+
*/
|
|
28
|
+
enableBlocksSlots?: boolean;
|
|
22
29
|
};
|
|
23
30
|
export declare const builderElementToMitosisNode: (block: BuilderElement, options: BuilderToMitosisOptions, _internalOptions?: InternalOptions) => MitosisNode;
|
|
24
31
|
export declare const getMetaFromBlock: (block: BuilderElement, options: BuilderToMitosisOptions) => {};
|
|
@@ -584,7 +584,7 @@ const componentMappers = {
|
|
|
584
584
|
},
|
|
585
585
|
};
|
|
586
586
|
const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
|
|
587
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
|
|
587
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
|
|
588
588
|
const { includeSpecialBindings = true } = options;
|
|
589
589
|
const localizedValues = {};
|
|
590
590
|
if (((_a = block.component) === null || _a === void 0 ? void 0 : _a.name) === 'Core:Fragment') {
|
|
@@ -675,6 +675,7 @@ const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
|
|
|
675
675
|
const bindings = {};
|
|
676
676
|
const children = [];
|
|
677
677
|
const slots = {};
|
|
678
|
+
const blocksSlots = {};
|
|
678
679
|
if (blockBindings) {
|
|
679
680
|
for (const key in blockBindings) {
|
|
680
681
|
if (key === 'css') {
|
|
@@ -772,6 +773,35 @@ const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
|
|
|
772
773
|
.map(transformBldrElementToMitosisNode);
|
|
773
774
|
slots[key] = childrenElements;
|
|
774
775
|
}
|
|
776
|
+
else if (options.enableBlocksSlots &&
|
|
777
|
+
!componentMappers[(_p = block.component) === null || _p === void 0 ? void 0 : _p.name] &&
|
|
778
|
+
(Array.isArray(value) || (typeof value === 'object' && value !== null))) {
|
|
779
|
+
/**
|
|
780
|
+
* Builder Elements that have their own mappers should not use blocksSlots
|
|
781
|
+
* even if the mapper is disabled via _internalOptions as it will cause
|
|
782
|
+
* problems when trying to use the mapper in the future.
|
|
783
|
+
*/
|
|
784
|
+
const data = Array.isArray(value) ? [...value] : { ...value };
|
|
785
|
+
let hasElement = false;
|
|
786
|
+
(0, legacy_1.default)(data).forEach(function (d) {
|
|
787
|
+
if ((0, exports.isBuilderElement)(d)) {
|
|
788
|
+
/**
|
|
789
|
+
* Replacing the Builder element with a Mitosis node in-place
|
|
790
|
+
* allows us to assign to blockSlots while preserving the structure
|
|
791
|
+
* of this deeply nested data.
|
|
792
|
+
*/
|
|
793
|
+
this.update((0, exports.builderElementToMitosisNode)(d, options, _internalOptions));
|
|
794
|
+
hasElement = true;
|
|
795
|
+
}
|
|
796
|
+
});
|
|
797
|
+
// If no elements were updated then this is just a regular binding
|
|
798
|
+
if (hasElement) {
|
|
799
|
+
blocksSlots[key] = data;
|
|
800
|
+
}
|
|
801
|
+
else {
|
|
802
|
+
bindings[key] = (0, bindings_1.createSingleBinding)({ code: json5_1.default.stringify(value) });
|
|
803
|
+
}
|
|
804
|
+
}
|
|
775
805
|
else {
|
|
776
806
|
bindings[key] = (0, bindings_1.createSingleBinding)({ code: json5_1.default.stringify(value) });
|
|
777
807
|
}
|
|
@@ -788,7 +818,7 @@ const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
|
|
|
788
818
|
}
|
|
789
819
|
}
|
|
790
820
|
const node = (0, create_mitosis_node_1.createMitosisNode)({
|
|
791
|
-
name: ((
|
|
821
|
+
name: ((_r = (_q = block.component) === null || _q === void 0 ? void 0 : _q.name) === null || _r === void 0 ? void 0 : _r.replace(/[^a-z0-9]/gi, '')) ||
|
|
792
822
|
block.tagName ||
|
|
793
823
|
(block.linkUrl ? 'a' : 'div'),
|
|
794
824
|
properties: {
|
|
@@ -810,17 +840,18 @@ const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
|
|
|
810
840
|
slots: {
|
|
811
841
|
...slots,
|
|
812
842
|
},
|
|
843
|
+
...(Object.keys(blocksSlots).length > 0 && { blocksSlots }),
|
|
813
844
|
meta: (0, exports.getMetaFromBlock)(block, options),
|
|
814
845
|
...(Object.keys(localizedValues).length && { localizedValues }),
|
|
815
846
|
});
|
|
816
847
|
// Has single text node child
|
|
817
|
-
const firstChild = (
|
|
818
|
-
if (((
|
|
819
|
-
((
|
|
848
|
+
const firstChild = (_s = block.children) === null || _s === void 0 ? void 0 : _s[0];
|
|
849
|
+
if (((_t = block.children) === null || _t === void 0 ? void 0 : _t.length) === 1 &&
|
|
850
|
+
((_u = firstChild === null || firstChild === void 0 ? void 0 : firstChild.component) === null || _u === void 0 ? void 0 : _u.name) === 'Text' &&
|
|
820
851
|
!options.preserveTextBlocks) {
|
|
821
852
|
const textProperties = (0, exports.builderElementToMitosisNode)(firstChild, options);
|
|
822
|
-
const parsedNodeCss = json5_1.default.parse(((
|
|
823
|
-
const parsedTextCss = json5_1.default.parse(((
|
|
853
|
+
const parsedNodeCss = json5_1.default.parse(((_v = node.bindings.css) === null || _v === void 0 ? void 0 : _v.code) || '{}');
|
|
854
|
+
const parsedTextCss = json5_1.default.parse(((_w = textProperties.bindings.css) === null || _w === void 0 ? void 0 : _w.code) || '{}');
|
|
824
855
|
const mergedCss = combineStyles(parsedNodeCss, parsedTextCss);
|
|
825
856
|
// Don't merge if text has styling that matters
|
|
826
857
|
const doNotMerge =
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import * as babel from '@babel/core';
|
|
2
2
|
import { MitosisNode } from '../../types/mitosis-node';
|
|
3
|
+
import type { ParseMitosisOptions } from './types';
|
|
3
4
|
declare const types: typeof babel.types;
|
|
4
5
|
/**
|
|
5
6
|
* Parses a JSX element into a MitosisNode.
|
|
6
7
|
*/
|
|
7
|
-
export declare const jsxElementToJson: (node: babel.types.Expression | babel.types.JSX) => MitosisNode | null;
|
|
8
|
+
export declare const jsxElementToJson: (node: babel.types.Expression | babel.types.JSX, options: Partial<ParseMitosisOptions>) => MitosisNode | null;
|
|
8
9
|
export {};
|
|
@@ -30,6 +30,7 @@ exports.jsxElementToJson = void 0;
|
|
|
30
30
|
const babel = __importStar(require("@babel/core"));
|
|
31
31
|
const generator_1 = __importDefault(require("@babel/generator"));
|
|
32
32
|
const function_1 = require("fp-ts/lib/function");
|
|
33
|
+
const json5_1 = __importDefault(require("json5"));
|
|
33
34
|
const bindings_1 = require("../../helpers/bindings");
|
|
34
35
|
const create_mitosis_node_1 = require("../../helpers/create-mitosis-node");
|
|
35
36
|
const nullable_1 = require("../../helpers/nullable");
|
|
@@ -65,7 +66,7 @@ const getForArguments = (params) => {
|
|
|
65
66
|
/**
|
|
66
67
|
* Parses a JSX element into a MitosisNode.
|
|
67
68
|
*/
|
|
68
|
-
const jsxElementToJson = (node) => {
|
|
69
|
+
const jsxElementToJson = (node, options) => {
|
|
69
70
|
var _a;
|
|
70
71
|
if (types.isJSXText(node)) {
|
|
71
72
|
const value = typeof ((_a = node.extra) === null || _a === void 0 ? void 0 : _a.raw) === 'string' ? node.extra.raw : node.value;
|
|
@@ -79,7 +80,7 @@ const jsxElementToJson = (node) => {
|
|
|
79
80
|
return null;
|
|
80
81
|
}
|
|
81
82
|
if (types.isJSXExpressionContainer(node)) {
|
|
82
|
-
return (0, exports.jsxElementToJson)(node.expression);
|
|
83
|
+
return (0, exports.jsxElementToJson)(node.expression, options);
|
|
83
84
|
}
|
|
84
85
|
if ((types.isCallExpression(node) || types.isOptionalCallExpression(node)) &&
|
|
85
86
|
(node.callee.type === 'MemberExpression' || node.callee.type === 'OptionalMemberExpression')) {
|
|
@@ -103,7 +104,7 @@ const jsxElementToJson = (node) => {
|
|
|
103
104
|
}),
|
|
104
105
|
},
|
|
105
106
|
scope: forArguments,
|
|
106
|
-
children: [(0, exports.jsxElementToJson)(bodyExpression)].filter(nullable_1.checkIsDefined),
|
|
107
|
+
children: [(0, exports.jsxElementToJson)(bodyExpression, options)].filter(nullable_1.checkIsDefined),
|
|
107
108
|
});
|
|
108
109
|
}
|
|
109
110
|
}
|
|
@@ -127,7 +128,7 @@ const jsxElementToJson = (node) => {
|
|
|
127
128
|
}),
|
|
128
129
|
},
|
|
129
130
|
scope: forArguments,
|
|
130
|
-
children: [(0, exports.jsxElementToJson)(bodyExpression)],
|
|
131
|
+
children: [(0, exports.jsxElementToJson)(bodyExpression, options)],
|
|
131
132
|
});
|
|
132
133
|
}
|
|
133
134
|
}
|
|
@@ -144,7 +145,7 @@ const jsxElementToJson = (node) => {
|
|
|
144
145
|
}).code,
|
|
145
146
|
}),
|
|
146
147
|
},
|
|
147
|
-
children: [(0, exports.jsxElementToJson)(node.right)].filter(nullable_1.checkIsDefined),
|
|
148
|
+
children: [(0, exports.jsxElementToJson)(node.right, options)].filter(nullable_1.checkIsDefined),
|
|
148
149
|
});
|
|
149
150
|
}
|
|
150
151
|
else {
|
|
@@ -153,8 +154,8 @@ const jsxElementToJson = (node) => {
|
|
|
153
154
|
}
|
|
154
155
|
else if (types.isConditionalExpression(node)) {
|
|
155
156
|
// {foo ? <div /> : <span />} -> <Show when={foo} else={<span />}>...</Show>
|
|
156
|
-
const child = (0, exports.jsxElementToJson)(node.consequent);
|
|
157
|
-
const elseCase = (0, exports.jsxElementToJson)(node.alternate);
|
|
157
|
+
const child = (0, exports.jsxElementToJson)(node.consequent, options);
|
|
158
|
+
const elseCase = (0, exports.jsxElementToJson)(node.alternate, options);
|
|
158
159
|
return (0, create_mitosis_node_1.createMitosisNode)({
|
|
159
160
|
name: 'Show',
|
|
160
161
|
meta: {
|
|
@@ -169,7 +170,9 @@ const jsxElementToJson = (node) => {
|
|
|
169
170
|
else if (types.isJSXFragment(node)) {
|
|
170
171
|
return (0, create_mitosis_node_1.createMitosisNode)({
|
|
171
172
|
name: 'Fragment',
|
|
172
|
-
children: node.children
|
|
173
|
+
children: node.children
|
|
174
|
+
.map((child) => (0, exports.jsxElementToJson)(child, options))
|
|
175
|
+
.filter(nullable_1.checkIsDefined),
|
|
173
176
|
});
|
|
174
177
|
}
|
|
175
178
|
else if (types.isJSXSpreadChild(node)) {
|
|
@@ -209,7 +212,7 @@ const jsxElementToJson = (node) => {
|
|
|
209
212
|
: undefined;
|
|
210
213
|
const elseValue = elseAttr &&
|
|
211
214
|
types.isJSXExpressionContainer(elseAttr.value) &&
|
|
212
|
-
(0, exports.jsxElementToJson)(elseAttr.value.expression);
|
|
215
|
+
(0, exports.jsxElementToJson)(elseAttr.value.expression, options);
|
|
213
216
|
return (0, create_mitosis_node_1.createMitosisNode)({
|
|
214
217
|
name: 'Show',
|
|
215
218
|
meta: {
|
|
@@ -218,7 +221,9 @@ const jsxElementToJson = (node) => {
|
|
|
218
221
|
bindings: {
|
|
219
222
|
...(whenValue ? { when: (0, bindings_1.createSingleBinding)({ code: whenValue }) } : {}),
|
|
220
223
|
},
|
|
221
|
-
children: node.children
|
|
224
|
+
children: node.children
|
|
225
|
+
.map((child) => (0, exports.jsxElementToJson)(child, options))
|
|
226
|
+
.filter(nullable_1.checkIsDefined),
|
|
222
227
|
});
|
|
223
228
|
}
|
|
224
229
|
// <For ...> control flow component
|
|
@@ -245,7 +250,7 @@ const jsxElementToJson = (node) => {
|
|
|
245
250
|
}),
|
|
246
251
|
},
|
|
247
252
|
scope: forArguments,
|
|
248
|
-
children: [(0, exports.jsxElementToJson)(childExpression.body)],
|
|
253
|
+
children: [(0, exports.jsxElementToJson)(childExpression.body, options)],
|
|
249
254
|
});
|
|
250
255
|
}
|
|
251
256
|
}
|
|
@@ -253,7 +258,7 @@ const jsxElementToJson = (node) => {
|
|
|
253
258
|
// const properties: MitosisNode['properties'] = {}
|
|
254
259
|
// const bindings: MitosisNode['bindings'] = {}
|
|
255
260
|
// const slots: MitosisNode['slots'] = {}
|
|
256
|
-
const { bindings, properties, slots } = node.openingElement.attributes.reduce((memo, item) => {
|
|
261
|
+
const { bindings, properties, slots, blocksSlots } = node.openingElement.attributes.reduce((memo, item) => {
|
|
257
262
|
if (types.isJSXAttribute(item)) {
|
|
258
263
|
const key = (0, helpers_1.transformAttributeName)(item.name.name);
|
|
259
264
|
const value = item.value;
|
|
@@ -296,7 +301,7 @@ const jsxElementToJson = (node) => {
|
|
|
296
301
|
else if (types.isJSXElement(expression) || types.isJSXFragment(expression)) {
|
|
297
302
|
// <Foo myProp={<MoreMitosisNode><div /></MoreMitosisNode>} />
|
|
298
303
|
// <Foo myProp={<><Node /><Node /></>} />
|
|
299
|
-
const slotNode = (0, exports.jsxElementToJson)(expression);
|
|
304
|
+
const slotNode = (0, exports.jsxElementToJson)(expression, options);
|
|
300
305
|
if (!slotNode)
|
|
301
306
|
return memo;
|
|
302
307
|
memo.slots[key] = [slotNode];
|
|
@@ -306,6 +311,77 @@ const jsxElementToJson = (node) => {
|
|
|
306
311
|
});
|
|
307
312
|
}
|
|
308
313
|
else {
|
|
314
|
+
if (options.enableBlocksSlots &&
|
|
315
|
+
(types.isArrayExpression(expression) || types.isObjectExpression(expression))) {
|
|
316
|
+
/**
|
|
317
|
+
* Find any deeply nested JSX Elements, convert them to Mitosis nodes
|
|
318
|
+
* then store them in "replacements" to later do a string substitution
|
|
319
|
+
* to swap out the stringified JSX with stringified Mitosis nodes.
|
|
320
|
+
*
|
|
321
|
+
* Object expressions need to wrapped in an expression statement (e.g. `({... })`)
|
|
322
|
+
* otherwise Babel generate will fail.
|
|
323
|
+
*/
|
|
324
|
+
const code = types.isObjectExpression(expression)
|
|
325
|
+
? (0, generator_1.default)(types.expressionStatement(expression)).code
|
|
326
|
+
: (0, generator_1.default)(expression).code;
|
|
327
|
+
const replacements = [];
|
|
328
|
+
(0, helpers_1.babelDefaultTransform)(code, {
|
|
329
|
+
JSXElement(path) {
|
|
330
|
+
const { start, end } = path.node;
|
|
331
|
+
if (start == null || end == null) {
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
const node = (0, exports.jsxElementToJson)(path.node, options);
|
|
335
|
+
if (!node)
|
|
336
|
+
return;
|
|
337
|
+
/**
|
|
338
|
+
* Perform replacements in the reverse order in which we saw them
|
|
339
|
+
* otherwise start/end indices will quickly become incorrect.
|
|
340
|
+
*/
|
|
341
|
+
replacements.unshift({
|
|
342
|
+
start,
|
|
343
|
+
end,
|
|
344
|
+
node,
|
|
345
|
+
});
|
|
346
|
+
/**
|
|
347
|
+
* babelTransform will keep iterating into deeper nodes. However,
|
|
348
|
+
* the "jsxElementToJson" call above will handle deeper nodes.
|
|
349
|
+
* Replace the path will null so we do not accidentally process
|
|
350
|
+
* child nodes multiple times.
|
|
351
|
+
*/
|
|
352
|
+
path.replaceWith(types.nullLiteral());
|
|
353
|
+
},
|
|
354
|
+
});
|
|
355
|
+
// If no replacements then this is just a regular binding
|
|
356
|
+
if (replacements.length > 0) {
|
|
357
|
+
// Replace stringified JSX (e.g. <Foo></Foo>) with stringified Mitosis JSON
|
|
358
|
+
let replacedCode = code;
|
|
359
|
+
replacements.forEach(({ start, end, node }) => {
|
|
360
|
+
replacedCode =
|
|
361
|
+
replacedCode.substring(0, start) +
|
|
362
|
+
JSON.stringify(node) +
|
|
363
|
+
replacedCode.substring(end);
|
|
364
|
+
});
|
|
365
|
+
let finalCode = replacedCode;
|
|
366
|
+
if (types.isObjectExpression(expression)) {
|
|
367
|
+
/**
|
|
368
|
+
* Remove the ( and ); surrounding the expression because we just want
|
|
369
|
+
* a valid JS object instead.
|
|
370
|
+
*/
|
|
371
|
+
const match = replacedCode.match(/\(([\s\S]*?)\);/);
|
|
372
|
+
if (match) {
|
|
373
|
+
finalCode = match[1];
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* The result should be a valid array of objects. Use json5 to parse
|
|
378
|
+
* as not every key will be wrapped in quotes, so a normal JSON.parse
|
|
379
|
+
* will fail.
|
|
380
|
+
*/
|
|
381
|
+
memo.blocksSlots[key] = json5_1.default.parse(finalCode);
|
|
382
|
+
return memo;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
309
385
|
memo.bindings[key] = (0, bindings_1.createSingleBinding)({
|
|
310
386
|
code: (0, generator_1.default)(expression, { compact: true }).code,
|
|
311
387
|
});
|
|
@@ -328,13 +404,15 @@ const jsxElementToJson = (node) => {
|
|
|
328
404
|
bindings: {},
|
|
329
405
|
properties: {},
|
|
330
406
|
slots: {},
|
|
407
|
+
blocksSlots: {},
|
|
331
408
|
});
|
|
332
409
|
return (0, create_mitosis_node_1.createMitosisNode)({
|
|
333
410
|
name: nodeName,
|
|
334
411
|
properties,
|
|
335
412
|
bindings,
|
|
336
|
-
children: node.children.map(exports.jsxElementToJson).filter(nullable_1.checkIsDefined),
|
|
413
|
+
children: node.children.map((child) => (0, exports.jsxElementToJson)(child, options)).filter(nullable_1.checkIsDefined),
|
|
337
414
|
slots: Object.keys(slots).length > 0 ? slots : undefined,
|
|
415
|
+
blocksSlots: Object.keys(blocksSlots).length > 0 ? blocksSlots : undefined,
|
|
338
416
|
});
|
|
339
417
|
};
|
|
340
418
|
exports.jsxElementToJson = jsxElementToJson;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as babel from '@babel/core';
|
|
2
2
|
import { JSONOrNode } from '../../types/json';
|
|
3
|
-
import { Context } from './types';
|
|
3
|
+
import type { Context, ParseMitosisOptions } from './types';
|
|
4
4
|
declare const types: typeof babel.types;
|
|
5
5
|
/**
|
|
6
6
|
* Parses function declarations within the Mitosis copmonent's body to JSON
|
|
7
7
|
*/
|
|
8
|
-
export declare const componentFunctionToJson: (node: babel.types.FunctionDeclaration, context: Context, stateToScope: string[]) => JSONOrNode;
|
|
8
|
+
export declare const componentFunctionToJson: (node: babel.types.FunctionDeclaration, context: Context, stateToScope: string[], options: Partial<ParseMitosisOptions>) => JSONOrNode;
|
|
9
9
|
export {};
|
|
@@ -44,7 +44,7 @@ const { types } = babel;
|
|
|
44
44
|
/**
|
|
45
45
|
* Parses function declarations within the Mitosis copmonent's body to JSON
|
|
46
46
|
*/
|
|
47
|
-
const componentFunctionToJson = (node, context, stateToScope) => {
|
|
47
|
+
const componentFunctionToJson = (node, context, stateToScope, options) => {
|
|
48
48
|
var _a;
|
|
49
49
|
const hooks = {
|
|
50
50
|
onMount: [],
|
|
@@ -327,7 +327,7 @@ const componentFunctionToJson = (node, context, stateToScope) => {
|
|
|
327
327
|
if (theReturn) {
|
|
328
328
|
const value = theReturn.argument;
|
|
329
329
|
if (types.isJSXElement(value) || types.isJSXFragment(value)) {
|
|
330
|
-
const jsxElement = (0, element_parser_1.jsxElementToJson)(value);
|
|
330
|
+
const jsxElement = (0, element_parser_1.jsxElementToJson)(value, options);
|
|
331
331
|
if (jsxElement) {
|
|
332
332
|
children.push(jsxElement);
|
|
333
333
|
}
|
|
@@ -130,7 +130,7 @@ function parseJsx(jsx, _options = {}) {
|
|
|
130
130
|
};
|
|
131
131
|
},
|
|
132
132
|
});
|
|
133
|
-
path.replaceWith((0, ast_1.jsonToAst)((0, function_parser_1.componentFunctionToJson)(node, context, stateToScope)));
|
|
133
|
+
path.replaceWith((0, ast_1.jsonToAst)((0, function_parser_1.componentFunctionToJson)(node, context, stateToScope, options)));
|
|
134
134
|
}
|
|
135
135
|
}
|
|
136
136
|
},
|
|
@@ -142,7 +142,7 @@ function parseJsx(jsx, _options = {}) {
|
|
|
142
142
|
},
|
|
143
143
|
JSXElement(path) {
|
|
144
144
|
const { node } = path;
|
|
145
|
-
path.replaceWith((0, ast_1.jsonToAst)((0, element_parser_1.jsxElementToJson)(node)));
|
|
145
|
+
path.replaceWith((0, ast_1.jsonToAst)((0, element_parser_1.jsxElementToJson)(node, _options)));
|
|
146
146
|
},
|
|
147
147
|
ExportNamedDeclaration(path, context) {
|
|
148
148
|
const { node } = path;
|
|
@@ -8,6 +8,13 @@ export type ParseMitosisOptions = {
|
|
|
8
8
|
project: Project;
|
|
9
9
|
};
|
|
10
10
|
filePath?: string;
|
|
11
|
+
/**
|
|
12
|
+
* When `true`, the `blocksSlots` field on Mitosis Nodes will be used to transform
|
|
13
|
+
* deeply nested JSX elements found on properties. Note that not every generator
|
|
14
|
+
* supports parsing `blocksSlots`.
|
|
15
|
+
* Defaults to `false`.
|
|
16
|
+
*/
|
|
17
|
+
enableBlocksSlots?: boolean;
|
|
11
18
|
};
|
|
12
19
|
export type ResolvedImport = {
|
|
13
20
|
path: string;
|
|
@@ -17,6 +17,7 @@ export declare function parseText(node: TemplateNode): {
|
|
|
17
17
|
localizedValues?: {
|
|
18
18
|
[index: string]: import("../../..").BuilderLocalizedValue;
|
|
19
19
|
} | undefined;
|
|
20
|
+
blocksSlots?: import("../../..").BlockSlot | undefined;
|
|
20
21
|
} | {
|
|
21
22
|
name: string;
|
|
22
23
|
properties: {
|
|
@@ -39,6 +40,7 @@ export declare function parseText(node: TemplateNode): {
|
|
|
39
40
|
localizedValues?: {
|
|
40
41
|
[index: string]: import("../../..").BuilderLocalizedValue;
|
|
41
42
|
} | undefined;
|
|
43
|
+
blocksSlots?: import("../../..").BlockSlot | undefined;
|
|
42
44
|
} | {
|
|
43
45
|
name: string;
|
|
44
46
|
properties: {
|
|
@@ -57,4 +59,5 @@ export declare function parseText(node: TemplateNode): {
|
|
|
57
59
|
localizedValues?: {
|
|
58
60
|
[index: string]: import("../../..").BuilderLocalizedValue;
|
|
59
61
|
} | undefined;
|
|
62
|
+
blocksSlots?: import("../../..").BlockSlot | undefined;
|
|
60
63
|
};
|
|
@@ -80,7 +80,15 @@ export type BaseNode = {
|
|
|
80
80
|
localizedValues?: {
|
|
81
81
|
[index: string]: BuilderLocalizedValue;
|
|
82
82
|
};
|
|
83
|
+
/**
|
|
84
|
+
* Key-value store of serialized elements passed into properties.
|
|
85
|
+
* Disabled by default. Use `enableBlocksSlots` on supported parsers to enable.
|
|
86
|
+
*/
|
|
87
|
+
blocksSlots?: BlockSlot;
|
|
83
88
|
};
|
|
89
|
+
export interface BlockSlot {
|
|
90
|
+
[key: string]: BlockSlot | BlockSlot[] | MitosisNode | MitosisNode[];
|
|
91
|
+
}
|
|
84
92
|
export declare const ForNodeName = "For";
|
|
85
93
|
export declare const FragmentNodeName = "Fragment";
|
|
86
94
|
export declare const ShowNodeName = "Show";
|