@builder.io/mitosis 0.11.5 → 0.12.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.
@@ -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
- const builderBlockPrefixes = ['Amp', 'Core', 'Builder', 'Raw', 'Form'];
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 generator_1 = __importDefault(require("@babel/generator"));
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 (!verifyCode.valid) {
135
+ if (verifyCode.valid) {
136
+ code = processBoundLogic(code);
137
+ }
138
+ else {
133
139
  if (options.escapeInvalidCode) {
134
140
  code = '`' + code + ' [INVALID CODE]`';
135
141
  }
@@ -456,150 +462,29 @@ const componentMappers = {
456
462
  children: (block.children || []).map((child) => (0, exports.builderElementToMitosisNode)(updateBindings(child, 'state.$index', 'index'), options)),
457
463
  });
458
464
  },
459
- Text: (block, options) => {
460
- var _a, _b, _c;
461
- let css = getCssFromBlock(block);
462
- const styleString = (0, exports.getStyleStringFromBlock)(block, options);
463
- const actionBindings = getActionBindingsFromBlock(block, options);
464
- const localizedValues = {};
465
- const blockBindings = {
466
- ...mapBuilderBindingsToMitosisBindingWithCode(block.bindings),
467
- ...mapBuilderBindingsToMitosisBindingWithCode((_a = block.code) === null || _a === void 0 ? void 0 : _a.bindings),
468
- };
469
- const bindings = {
470
- ...(0, lodash_1.omitBy)(blockBindings, (value, key) => {
471
- if (key === 'component.options.text') {
472
- return true;
473
- }
474
- if (key && key.includes('style')) {
475
- return true;
476
- }
477
- return false;
478
- }),
479
- ...actionBindings,
480
- ...(styleString && {
481
- style: { code: styleString },
482
- }),
483
- ...(Object.keys(css).length && {
484
- css: { code: JSON.stringify(css) },
485
- }),
486
- };
487
- const properties = { ...block.properties };
488
- for (const key in properties) {
489
- if (typeof properties[key] === 'object' &&
490
- properties[key] !== null &&
491
- properties[key]['@type'] === '@builder.io/core:LocalizedValue') {
492
- const localizedValue = properties[key];
493
- localizedValues[`properties.${key}`] = localizedValue;
494
- properties[key] = localizedValue.Default;
465
+ };
466
+ const processBoundLogic = (code) => {
467
+ const ast = babel.parse(code);
468
+ if (!ast)
469
+ return code;
470
+ let replacedWithReturn = false;
471
+ (0, traverse_1.default)(ast, {
472
+ ExportDefaultDeclaration(path) {
473
+ const exportedNode = path.node.declaration;
474
+ if (t.isExpression(exportedNode)) {
475
+ const returnStatement = t.returnStatement(exportedNode);
476
+ path.replaceWith(returnStatement);
477
+ replacedWithReturn = true;
495
478
  }
496
- }
497
- if (options.includeBuilderExtras && block.id)
498
- properties['builder-id'] = block.id;
499
- if (block.class)
500
- properties['class'] = block.class;
501
- if (block.layerName) {
502
- properties.$name = block.layerName;
503
- }
504
- // Add data attributes for Builder layer properties
505
- const dataAttributes = {};
506
- if (block.layerLocked !== undefined) {
507
- dataAttributes['data-builder-layerLocked'] = String(block.layerLocked);
508
- }
509
- if (block.groupLocked !== undefined) {
510
- dataAttributes['data-builder-groupLocked'] = String(block.groupLocked);
511
- }
512
- const innerBindings = {};
513
- const componentOptionsText = blockBindings['component.options.text'];
514
- if (componentOptionsText) {
515
- innerBindings[options.preserveTextBlocks ? 'innerHTML' : '_text'] = (0, bindings_1.createSingleBinding)({
516
- code: wrapBindingIfNeeded(componentOptionsText.code, options),
517
- });
518
- }
519
- let text = ((_b = block.component.options) === null || _b === void 0 ? void 0 : _b.text) || '';
520
- if (typeof text === 'object' &&
521
- text !== null &&
522
- text['@type'] === '@builder.io/core:LocalizedValue') {
523
- localizedValues['component.options.text'] = (_c = block.component.options) === null || _c === void 0 ? void 0 : _c.text;
524
- text = text.Default;
525
- }
526
- // Builder uses {{}} for bindings, but Mitosis expects {} so we need to convert
527
- const innerProperties = innerBindings._text
528
- ? {}
529
- : {
530
- [options.preserveTextBlocks ? 'innerHTML' : '_text']: text.replace(/\{\{(.*?)\}\}/g, '{$1}'),
531
- };
532
- if (options.preserveTextBlocks) {
533
- return (0, create_mitosis_node_1.createMitosisNode)({
534
- name: block.tagName || 'div',
535
- bindings,
536
- properties: {
537
- ...properties,
538
- ...dataAttributes,
539
- },
540
- meta: (0, exports.getMetaFromBlock)(block, options),
541
- ...(Object.keys(localizedValues).length && { localizedValues }),
542
- children: [
543
- (0, create_mitosis_node_1.createMitosisNode)({
544
- bindings: innerBindings,
545
- properties: {
546
- ...innerProperties,
547
- class: 'builder-text',
548
- },
549
- ...(Object.keys(localizedValues).length && { localizedValues }),
550
- }),
551
- ],
552
- });
553
- }
554
- // Disabling for now
555
- const assumeLink = false;
556
- const finalProperties = {
557
- ...(assumeLink
558
- ? {
559
- href: '...',
560
- }
561
- : {}),
562
- ...properties,
563
- ...dataAttributes,
564
- };
565
- const finalTagname = block.tagName || (assumeLink ? 'a' : 'div');
566
- if ((block.tagName && block.tagName !== 'div') ||
567
- hasStyles(block) ||
568
- hasComponent(block) ||
569
- hasBindings(block) ||
570
- hasProperties(block)) {
571
- return (0, create_mitosis_node_1.createMitosisNode)({
572
- name: finalTagname,
573
- bindings,
574
- properties: finalProperties,
575
- meta: (0, exports.getMetaFromBlock)(block, options),
576
- children: [
577
- (0, create_mitosis_node_1.createMitosisNode)({
578
- bindings: innerBindings,
579
- properties: innerProperties,
580
- ...(Object.keys(localizedValues).length && { localizedValues }),
581
- }),
582
- ],
583
- });
584
- }
585
- return (0, create_mitosis_node_1.createMitosisNode)({
586
- name: finalTagname,
587
- properties: {
588
- ...finalProperties,
589
- ...properties,
590
- ...innerProperties,
591
- },
592
- bindings: {
593
- ...bindings,
594
- ...innerBindings,
595
- },
596
- meta: (0, exports.getMetaFromBlock)(block, options),
597
- ...(Object.keys(localizedValues).length && { localizedValues }),
598
- });
599
- },
479
+ },
480
+ });
481
+ if (replacedWithReturn) {
482
+ return (0, generator_2.default)(ast).code;
483
+ }
484
+ return code;
600
485
  };
601
486
  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;
487
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
603
488
  const { includeSpecialBindings = true } = options;
604
489
  const localizedValues = {};
605
490
  if (((_a = block.component) === null || _a === void 0 ? void 0 : _a.name) === 'Core:Fragment') {
@@ -700,7 +585,10 @@ const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
700
585
  if (!useKey.includes('.')) {
701
586
  let code = blockBindings[key].code || blockBindings[key];
702
587
  const verifyCode = verifyIsValid(code);
703
- if (!verifyCode.valid) {
588
+ if (verifyCode.valid) {
589
+ code = processBoundLogic(code);
590
+ }
591
+ else {
704
592
  if (options.escapeInvalidCode) {
705
593
  code = '`' + code + ' [INVALID CODE]`';
706
594
  }
@@ -840,8 +728,13 @@ const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
840
728
  if (block.groupLocked !== undefined) {
841
729
  dataAttributes['data-builder-groupLocked'] = String(block.groupLocked);
842
730
  }
731
+ if (((_q = block.component) === null || _q === void 0 ? void 0 : _q.name) &&
732
+ /:/.test((_r = block.component) === null || _r === void 0 ? void 0 : _r.name) &&
733
+ !generator_1.builderBlockPrefixes.includes((_s = block.component) === null || _s === void 0 ? void 0 : _s.name.split(':')[0])) {
734
+ dataAttributes['data-builder-originalName'] = (_t = block.component) === null || _t === void 0 ? void 0 : _t.name;
735
+ }
843
736
  const node = (0, create_mitosis_node_1.createMitosisNode)({
844
- 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, '')) ||
737
+ 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
738
  block.tagName ||
846
739
  (block.linkUrl ? 'a' : 'div'),
847
740
  properties: {
@@ -865,36 +758,11 @@ const builderElementToMitosisNode = (block, options, _internalOptions = {}) => {
865
758
  ...slots,
866
759
  },
867
760
  ...(Object.keys(blocksSlots).length > 0 && { blocksSlots }),
868
- meta: (0, exports.getMetaFromBlock)(block, options),
761
+ meta: {
762
+ ...(0, exports.getMetaFromBlock)(block, options),
763
+ },
869
764
  ...(Object.keys(localizedValues).length && { localizedValues }),
870
765
  });
871
- // Has single text node child
872
- const firstChild = (_s = block.children) === null || _s === void 0 ? void 0 : _s[0];
873
- if (((_t = block.children) === null || _t === void 0 ? void 0 : _t.length) === 1 &&
874
- ((_u = firstChild === null || firstChild === void 0 ? void 0 : firstChild.component) === null || _u === void 0 ? void 0 : _u.name) === 'Text' &&
875
- !options.preserveTextBlocks) {
876
- const textProperties = (0, exports.builderElementToMitosisNode)(firstChild, options);
877
- const parsedNodeCss = json5_1.default.parse(((_v = node.bindings.css) === null || _v === void 0 ? void 0 : _v.code) || '{}');
878
- const parsedTextCss = json5_1.default.parse(((_w = textProperties.bindings.css) === null || _w === void 0 ? void 0 : _w.code) || '{}');
879
- const mergedCss = combineStyles(parsedNodeCss, parsedTextCss);
880
- // Don't merge if text has styling that matters
881
- const doNotMerge =
882
- // Text has flex alignment
883
- ['end', 'right', 'center'].includes(parsedTextCss.alignSelf) ||
884
- // Text has specific styling
885
- parsedTextCss.backgroundColor ||
886
- parsedTextCss.opacity ||
887
- parsedTextCss.background;
888
- if (!doNotMerge) {
889
- return (0, lodash_1.merge)({}, textProperties, node, {
890
- bindings: {
891
- ...(Object.keys(mergedCss).length && {
892
- css: { code: json5_1.default.stringify(mergedCss) },
893
- }),
894
- },
895
- });
896
- }
897
- }
898
766
  node.children = children.concat((block.children || []).map((item) => (0, exports.builderElementToMitosisNode)(item, options)));
899
767
  return node;
900
768
  };
@@ -977,7 +845,7 @@ function extractStateHook(code) {
977
845
  }
978
846
  }
979
847
  }
980
- const newCode = (0, generator_1.default)(types.program(newBody)).code || '';
848
+ const newCode = (0, generator_2.default)(types.program(newBody)).code || '';
981
849
  return { code: newCode, state };
982
850
  }
983
851
  exports.extractStateHook = extractStateHook;
@@ -1025,7 +893,7 @@ function convertExportDefaultToReturn(code) {
1025
893
  }
1026
894
  }
1027
895
  }
1028
- return (0, generator_1.default)(types.program(newBody)).code || '';
896
+ return (0, generator_2.default)(types.program(newBody)).code || '';
1029
897
  }
1030
898
  catch (e) {
1031
899
  const error = e;
@@ -1203,20 +1071,35 @@ const builderContentToMitosisComponent = (builderContent, options = {}) => {
1203
1071
  return componentJson;
1204
1072
  };
1205
1073
  exports.builderContentToMitosisComponent = builderContentToMitosisComponent;
1206
- function mapBuilderBindingsToMitosisBindingWithCode(bindings) {
1074
+ function mapBuilderBindingsToMitosisBindingWithCode(bindings, options) {
1207
1075
  const result = {};
1208
1076
  bindings &&
1209
1077
  Object.keys(bindings).forEach((key) => {
1210
1078
  const value = bindings[key];
1079
+ let code = '';
1211
1080
  if (typeof value === 'string') {
1212
- result[key] = (0, bindings_1.createSingleBinding)({ code: value });
1081
+ code = value;
1213
1082
  }
1214
1083
  else if (value && typeof value === 'object' && value.code) {
1215
- result[key] = (0, bindings_1.createSingleBinding)({ code: value.code });
1084
+ code = value.code;
1216
1085
  }
1217
1086
  else {
1218
1087
  throw new Error('Unexpected binding value: ' + JSON.stringify(value));
1219
1088
  }
1089
+ const verifyCode = verifyIsValid(code);
1090
+ if (verifyCode.valid) {
1091
+ code = processBoundLogic(code);
1092
+ }
1093
+ else {
1094
+ if (options === null || options === void 0 ? void 0 : options.escapeInvalidCode) {
1095
+ code = '`' + code + ' [INVALID CODE]`';
1096
+ }
1097
+ else {
1098
+ console.warn(`Dropping binding "${key}" due to invalid code: ${code}`);
1099
+ return;
1100
+ }
1101
+ }
1102
+ result[key] = (0, bindings_1.createSingleBinding)({ code });
1220
1103
  });
1221
1104
  return result;
1222
1105
  }
@@ -492,6 +492,52 @@ exports.components = {
492
492
  });
493
493
  return wrapOutput(node, videoContainer, components);
494
494
  },
495
+ Text: (node) => {
496
+ if (node.properties.text) {
497
+ node.properties._text = node.properties.text;
498
+ delete node.properties.text;
499
+ }
500
+ if (node.bindings.text) {
501
+ node.bindings._text = node.bindings.text;
502
+ delete node.bindings.text;
503
+ }
504
+ const { _text: propText, $tagName: nodeTagName, ...outerProps } = node.properties;
505
+ const { _text: bindingText, ...outerBindings } = node.bindings;
506
+ const hasBindings = Object.keys(outerProps).length > 0;
507
+ const hasProperties = Object.keys(outerBindings).length > 0;
508
+ /**
509
+ * If there are things we need to reflect on the text then we must
510
+ * render a wrapper div so we can put it on that element.
511
+ */
512
+ if (hasBindings || hasProperties) {
513
+ /**
514
+ * Text binding needs to be on the inner text node. Some generators
515
+ * will skip rendering other bindings if they see a _text binding,
516
+ * so the _text binding needs to go on the inner text node, and all
517
+ * other bindings need to go on the wrapper div so they get generated.
518
+ */
519
+ return (0, create_mitosis_node_1.createMitosisNode)({
520
+ name: 'div',
521
+ bindings: outerBindings,
522
+ properties: outerProps,
523
+ children: [
524
+ (0, create_mitosis_node_1.createMitosisNode)({
525
+ ...node,
526
+ properties: {
527
+ $tagName: nodeTagName,
528
+ ...(propText ? { _text: propText } : {}),
529
+ },
530
+ bindings: bindingText ? { _text: bindingText } : {},
531
+ name: nodeTagName !== null && nodeTagName !== void 0 ? nodeTagName : 'div',
532
+ }),
533
+ ],
534
+ });
535
+ }
536
+ return (0, create_mitosis_node_1.createMitosisNode)({
537
+ ...node,
538
+ name: nodeTagName !== null && nodeTagName !== void 0 ? nodeTagName : 'div',
539
+ });
540
+ },
495
541
  };
496
542
  const compileAwayBuilderComponentsFromTree = (tree, components) => {
497
543
  (0, legacy_1.default)(tree).forEach(function (item) {
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "name": "Builder.io",
23
23
  "url": "https://www.builder.io"
24
24
  },
25
- "version": "0.11.5",
25
+ "version": "0.12.0",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {