@builder.io/mitosis 0.11.4 → 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,8 +2,10 @@ 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;
8
+ includeStateMap?: boolean;
7
9
  };
8
10
  export declare const blockToBuilder: (json: MitosisNode, options?: ToBuilderOptions, _internalOptions?: InternalOptions) => BuilderElement;
9
11
  export declare const componentToBuilder: (options?: ToBuilderOptions) => ({ component }: TranspilerArgs) => BuilderContent;
@@ -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");
@@ -19,6 +19,7 @@ const replace_identifiers_1 = require("../../helpers/replace-identifiers");
19
19
  const state_1 = require("../../helpers/state");
20
20
  const builder_1 = require("../../parsers/builder");
21
21
  const symbol_processor_1 = require("../../symbols/symbol-processor");
22
+ const mitosis_node_1 = require("../../types/mitosis-node");
22
23
  const core_1 = require("@babel/core");
23
24
  const generator_1 = __importDefault(require("@babel/generator"));
24
25
  const parser_1 = require("@babel/parser");
@@ -27,13 +28,101 @@ const lodash_1 = require("lodash");
27
28
  const legacy_1 = __importDefault(require("neotraverse/legacy"));
28
29
  const standalone_1 = require("prettier/standalone");
29
30
  const on_mount_1 = require("../helpers/on-mount");
31
+ const isValidCollection = (code) => {
32
+ if (!code || typeof code !== 'string') {
33
+ return false;
34
+ }
35
+ // Pattern: Array.from({ length: number })
36
+ // Examples: "Array.from({ length: 10 })", "Array.from({ length: 5 })"
37
+ const arrayFromPattern = /^Array\.from\(\{\s*length\s*:\s*\d+\s*\}\)$/;
38
+ if (arrayFromPattern.test(code)) {
39
+ return false;
40
+ }
41
+ // Pattern: alphanumeric strings separated by dots
42
+ // Examples: "abc.def", "state.list1", "data.items"
43
+ const dotPattern = /^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*$/;
44
+ return dotPattern.test(code);
45
+ };
46
+ const replaceWithStateVariable = (code, stateMap) => {
47
+ if (!code) {
48
+ return '';
49
+ }
50
+ if (stateMap === null || stateMap === void 0 ? void 0 : stateMap.has(code)) {
51
+ return 'state.' + (stateMap.get(code) || '');
52
+ }
53
+ return code;
54
+ };
55
+ const generateUniqueKey = (state) => {
56
+ let newKeyPrefix = 'dataBuilderList';
57
+ let counter = 1;
58
+ while (state[newKeyPrefix + counter]) {
59
+ counter++;
60
+ }
61
+ return newKeyPrefix + counter;
62
+ };
63
+ const convertMitosisStateToBuilderState = (state) => {
64
+ return Object.entries(state).reduce((acc, [key, value]) => {
65
+ if ((value === null || value === void 0 ? void 0 : value.type) === 'property' && (value === null || value === void 0 ? void 0 : value.code)) {
66
+ if (value.code === 'true' || value.code === 'false') {
67
+ acc[key] = value.code === 'true';
68
+ }
69
+ else if (value.code === 'null') {
70
+ acc[key] = null;
71
+ }
72
+ else if (value.code === 'undefined') {
73
+ acc[key] = undefined;
74
+ }
75
+ else if (!Number.isNaN(Number(value.code))) {
76
+ acc[key] = Number(value.code);
77
+ }
78
+ else {
79
+ try {
80
+ acc[key] = JSON.parse(value.code);
81
+ }
82
+ catch (e) {
83
+ acc[key] = value.code;
84
+ }
85
+ }
86
+ }
87
+ return acc;
88
+ }, {});
89
+ };
90
+ const findStateWithinMitosisNode = (node, options, state, stateMap) => {
91
+ var _a, _b, _c;
92
+ if ((0, mitosis_node_1.checkIsForNode)(node)) {
93
+ if (!isValidCollection((_a = node.bindings.each) === null || _a === void 0 ? void 0 : _a.code) &&
94
+ !stateMap.has((_b = node.bindings.each) === null || _b === void 0 ? void 0 : _b.code)) {
95
+ const newKey = generateUniqueKey(state);
96
+ const code = (_c = node.bindings.each) === null || _c === void 0 ? void 0 : _c.code;
97
+ try {
98
+ state[newKey] = JSON.parse(code);
99
+ stateMap.set(code, newKey);
100
+ }
101
+ catch (parseError) {
102
+ // The parsing error happens when `code` is a function or expression
103
+ // We would need `eval` to parse the code and then set the state. But because
104
+ // of security concerns we are not handling this case right now.
105
+ // Will revisit this if we need to support this.
106
+ console.log('Failed to parse:', code, parseError);
107
+ }
108
+ }
109
+ }
110
+ node.children.forEach((child) => findStateWithinMitosisNode(child, options, state, stateMap));
111
+ };
112
+ const findStateWithinMitosisComponent = (component, options, state, stateMap) => {
113
+ component.children.forEach((child) => findStateWithinMitosisNode(child, options, state, stateMap));
114
+ return state;
115
+ };
30
116
  const omitMetaProperties = (obj) => (0, lodash_1.omitBy)(obj, (_value, key) => key.startsWith('$'));
31
- const builderBlockPrefixes = ['Amp', 'Core', 'Builder', 'Raw', 'Form'];
32
- 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
+ }
33
122
  if (name === 'CustomCode') {
34
123
  return 'Custom Code';
35
124
  }
36
- for (const prefix of builderBlockPrefixes) {
125
+ for (const prefix of exports.builderBlockPrefixes) {
37
126
  if (name.startsWith(prefix)) {
38
127
  const suffix = name.replace(prefix, '');
39
128
  const restOfName = suffix[0];
@@ -137,8 +226,9 @@ const componentMappers = {
137
226
  return block;
138
227
  },
139
228
  For(_node, options) {
140
- var _a;
229
+ var _a, _b, _c;
141
230
  const node = _node;
231
+ const stateMap = options.stateMap;
142
232
  const replaceIndexNode = (str) => (0, replace_identifiers_1.replaceNodes)({
143
233
  code: str,
144
234
  nodeMaps: [
@@ -203,7 +293,9 @@ const componentMappers = {
203
293
  name: 'Core:Fragment',
204
294
  },
205
295
  repeat: {
206
- collection: (_a = node.bindings.each) === null || _a === void 0 ? void 0 : _a.code,
296
+ collection: isValidCollection((_a = node.bindings.each) === null || _a === void 0 ? void 0 : _a.code)
297
+ ? ((_b = node.bindings.each) === null || _b === void 0 ? void 0 : _b.code) || ''
298
+ : replaceWithStateVariable((_c = node.bindings.each) === null || _c === void 0 ? void 0 : _c.code, stateMap),
207
299
  itemName: node.scope.forName,
208
300
  },
209
301
  children: node.children
@@ -591,8 +683,8 @@ const blockToBuilder = (json, options = {}, _internalOptions = {}) => {
591
683
  }),
592
684
  ...(thisIsComponent && {
593
685
  component: {
594
- name: mapComponentName(json.name),
595
- options: componentOptions,
686
+ name: mapComponentName(json.name, json.properties),
687
+ options: (0, lodash_1.omit)(componentOptions, ['data-builder-originalName']),
596
688
  },
597
689
  }),
598
690
  code: {
@@ -611,9 +703,29 @@ const blockToBuilder = (json, options = {}, _internalOptions = {}) => {
611
703
  return processLocalizedValues(element, json);
612
704
  };
613
705
  exports.blockToBuilder = blockToBuilder;
706
+ const recursivelyCheckForChildrenWithSameComponent = (elementOrContent, componentName, path = '') => {
707
+ var _a, _b, _c, _d, _e;
708
+ if ((0, builder_1.isBuilderElement)(elementOrContent)) {
709
+ if (((_a = elementOrContent.component) === null || _a === void 0 ? void 0 : _a.name) === componentName) {
710
+ return path;
711
+ }
712
+ return (((_b = elementOrContent.children) === null || _b === void 0 ? void 0 : _b.map((child, index) => recursivelyCheckForChildrenWithSameComponent(child, componentName, `${path}.children[${index}]`)).find(Boolean)) || '');
713
+ }
714
+ if ((_c = elementOrContent.data) === null || _c === void 0 ? void 0 : _c.blocks) {
715
+ return (((_e = (_d = elementOrContent.data) === null || _d === void 0 ? void 0 : _d.blocks) === null || _e === void 0 ? void 0 : _e.map((block, index) => recursivelyCheckForChildrenWithSameComponent(block, componentName, `${path ? `${path}.` : ''}data.blocks[${index}]`)).find(Boolean)) || '');
716
+ }
717
+ return '';
718
+ };
719
+ function removeItem(obj, path, indexToRemove) {
720
+ return (0, lodash_1.set)((0, lodash_1.cloneDeep)(obj), // Clone to ensure immutability
721
+ path, (0, lodash_1.filter)((0, lodash_1.get)(obj, path), (item, index) => index !== indexToRemove));
722
+ }
614
723
  const componentToBuilder = (options = {}) => ({ component }) => {
615
- var _a, _b;
724
+ var _a, _b, _c;
616
725
  const hasState = (0, state_1.checkHasState)(component);
726
+ if (!options.stateMap) {
727
+ options.stateMap = new Map();
728
+ }
617
729
  const result = (0, fast_clone_1.fastClone)({
618
730
  data: {
619
731
  httpRequests: (_b = (_a = component === null || component === void 0 ? void 0 : component.meta) === null || _a === void 0 ? void 0 : _a.useMetadata) === null || _b === void 0 ? void 0 : _b.httpRequests,
@@ -636,11 +748,18 @@ const componentToBuilder = (options = {}) => ({ component }) => {
636
748
  })`}
637
749
  `),
638
750
  cssCode: component === null || component === void 0 ? void 0 : component.style,
751
+ ...(() => {
752
+ const stateData = findStateWithinMitosisComponent(component, options, { ...convertMitosisStateToBuilderState(component.state) }, options.stateMap);
753
+ return { state: stateData };
754
+ })(),
639
755
  blocks: component.children
640
756
  .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
641
757
  .map((child) => (0, exports.blockToBuilder)(child, options)),
642
758
  },
643
759
  });
760
+ if (((_c = result.data) === null || _c === void 0 ? void 0 : _c.state) && Object.keys(result.data.state).length === 0) {
761
+ delete result.data.state;
762
+ }
644
763
  const subComponentMap = {};
645
764
  for (const subComponent of component.subComponents) {
646
765
  const name = subComponent.name;
@@ -649,11 +768,33 @@ const componentToBuilder = (options = {}) => ({ component }) => {
649
768
  });
650
769
  }
651
770
  (0, legacy_1.default)([result, subComponentMap]).forEach(function (el) {
652
- var _a;
653
- if ((0, builder_1.isBuilderElement)(el)) {
654
- const value = subComponentMap[(_a = el.component) === null || _a === void 0 ? void 0 : _a.name];
771
+ var _a, _b, _c, _d, _e;
772
+ if ((0, builder_1.isBuilderElement)(el) && !((_a = el.meta) === null || _a === void 0 ? void 0 : _a.preventRecursion)) {
773
+ const value = subComponentMap[(_b = el.component) === null || _b === void 0 ? void 0 : _b.name];
655
774
  if (value) {
656
- (0, lodash_1.set)(el, 'component.options.symbol.content', value);
775
+ // Recursive Components are handled in the following steps :
776
+ // 1. Find out the path in which the component is self-referenced ( where that component reoccurs within it’s tree ).
777
+ // 2. We populate that component recursively for 4 times in a row.
778
+ // 3. Finally remove the recursive part from the last component which was populated.
779
+ // Also note that it doesn’t mean that component will render that many times, the rendering logic depends on the logic in it's parent. (Eg. show property binding)
780
+ const path = recursivelyCheckForChildrenWithSameComponent(value, (_c = el.component) === null || _c === void 0 ? void 0 : _c.name);
781
+ if (path) {
782
+ let tempElement = el;
783
+ for (let i = 0; i < 4; i++) {
784
+ const tempValue = (0, lodash_1.cloneDeep)(value);
785
+ (0, lodash_1.set)(tempElement, 'component.options.symbol.content', tempValue);
786
+ (0, lodash_1.set)(tempElement, 'meta.preventRecursion', true);
787
+ tempElement = (0, lodash_1.get)(tempValue, path);
788
+ }
789
+ // Finally remove the recursive part.
790
+ const arrayPath = path.replace(/\[\d+\]$/, '');
791
+ const newValue = removeItem(value, arrayPath, Number((_d = path.match(/\[(\d+)\]$/)) === null || _d === void 0 ? void 0 : _d[1]));
792
+ (0, lodash_1.set)(tempElement, 'component.options.symbol.content', newValue);
793
+ (0, lodash_1.set)(tempElement, 'meta.preventRecursion', true);
794
+ }
795
+ else {
796
+ (0, lodash_1.set)(el, 'component.options.symbol.content', value);
797
+ }
657
798
  }
658
799
  if (el.bindings) {
659
800
  for (const [key, value] of Object.entries(el.bindings)) {
@@ -670,6 +811,12 @@ const componentToBuilder = (options = {}) => ({ component }) => {
670
811
  }
671
812
  }
672
813
  }
814
+ if ((0, builder_1.isBuilderElement)(el) && ((_e = el.meta) === null || _e === void 0 ? void 0 : _e.preventRecursion)) {
815
+ delete el.meta.preventRecursion;
816
+ if (el.meta && Object.keys(el.meta).length === 0) {
817
+ delete el.meta;
818
+ }
819
+ }
673
820
  });
674
821
  return result;
675
822
  };
@@ -1,5 +1,6 @@
1
1
  import { BaseTranspilerOptions } from '../../types/transpiler';
2
2
  export interface ToBuilderOptions extends BaseTranspilerOptions {
3
3
  includeIds?: boolean;
4
+ stateMap?: Map<string, string>;
4
5
  }
5
6
  export type BuilderMetadata = {};
@@ -39,6 +39,15 @@ export declare function extractStateHook(code: string): {
39
39
  code: string;
40
40
  state: MitosisState;
41
41
  };
42
+ /**
43
+ * Extracts Mitosis state from Builder state.
44
+ * @param mitosisState Mitosis state to update
45
+ * @param builderState Builder state to extract from
46
+ * @returns
47
+ */
48
+ export declare function extractMitosisStateFromBuilderState(mitosisState: MitosisState, builderState?: {
49
+ [key: string]: any;
50
+ }): void;
42
51
  export declare function convertExportDefaultToReturn(code: string): string;
43
52
  export declare const createBuilderElement: (options?: Partial<BuilderElement>) => BuilderElement;
44
53
  export declare const isBuilderElement: (el: unknown) => el is BuilderElement;
@@ -26,10 +26,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.builderContentToMitosisComponent = exports.isBuilderElement = exports.createBuilderElement = exports.convertExportDefaultToReturn = exports.extractStateHook = exports.getMetaFromBlock = exports.builderElementToMitosisNode = exports.symbolBlocksAsChildren = exports.getStyleStringFromBlock = void 0;
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
  }
@@ -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 (!verifyCode.valid) {
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: ((_r = (_q = block.component) === null || _q === void 0 ? void 0 : _q.name) === null || _r === void 0 ? void 0 : _r.replace(/[^a-z0-9]/gi, '')) ||
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: (0, exports.getMetaFromBlock)(block, options),
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 = (_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' &&
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(((_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) || '{}');
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,10 +1013,38 @@ function extractStateHook(code) {
977
1013
  }
978
1014
  }
979
1015
  }
980
- const newCode = (0, generator_1.default)(types.program(newBody)).code || '';
1016
+ const newCode = (0, generator_2.default)(types.program(newBody)).code || '';
981
1017
  return { code: newCode, state };
982
1018
  }
983
1019
  exports.extractStateHook = extractStateHook;
1020
+ /**
1021
+ * Extracts Mitosis state from Builder state.
1022
+ * @param mitosisState Mitosis state to update
1023
+ * @param builderState Builder state to extract from
1024
+ * @returns
1025
+ */
1026
+ function extractMitosisStateFromBuilderState(mitosisState, builderState) {
1027
+ if (!builderState)
1028
+ return;
1029
+ for (const key in builderState) {
1030
+ let value = builderState[key];
1031
+ if (typeof value === 'function' && !mitosisState[key]) {
1032
+ mitosisState[key] = {
1033
+ type: 'function',
1034
+ code: value.toString(),
1035
+ };
1036
+ continue;
1037
+ }
1038
+ if (!mitosisState[key]) {
1039
+ mitosisState[key] = {
1040
+ type: 'property',
1041
+ propertyType: 'normal',
1042
+ code: JSON.stringify(value),
1043
+ };
1044
+ }
1045
+ }
1046
+ }
1047
+ exports.extractMitosisStateFromBuilderState = extractMitosisStateFromBuilderState;
984
1048
  function convertExportDefaultToReturn(code) {
985
1049
  try {
986
1050
  const { types } = babel;
@@ -997,7 +1061,7 @@ function convertExportDefaultToReturn(code) {
997
1061
  }
998
1062
  }
999
1063
  }
1000
- return (0, generator_1.default)(types.program(newBody)).code || '';
1064
+ return (0, generator_2.default)(types.program(newBody)).code || '';
1001
1065
  }
1002
1066
  catch (e) {
1003
1067
  const error = e;
@@ -1082,7 +1146,7 @@ exports.createBuilderElement = createBuilderElement;
1082
1146
  const isBuilderElement = (el) => (el === null || el === void 0 ? void 0 : el['@type']) === '@builder.io/sdk:Element';
1083
1147
  exports.isBuilderElement = isBuilderElement;
1084
1148
  const builderContentPartToMitosisComponent = (builderContent, options = {}) => {
1085
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
1149
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
1086
1150
  builderContent = (0, fast_clone_1.fastClone)(builderContent);
1087
1151
  (0, legacy_1.default)(builderContent).forEach(function (elem) {
1088
1152
  var _a, _b;
@@ -1126,16 +1190,17 @@ const builderContentPartToMitosisComponent = (builderContent, options = {}) => {
1126
1190
  ...state,
1127
1191
  ...(0, helpers_1.mapBuilderContentStateToMitosisState)(((_c = builderContent.data) === null || _c === void 0 ? void 0 : _c.state) || {}),
1128
1192
  };
1193
+ extractMitosisStateFromBuilderState(mitosisState, (_d = builderContent.data) === null || _d === void 0 ? void 0 : _d.state);
1129
1194
  const componentJson = (0, create_mitosis_component_1.createMitosisComponent)({
1130
1195
  meta: {
1131
1196
  useMetadata: {
1132
- httpRequests: (_d = builderContent.data) === null || _d === void 0 ? void 0 : _d.httpRequests,
1197
+ httpRequests: (_e = builderContent.data) === null || _e === void 0 ? void 0 : _e.httpRequests,
1133
1198
  },
1134
1199
  // cmp.meta.cssCode exists for backwards compatibility, prefer cmp.style
1135
- ...(((_e = builderContent.data) === null || _e === void 0 ? void 0 : _e.cssCode) && { cssCode: builderContent.data.cssCode }),
1200
+ ...(((_f = builderContent.data) === null || _f === void 0 ? void 0 : _f.cssCode) && { cssCode: builderContent.data.cssCode }),
1136
1201
  },
1137
- ...(((_f = builderContent.data) === null || _f === void 0 ? void 0 : _f.cssCode) && { style: (_g = builderContent.data) === null || _g === void 0 ? void 0 : _g.cssCode }),
1138
- inputs: (_j = (_h = builderContent.data) === null || _h === void 0 ? void 0 : _h.inputs) === null || _j === void 0 ? void 0 : _j.map((input) => ({
1202
+ ...(((_g = builderContent.data) === null || _g === void 0 ? void 0 : _g.cssCode) && { style: (_h = builderContent.data) === null || _h === void 0 ? void 0 : _h.cssCode }),
1203
+ inputs: (_k = (_j = builderContent.data) === null || _j === void 0 ? void 0 : _j.inputs) === null || _k === void 0 ? void 0 : _k.map((input) => ({
1139
1204
  name: input.name,
1140
1205
  defaultValue: input.defaultValue,
1141
1206
  })),
@@ -1149,7 +1214,7 @@ const builderContentPartToMitosisComponent = (builderContent, options = {}) => {
1149
1214
  : []),
1150
1215
  ],
1151
1216
  },
1152
- children: (((_k = builderContent.data) === null || _k === void 0 ? void 0 : _k.blocks) || [])
1217
+ children: (((_l = builderContent.data) === null || _l === void 0 ? void 0 : _l.blocks) || [])
1153
1218
  .filter((item) => {
1154
1219
  var _a, _b;
1155
1220
  if ((_b = (_a = item.properties) === null || _a === void 0 ? void 0 : _a.src) === null || _b === void 0 ? void 0 : _b.includes('/api/v1/pixel')) {
@@ -1174,20 +1239,35 @@ const builderContentToMitosisComponent = (builderContent, options = {}) => {
1174
1239
  return componentJson;
1175
1240
  };
1176
1241
  exports.builderContentToMitosisComponent = builderContentToMitosisComponent;
1177
- function mapBuilderBindingsToMitosisBindingWithCode(bindings) {
1242
+ function mapBuilderBindingsToMitosisBindingWithCode(bindings, options) {
1178
1243
  const result = {};
1179
1244
  bindings &&
1180
1245
  Object.keys(bindings).forEach((key) => {
1181
1246
  const value = bindings[key];
1247
+ let code = '';
1182
1248
  if (typeof value === 'string') {
1183
- result[key] = (0, bindings_1.createSingleBinding)({ code: value });
1249
+ code = value;
1184
1250
  }
1185
1251
  else if (value && typeof value === 'object' && value.code) {
1186
- result[key] = (0, bindings_1.createSingleBinding)({ code: value.code });
1252
+ code = value.code;
1187
1253
  }
1188
1254
  else {
1189
1255
  throw new Error('Unexpected binding value: ' + JSON.stringify(value));
1190
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 });
1191
1271
  });
1192
1272
  return result;
1193
1273
  }
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.4",
25
+ "version": "0.11.6",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {