@builder.io/mitosis 0.5.9 → 0.5.11

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.
@@ -281,10 +281,10 @@ const blockToAngular = ({ root, json, options = {}, blockOptions = {
281
281
  code: `${trackByFnName}(${indexName !== null && indexName !== void 0 ? indexName : '_'}, ${forName}) { return ${code}; }`,
282
282
  type: 'method',
283
283
  };
284
- str += `<ng-container *ngFor="let ${forName} of ${(_e = json.bindings.each) === null || _e === void 0 ? void 0 : _e.code}${indexName ? `; let ${indexName} = index` : ''}; trackBy: ${trackByFnName}">`;
284
+ str += `<ng-container *ngFor="let ${forName !== null && forName !== void 0 ? forName : '_'} of ${(_e = json.bindings.each) === null || _e === void 0 ? void 0 : _e.code}${indexName ? `; index as ${indexName}` : ''}; trackBy: ${trackByFnName}">`;
285
285
  }
286
286
  else {
287
- str += `<ng-container *ngFor="let ${forName} of ${(_f = json.bindings.each) === null || _f === void 0 ? void 0 : _f.code}${indexName ? `; let ${indexName} = index` : ''}">`;
287
+ str += `<ng-container *ngFor="let ${forName !== null && forName !== void 0 ? forName : '_'} of ${(_f = json.bindings.each) === null || _f === void 0 ? void 0 : _f.code}${indexName ? `; index as ${indexName}` : ''}">`;
288
288
  }
289
289
  str += json.children
290
290
  .map((item) => (0, exports.blockToAngular)({ root, json: item, options, blockOptions }))
@@ -825,7 +825,7 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
825
825
  ${Array.from(refsForObjSpread)
826
826
  .map((refName) => `@ViewChild('${refName}') ${refName}${options.typescript ? '!: ElementRef' : ''}`)
827
827
  .join('\n')}
828
-
828
+
829
829
  ${Array.from(dynamicComponents)
830
830
  .map((component) => `@ViewChild('${component
831
831
  .split('.')[1]
@@ -126,17 +126,19 @@ const componentMappers = {
126
126
  },
127
127
  For(_node, options) {
128
128
  var _a;
129
+ const node = _node;
129
130
  // rename `index` var to `state.$index`
131
+ const target = node.scope.indexName || 'index';
130
132
  const replaceIndex = (node) => {
131
133
  (0, legacy_1.default)(node).forEach(function (thing) {
132
134
  if ((0, is_mitosis_node_1.isMitosisNode)(thing)) {
133
135
  for (const [key, value] of Object.entries(thing.bindings)) {
134
- if (value === null || value === void 0 ? void 0 : value.code.includes('index')) {
136
+ if (value === null || value === void 0 ? void 0 : value.code.includes(target)) {
135
137
  thing.bindings[key].code = (0, replace_identifiers_1.replaceNodes)({
136
138
  code: value.code,
137
139
  nodeMaps: [
138
140
  {
139
- from: core_1.types.identifier('index'),
141
+ from: core_1.types.identifier(target),
140
142
  to: core_1.types.memberExpression(core_1.types.identifier('state'), core_1.types.identifier('$index')),
141
143
  },
142
144
  ],
@@ -147,7 +149,6 @@ const componentMappers = {
147
149
  });
148
150
  return node;
149
151
  };
150
- const node = _node;
151
152
  return el({
152
153
  component: {
153
154
  name: 'Core:Fragment',
@@ -36,7 +36,7 @@ const getCustomTagName = (name, options) => {
36
36
  return kebabCaseName;
37
37
  };
38
38
  const blockToLit = (json, options = {}) => {
39
- var _a, _b, _c, _d;
39
+ var _a, _b, _c, _d, _e, _f;
40
40
  if (json.properties._text) {
41
41
  return json.properties._text;
42
42
  }
@@ -44,7 +44,7 @@ const blockToLit = (json, options = {}) => {
44
44
  return `\${${processBinding((_b = json.bindings) === null || _b === void 0 ? void 0 : _b._text.code)}}`;
45
45
  }
46
46
  if ((0, mitosis_node_1.checkIsForNode)(json)) {
47
- return `\${${processBinding((_c = json.bindings.each) === null || _c === void 0 ? void 0 : _c.code)}?.map((${json.scope.forName}, index) => (
47
+ return `\${${processBinding((_c = json.bindings.each) === null || _c === void 0 ? void 0 : _c.code)}?.map((${(_d = json.scope.forName) !== null && _d !== void 0 ? _d : '_'}, ${(_e = json.scope.indexName) !== null && _e !== void 0 ? _e : 'index'}) => (
48
48
  html\`${json.children
49
49
  .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
50
50
  .map((item) => blockToLit(item, options))
@@ -52,7 +52,7 @@ const blockToLit = (json, options = {}) => {
52
52
  ))}`;
53
53
  }
54
54
  else if (json.name === 'Show') {
55
- return `\${${processBinding((_d = json.bindings.when) === null || _d === void 0 ? void 0 : _d.code)} ?
55
+ return `\${${processBinding((_f = json.bindings.when) === null || _f === void 0 ? void 0 : _f.code)} ?
56
56
  html\`${json.children
57
57
  .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
58
58
  .map((item) => blockToLit(item, options))
@@ -219,15 +219,15 @@ const componentToLit = (_options = {}) => ({ component }) => {
219
219
  ${(0, lodash_1.camelCase)(refName)}!: HTMLElement;
220
220
  `)
221
221
  .join('\n')}
222
-
223
-
222
+
223
+
224
224
  ${Array.from(props)
225
225
  .map((item) => `@property() ${item}: any`)
226
226
  .join('\n')}
227
227
 
228
228
  ${dataString}
229
229
  ${methodsString}
230
-
230
+
231
231
  ${json.hooks.onMount.length === 0
232
232
  ? ''
233
233
  : `connectedCallback() { ${processBinding((0, on_mount_1.stringifySingleScopeOnMount)(json))} }`}
@@ -236,10 +236,10 @@ const componentToLit = (_options = {}) => ({ component }) => {
236
236
  : `disconnectedCallback() { ${processBinding(json.hooks.onUnMount.code)} }`}
237
237
  ${!((_c = json.hooks.onUpdate) === null || _c === void 0 ? void 0 : _c.length)
238
238
  ? ''
239
- : `updated() {
240
- ${json.hooks.onUpdate.map((hook) => processBinding(hook.code)).join('\n\n')}
239
+ : `updated() {
240
+ ${json.hooks.onUpdate.map((hook) => processBinding(hook.code)).join('\n\n')}
241
241
  }`}
242
-
242
+
243
243
  render() {
244
244
  return html\`
245
245
  ${options.useShadowDom || !css.length ? '' : `<style>${css}</style>`}
@@ -69,7 +69,7 @@ const blockToMitosis = (json, toMitosisOptions = {}, component, insideJsx) => {
69
69
  const needsWrapper = json.children.length !== 1;
70
70
  if (options.nativeLoops) {
71
71
  const a = `${insideJsx ? '{' : ''}(${(_b = json.bindings.each) === null || _b === void 0 ? void 0 : _b.code}).map(
72
- (${json.scope.forName}, ${json.scope.indexName || 'index'}) => (
72
+ (${json.scope.forName || '_'}, ${json.scope.indexName || 'index'}) => (
73
73
  ${needsWrapper ? '<>' : ''}
74
74
  ${json.children
75
75
  .map((child) => (0, exports.blockToMitosis)(child, options, component, needsWrapper))
@@ -79,7 +79,7 @@ const blockToMitosis = (json, toMitosisOptions = {}, component, insideJsx) => {
79
79
  return a;
80
80
  }
81
81
  return `<For each={${(_c = json.bindings.each) === null || _c === void 0 ? void 0 : _c.code}}>
82
- {(${json.scope.forName}, ${json.scope.indexName || 'index'}) =>
82
+ {(${json.scope.forName || '_'}, ${json.scope.indexName || 'index'}) =>
83
83
  ${needsWrapper ? '<>' : ''}
84
84
  ${json.children.map((child) => (0, exports.blockToMitosis)(child, options, component, needsWrapper))}}
85
85
  ${needsWrapper ? '</>' : ''}
@@ -243,11 +243,11 @@ const componentToMitosis = (toMitosisOptions = {}) => ({ component }) => {
243
243
 
244
244
  ${json.style ? `useStyle(\`${json.style}\`)` : ''}
245
245
 
246
- return (${addWrapper ? '<>' : ''}
246
+ return ${options.returnArray ? '[' : '('}${addWrapper ? '<>' : ''}
247
247
  ${json.children
248
248
  .map((item) => (0, exports.blockToMitosis)(item, options, component, addWrapper))
249
249
  .join('\n')}
250
- ${addWrapper ? '</>' : ''})
250
+ ${addWrapper ? '</>' : ''}${options.returnArray ? ']' : ')'}
251
251
  }
252
252
 
253
253
  `;
@@ -3,5 +3,6 @@ export interface ToMitosisOptions extends BaseTranspilerOptions {
3
3
  format: 'react' | 'legacy';
4
4
  nativeConditionals?: boolean;
5
5
  nativeLoops?: boolean;
6
+ returnArray?: boolean;
6
7
  }
7
8
  export type MitosisMetadata = {};
@@ -40,10 +40,10 @@ const NODE_MAPPERS = {
40
40
  return childrenStr;
41
41
  },
42
42
  For(_json, options) {
43
- var _a;
43
+ var _a, _b, _c, _d;
44
44
  const json = _json;
45
- const keyValue = json.bindings.key || { code: 'index', type: 'single' };
46
- const forValue = `(${json.scope.forName}, index) in ${(_a = json.bindings.each) === null || _a === void 0 ? void 0 : _a.code}`;
45
+ const keyValue = json.bindings.key || { code: (_a = json.scope.indexName) !== null && _a !== void 0 ? _a : 'index', type: 'single' };
46
+ const forValue = `(${(_b = json.scope.forName) !== null && _b !== void 0 ? _b : '_'}, ${(_c = json.scope.indexName) !== null && _c !== void 0 ? _c : 'index'}) in ${(_d = json.bindings.each) === null || _d === void 0 ? void 0 : _d.code}`;
47
47
  // TODO: tmk key goes on different element (parent vs child) based on Vue 2 vs Vue 3
48
48
  return `<template :key="${(0, helpers_1.encodeQuotes)((keyValue === null || keyValue === void 0 ? void 0 : keyValue.code) || 'index')}" v-for="${(0, helpers_1.encodeQuotes)(forValue)}">
49
49
  ${json.children.map((item) => (0, exports.blockToVue)(item, options)).join('\n')}
@@ -35,6 +35,22 @@ const create_mitosis_node_1 = require("../../helpers/create-mitosis-node");
35
35
  const nullable_1 = require("../../helpers/nullable");
36
36
  const helpers_1 = require("./helpers");
37
37
  const { types } = babel;
38
+ const getBodyExpression = (node) => {
39
+ if (types.isArrowFunctionExpression(node) || types.isFunctionExpression(node)) {
40
+ const callback = node.body;
41
+ if (callback.type === 'BlockStatement') {
42
+ for (const statement of callback.body) {
43
+ if (statement.type === 'ReturnStatement') {
44
+ return statement.argument;
45
+ }
46
+ }
47
+ }
48
+ else {
49
+ return callback;
50
+ }
51
+ }
52
+ return undefined;
53
+ };
38
54
  const getForArguments = (params) => {
39
55
  const [forName, indexName, collectionName] = params
40
56
  .filter((param) => types.isIdentifier(param))
@@ -65,22 +81,53 @@ const jsxElementToJson = (node) => {
65
81
  if (types.isJSXExpressionContainer(node)) {
66
82
  return (0, exports.jsxElementToJson)(node.expression);
67
83
  }
68
- if (types.isCallExpression(node) || types.isOptionalCallExpression(node)) {
69
- const callback = node.arguments[0];
70
- if (types.isArrowFunctionExpression(callback)) {
71
- if (types.isIdentifier(callback.params[0])) {
84
+ if ((types.isCallExpression(node) || types.isOptionalCallExpression(node)) &&
85
+ (node.callee.type === 'MemberExpression' || node.callee.type === 'OptionalMemberExpression')) {
86
+ const isMap = node.callee.property.type === 'Identifier' && node.callee.property.name === 'map';
87
+ const isArrayFrom = node.callee.property.type === 'Identifier' &&
88
+ node.callee.property.name === 'from' &&
89
+ node.callee.object.type === 'Identifier' &&
90
+ node.callee.object.name === 'Array';
91
+ if (isMap) {
92
+ const callback = node.arguments[0];
93
+ const bodyExpression = getBodyExpression(callback);
94
+ if (bodyExpression) {
72
95
  const forArguments = getForArguments(callback.params);
73
96
  return (0, create_mitosis_node_1.createMitosisNode)({
74
97
  name: 'For',
75
98
  bindings: {
76
99
  each: (0, bindings_1.createSingleBinding)({
77
- code: (0, generator_1.default)(node.callee)
78
- .code // Remove .map or potentially ?.map
79
- .replace(/\??\.map$/, ''),
100
+ code: (0, generator_1.default)(node.callee.object, {
101
+ compact: true,
102
+ }).code,
80
103
  }),
81
104
  },
82
105
  scope: forArguments,
83
- children: [(0, exports.jsxElementToJson)(callback.body)],
106
+ children: [(0, exports.jsxElementToJson)(bodyExpression)],
107
+ });
108
+ }
109
+ }
110
+ else if (isArrayFrom) {
111
+ // Array.from
112
+ const each = node.arguments[0];
113
+ const callback = node.arguments[1];
114
+ const bodyExpression = getBodyExpression(callback);
115
+ if (bodyExpression) {
116
+ const forArguments = getForArguments(callback.params);
117
+ return (0, create_mitosis_node_1.createMitosisNode)({
118
+ name: 'For',
119
+ bindings: {
120
+ each: (0, bindings_1.createSingleBinding)({
121
+ code: (0, generator_1.default)({
122
+ ...node,
123
+ arguments: [each],
124
+ }, {
125
+ compact: true,
126
+ }).code,
127
+ }),
128
+ },
129
+ scope: forArguments,
130
+ children: [(0, exports.jsxElementToJson)(bodyExpression)],
84
131
  });
85
132
  }
86
133
  }
@@ -91,7 +138,11 @@ const jsxElementToJson = (node) => {
91
138
  return (0, create_mitosis_node_1.createMitosisNode)({
92
139
  name: 'Show',
93
140
  bindings: {
94
- when: (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(node.left).code }),
141
+ when: (0, bindings_1.createSingleBinding)({
142
+ code: (0, generator_1.default)(node.left, {
143
+ compact: true,
144
+ }).code,
145
+ }),
95
146
  },
96
147
  children: [(0, exports.jsxElementToJson)(node.right)],
97
148
  });
@@ -109,7 +160,7 @@ const jsxElementToJson = (node) => {
109
160
  else: (0, exports.jsxElementToJson)(node.alternate),
110
161
  },
111
162
  bindings: {
112
- when: (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(node.test).code }),
163
+ when: (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(node.test, { compact: true }).code }),
113
164
  },
114
165
  children: child === null ? [] : [child],
115
166
  });
@@ -144,7 +195,7 @@ const jsxElementToJson = (node) => {
144
195
  if (!types.isJSXElement(node)) {
145
196
  return (0, create_mitosis_node_1.createMitosisNode)({
146
197
  bindings: {
147
- _text: (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(node).code }),
198
+ _text: (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(node, { compact: true }).code }),
148
199
  },
149
200
  });
150
201
  }
@@ -153,7 +204,7 @@ const jsxElementToJson = (node) => {
153
204
  const whenAttr = node.openingElement.attributes.find((item) => types.isJSXAttribute(item) && item.name.name === 'when');
154
205
  const elseAttr = node.openingElement.attributes.find((item) => types.isJSXAttribute(item) && item.name.name === 'else');
155
206
  const whenValue = whenAttr && types.isJSXExpressionContainer(whenAttr.value)
156
- ? (0, generator_1.default)(whenAttr.value.expression).code
207
+ ? (0, generator_1.default)(whenAttr.value.expression, { compact: true }).code
157
208
  : undefined;
158
209
  const elseValue = elseAttr &&
159
210
  types.isJSXExpressionContainer(elseAttr.value) &&
@@ -178,7 +229,7 @@ const jsxElementToJson = (node) => {
178
229
  const forArguments = getForArguments(childExpression === null || childExpression === void 0 ? void 0 : childExpression.params);
179
230
  const forCode = (0, function_1.pipe)(node.openingElement.attributes[0], (attr) => {
180
231
  if (types.isJSXAttribute(attr) && types.isJSXExpressionContainer(attr.value)) {
181
- return (0, generator_1.default)(attr.value.expression).code;
232
+ return (0, generator_1.default)(attr.value.expression, { compact: true }).code;
182
233
  }
183
234
  else {
184
235
  // TO-DO: is an empty string valid here?
@@ -228,7 +279,7 @@ const jsxElementToJson = (node) => {
228
279
  ? expression.params.map((node) => node === null || node === void 0 ? void 0 : node.name)
229
280
  : [];
230
281
  memo.bindings[key] = (0, bindings_1.createSingleBinding)({
231
- code: (0, generator_1.default)(expression.body).code,
282
+ code: (0, generator_1.default)(expression.body, { compact: true }).code,
232
283
  arguments: args.length ? args : undefined,
233
284
  });
234
285
  }
@@ -239,10 +290,14 @@ const jsxElementToJson = (node) => {
239
290
  return memo;
240
291
  memo.slots[key] = [slotNode];
241
292
  // Temporarily keep the slot as a binding until we migrate generators to use the slots.
242
- memo.bindings[key] = (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(expression).code });
293
+ memo.bindings[key] = (0, bindings_1.createSingleBinding)({
294
+ code: (0, generator_1.default)(expression, { compact: true }).code,
295
+ });
243
296
  }
244
297
  else {
245
- memo.bindings[key] = (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(expression).code });
298
+ memo.bindings[key] = (0, bindings_1.createSingleBinding)({
299
+ code: (0, generator_1.default)(expression, { compact: true }).code,
300
+ });
246
301
  }
247
302
  return memo;
248
303
  }
@@ -250,9 +305,9 @@ const jsxElementToJson = (node) => {
250
305
  // TODO: potentially like Vue store bindings and properties as array of key value pairs
251
306
  // too so can do this accurately when order matters. Also tempting to not support spread,
252
307
  // as some frameworks do not support it (e.g. Angular) tho Angular may be the only one
253
- const { code: key } = (0, generator_1.default)(item.argument);
308
+ const { code: key } = (0, generator_1.default)(item.argument, { compact: true });
254
309
  memo.bindings[key] = {
255
- code: types.stringLiteral((0, generator_1.default)(item.argument).code).value,
310
+ code: types.stringLiteral((0, generator_1.default)(item.argument, { compact: true }).code).value,
256
311
  type: 'spread',
257
312
  spreadType: 'normal',
258
313
  };
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.5.9",
25
+ "version": "0.5.11",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {