@builder.io/mitosis 0.11.5 → 0.11.6
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.
|
@@ -2,6 +2,7 @@ import { ToBuilderOptions } from '../../generators/builder/types';
|
|
|
2
2
|
import { MitosisNode } from '../../types/mitosis-node';
|
|
3
3
|
import { TranspilerArgs } from '../../types/transpiler';
|
|
4
4
|
import { BuilderContent, BuilderElement } from '@builder.io/sdk';
|
|
5
|
+
export declare const builderBlockPrefixes: string[];
|
|
5
6
|
type InternalOptions = {
|
|
6
7
|
skipMapper?: boolean;
|
|
7
8
|
includeStateMap?: boolean;
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.componentToBuilder = exports.blockToBuilder = void 0;
|
|
6
|
+
exports.componentToBuilder = exports.blockToBuilder = exports.builderBlockPrefixes = void 0;
|
|
7
7
|
const media_sizes_1 = require("../../constants/media-sizes");
|
|
8
8
|
const dedent_1 = require("../../helpers/dedent");
|
|
9
9
|
const fast_clone_1 = require("../../helpers/fast-clone");
|
|
@@ -114,12 +114,15 @@ const findStateWithinMitosisComponent = (component, options, state, stateMap) =>
|
|
|
114
114
|
return state;
|
|
115
115
|
};
|
|
116
116
|
const omitMetaProperties = (obj) => (0, lodash_1.omitBy)(obj, (_value, key) => key.startsWith('$'));
|
|
117
|
-
|
|
118
|
-
const mapComponentName = (name) => {
|
|
117
|
+
exports.builderBlockPrefixes = ['Amp', 'Core', 'Builder', 'Raw', 'Form'];
|
|
118
|
+
const mapComponentName = (name, properties) => {
|
|
119
|
+
if (properties === null || properties === void 0 ? void 0 : properties['data-builder-originalName']) {
|
|
120
|
+
return properties['data-builder-originalName'];
|
|
121
|
+
}
|
|
119
122
|
if (name === 'CustomCode') {
|
|
120
123
|
return 'Custom Code';
|
|
121
124
|
}
|
|
122
|
-
for (const prefix of builderBlockPrefixes) {
|
|
125
|
+
for (const prefix of exports.builderBlockPrefixes) {
|
|
123
126
|
if (name.startsWith(prefix)) {
|
|
124
127
|
const suffix = name.replace(prefix, '');
|
|
125
128
|
const restOfName = suffix[0];
|
|
@@ -680,8 +683,8 @@ const blockToBuilder = (json, options = {}, _internalOptions = {}) => {
|
|
|
680
683
|
}),
|
|
681
684
|
...(thisIsComponent && {
|
|
682
685
|
component: {
|
|
683
|
-
name: mapComponentName(json.name),
|
|
684
|
-
options: componentOptions,
|
|
686
|
+
name: mapComponentName(json.name, json.properties),
|
|
687
|
+
options: (0, lodash_1.omit)(componentOptions, ['data-builder-originalName']),
|
|
685
688
|
},
|
|
686
689
|
}),
|
|
687
690
|
code: {
|
|
@@ -747,7 +750,6 @@ const componentToBuilder = (options = {}) => ({ component }) => {
|
|
|
747
750
|
cssCode: component === null || component === void 0 ? void 0 : component.style,
|
|
748
751
|
...(() => {
|
|
749
752
|
const stateData = findStateWithinMitosisComponent(component, options, { ...convertMitosisStateToBuilderState(component.state) }, options.stateMap);
|
|
750
|
-
console.log('stateData', stateData);
|
|
751
753
|
return { state: stateData };
|
|
752
754
|
})(),
|
|
753
755
|
blocks: component.children
|
|
@@ -27,9 +27,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.builderContentToMitosisComponent = exports.isBuilderElement = exports.createBuilderElement = exports.convertExportDefaultToReturn = exports.extractMitosisStateFromBuilderState = exports.extractStateHook = exports.getMetaFromBlock = exports.builderElementToMitosisNode = exports.symbolBlocksAsChildren = exports.getStyleStringFromBlock = void 0;
|
|
30
|
+
const generator_1 = require("../../generators/builder/generator");
|
|
30
31
|
const symbol_processor_1 = require("../../symbols/symbol-processor");
|
|
31
32
|
const babel = __importStar(require("@babel/core"));
|
|
32
|
-
const
|
|
33
|
+
const generator_2 = __importDefault(require("@babel/generator"));
|
|
34
|
+
const traverse_1 = __importDefault(require("@babel/traverse"));
|
|
35
|
+
const t = __importStar(require("@babel/types"));
|
|
33
36
|
const json5_1 = __importDefault(require("json5"));
|
|
34
37
|
const lodash_1 = require("lodash");
|
|
35
38
|
const legacy_1 = __importDefault(require("neotraverse/legacy"));
|
|
@@ -129,7 +132,10 @@ const getStyleStringFromBlock = (block, options) => {
|
|
|
129
132
|
}
|
|
130
133
|
let code = ((_b = (_a = block.code) === null || _a === void 0 ? void 0 : _a.bindings) === null || _b === void 0 ? void 0 : _b[key]) || block.bindings[key];
|
|
131
134
|
const verifyCode = verifyIsValid(code);
|
|
132
|
-
if (
|
|
135
|
+
if (verifyCode.valid) {
|
|
136
|
+
code = processBoundLogic(code);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
133
139
|
if (options.escapeInvalidCode) {
|
|
134
140
|
code = '`' + code + ' [INVALID CODE]`';
|
|
135
141
|
}
|
|
@@ -463,8 +469,8 @@ const componentMappers = {
|
|
|
463
469
|
const actionBindings = getActionBindingsFromBlock(block, options);
|
|
464
470
|
const localizedValues = {};
|
|
465
471
|
const blockBindings = {
|
|
466
|
-
...mapBuilderBindingsToMitosisBindingWithCode(block.bindings),
|
|
467
|
-
...mapBuilderBindingsToMitosisBindingWithCode((_a = block.code) === null || _a === void 0 ? void 0 : _a.bindings),
|
|
472
|
+
...mapBuilderBindingsToMitosisBindingWithCode(block.bindings, options),
|
|
473
|
+
...mapBuilderBindingsToMitosisBindingWithCode((_a = block.code) === null || _a === void 0 ? void 0 : _a.bindings, options),
|
|
468
474
|
};
|
|
469
475
|
const bindings = {
|
|
470
476
|
...(0, lodash_1.omitBy)(blockBindings, (value, key) => {
|
|
@@ -598,8 +604,28 @@ const componentMappers = {
|
|
|
598
604
|
});
|
|
599
605
|
},
|
|
600
606
|
};
|
|
607
|
+
const processBoundLogic = (code) => {
|
|
608
|
+
const ast = babel.parse(code);
|
|
609
|
+
if (!ast)
|
|
610
|
+
return code;
|
|
611
|
+
let replacedWithReturn = false;
|
|
612
|
+
(0, traverse_1.default)(ast, {
|
|
613
|
+
ExportDefaultDeclaration(path) {
|
|
614
|
+
const exportedNode = path.node.declaration;
|
|
615
|
+
if (t.isExpression(exportedNode)) {
|
|
616
|
+
const returnStatement = t.returnStatement(exportedNode);
|
|
617
|
+
path.replaceWith(returnStatement);
|
|
618
|
+
replacedWithReturn = true;
|
|
619
|
+
}
|
|
620
|
+
},
|
|
621
|
+
});
|
|
622
|
+
if (replacedWithReturn) {
|
|
623
|
+
return (0, generator_2.default)(ast).code;
|
|
624
|
+
}
|
|
625
|
+
return code;
|
|
626
|
+
};
|
|
601
627
|
const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
|
|
602
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w;
|
|
628
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0;
|
|
603
629
|
const { includeSpecialBindings = true } = options;
|
|
604
630
|
const localizedValues = {};
|
|
605
631
|
if (((_a = block.component) === null || _a === void 0 ? void 0 : _a.name) === 'Core:Fragment') {
|
|
@@ -700,7 +726,10 @@ const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
|
|
|
700
726
|
if (!useKey.includes('.')) {
|
|
701
727
|
let code = blockBindings[key].code || blockBindings[key];
|
|
702
728
|
const verifyCode = verifyIsValid(code);
|
|
703
|
-
if (
|
|
729
|
+
if (verifyCode.valid) {
|
|
730
|
+
code = processBoundLogic(code);
|
|
731
|
+
}
|
|
732
|
+
else {
|
|
704
733
|
if (options.escapeInvalidCode) {
|
|
705
734
|
code = '`' + code + ' [INVALID CODE]`';
|
|
706
735
|
}
|
|
@@ -840,8 +869,13 @@ const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
|
|
|
840
869
|
if (block.groupLocked !== undefined) {
|
|
841
870
|
dataAttributes['data-builder-groupLocked'] = String(block.groupLocked);
|
|
842
871
|
}
|
|
872
|
+
if (((_q = block.component) === null || _q === void 0 ? void 0 : _q.name) &&
|
|
873
|
+
/:/.test((_r = block.component) === null || _r === void 0 ? void 0 : _r.name) &&
|
|
874
|
+
!generator_1.builderBlockPrefixes.includes((_s = block.component) === null || _s === void 0 ? void 0 : _s.name.split(':')[0])) {
|
|
875
|
+
dataAttributes['data-builder-originalName'] = (_t = block.component) === null || _t === void 0 ? void 0 : _t.name;
|
|
876
|
+
}
|
|
843
877
|
const node = (0, create_mitosis_node_1.createMitosisNode)({
|
|
844
|
-
name: ((
|
|
878
|
+
name: ((_v = (_u = block.component) === null || _u === void 0 ? void 0 : _u.name) === null || _v === void 0 ? void 0 : _v.replace(/[^a-z0-9]/gi, '')) ||
|
|
845
879
|
block.tagName ||
|
|
846
880
|
(block.linkUrl ? 'a' : 'div'),
|
|
847
881
|
properties: {
|
|
@@ -865,17 +899,19 @@ const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
|
|
|
865
899
|
...slots,
|
|
866
900
|
},
|
|
867
901
|
...(Object.keys(blocksSlots).length > 0 && { blocksSlots }),
|
|
868
|
-
meta:
|
|
902
|
+
meta: {
|
|
903
|
+
...(0, exports.getMetaFromBlock)(block, options),
|
|
904
|
+
},
|
|
869
905
|
...(Object.keys(localizedValues).length && { localizedValues }),
|
|
870
906
|
});
|
|
871
907
|
// Has single text node child
|
|
872
|
-
const firstChild = (
|
|
873
|
-
if (((
|
|
874
|
-
((
|
|
908
|
+
const firstChild = (_w = block.children) === null || _w === void 0 ? void 0 : _w[0];
|
|
909
|
+
if (((_x = block.children) === null || _x === void 0 ? void 0 : _x.length) === 1 &&
|
|
910
|
+
((_y = firstChild === null || firstChild === void 0 ? void 0 : firstChild.component) === null || _y === void 0 ? void 0 : _y.name) === 'Text' &&
|
|
875
911
|
!options.preserveTextBlocks) {
|
|
876
912
|
const textProperties = (0, exports.builderElementToMitosisNode)(firstChild, options);
|
|
877
|
-
const parsedNodeCss = json5_1.default.parse(((
|
|
878
|
-
const parsedTextCss = json5_1.default.parse(((
|
|
913
|
+
const parsedNodeCss = json5_1.default.parse(((_z = node.bindings.css) === null || _z === void 0 ? void 0 : _z.code) || '{}');
|
|
914
|
+
const parsedTextCss = json5_1.default.parse(((_0 = textProperties.bindings.css) === null || _0 === void 0 ? void 0 : _0.code) || '{}');
|
|
879
915
|
const mergedCss = combineStyles(parsedNodeCss, parsedTextCss);
|
|
880
916
|
// Don't merge if text has styling that matters
|
|
881
917
|
const doNotMerge =
|
|
@@ -977,7 +1013,7 @@ function extractStateHook(code) {
|
|
|
977
1013
|
}
|
|
978
1014
|
}
|
|
979
1015
|
}
|
|
980
|
-
const newCode = (0,
|
|
1016
|
+
const newCode = (0, generator_2.default)(types.program(newBody)).code || '';
|
|
981
1017
|
return { code: newCode, state };
|
|
982
1018
|
}
|
|
983
1019
|
exports.extractStateHook = extractStateHook;
|
|
@@ -1025,7 +1061,7 @@ function convertExportDefaultToReturn(code) {
|
|
|
1025
1061
|
}
|
|
1026
1062
|
}
|
|
1027
1063
|
}
|
|
1028
|
-
return (0,
|
|
1064
|
+
return (0, generator_2.default)(types.program(newBody)).code || '';
|
|
1029
1065
|
}
|
|
1030
1066
|
catch (e) {
|
|
1031
1067
|
const error = e;
|
|
@@ -1203,20 +1239,35 @@ const builderContentToMitosisComponent = (builderContent, options = {}) => {
|
|
|
1203
1239
|
return componentJson;
|
|
1204
1240
|
};
|
|
1205
1241
|
exports.builderContentToMitosisComponent = builderContentToMitosisComponent;
|
|
1206
|
-
function mapBuilderBindingsToMitosisBindingWithCode(bindings) {
|
|
1242
|
+
function mapBuilderBindingsToMitosisBindingWithCode(bindings, options) {
|
|
1207
1243
|
const result = {};
|
|
1208
1244
|
bindings &&
|
|
1209
1245
|
Object.keys(bindings).forEach((key) => {
|
|
1210
1246
|
const value = bindings[key];
|
|
1247
|
+
let code = '';
|
|
1211
1248
|
if (typeof value === 'string') {
|
|
1212
|
-
|
|
1249
|
+
code = value;
|
|
1213
1250
|
}
|
|
1214
1251
|
else if (value && typeof value === 'object' && value.code) {
|
|
1215
|
-
|
|
1252
|
+
code = value.code;
|
|
1216
1253
|
}
|
|
1217
1254
|
else {
|
|
1218
1255
|
throw new Error('Unexpected binding value: ' + JSON.stringify(value));
|
|
1219
1256
|
}
|
|
1257
|
+
const verifyCode = verifyIsValid(code);
|
|
1258
|
+
if (verifyCode.valid) {
|
|
1259
|
+
code = processBoundLogic(code);
|
|
1260
|
+
}
|
|
1261
|
+
else {
|
|
1262
|
+
if (options === null || options === void 0 ? void 0 : options.escapeInvalidCode) {
|
|
1263
|
+
code = '`' + code + ' [INVALID CODE]`';
|
|
1264
|
+
}
|
|
1265
|
+
else {
|
|
1266
|
+
console.warn(`Dropping binding "${key}" due to invalid code: ${code}`);
|
|
1267
|
+
return;
|
|
1268
|
+
}
|
|
1269
|
+
}
|
|
1270
|
+
result[key] = (0, bindings_1.createSingleBinding)({ code });
|
|
1220
1271
|
});
|
|
1221
1272
|
return result;
|
|
1222
1273
|
}
|