@builder.io/mitosis 0.3.4 → 0.3.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.
@@ -1,4 +1,4 @@
1
- export declare const HELPER_FUNCTIONS: {
1
+ export declare const HELPER_FUNCTIONS: (isTs?: boolean) => {
2
2
  [key: string]: string;
3
3
  };
4
4
  export declare const getAppropriateTemplateFunctionKeys: (code: string) => string[];
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getAppropriateTemplateFunctionKeys = exports.HELPER_FUNCTIONS = void 0;
4
- exports.HELPER_FUNCTIONS = {
5
- useObjectWrapper: "useObjectWrapper(...args: any[]) {\n let obj = {}\n args.forEach((arg) => {\n obj = { ...obj, ...arg };\n });\n return obj;\n }",
6
- useObjectDotValues: "useObjectDotValues(obj: any): any[] {\n return Object.values(obj);\n }",
7
- useTypeOf: "useTypeOf(obj: any): string {\n return typeof obj;\n }",
8
- useJsonStringify: "useJsonStringify(obj: any): string {\n return JSON.stringify(obj);\n }",
9
- };
4
+ var HELPER_FUNCTIONS = function (isTs) { return ({
5
+ useObjectWrapper: "useObjectWrapper(...args".concat(isTs ? ': any[]' : '', ") {\n let obj = {}\n args.forEach((arg) => {\n obj = { ...obj, ...arg };\n });\n return obj;\n }"),
6
+ useObjectDotValues: "useObjectDotValues(obj".concat(isTs ? ': any' : '', ")").concat(isTs ? ': any[]' : '', ") {\n return Object.values(obj);\n }"),
7
+ useTypeOf: "useTypeOf(obj".concat(isTs ? ': any' : '', ")").concat(isTs ? ': string' : '', ") {\n return typeof obj;\n }"),
8
+ useJsonStringify: "useJsonStringify(...args".concat(isTs ? ': any' : '', ")").concat(isTs ? ': string' : '', ") {\n return JSON.stringify(...args);\n }"),
9
+ }); };
10
+ exports.HELPER_FUNCTIONS = HELPER_FUNCTIONS;
10
11
  var getAppropriateTemplateFunctionKeys = function (code) {
11
- return Object.keys(exports.HELPER_FUNCTIONS).filter(function (key) { return code.includes(key); });
12
+ return Object.keys((0, exports.HELPER_FUNCTIONS)()).filter(function (key) { return code.includes(key); });
12
13
  };
13
14
  exports.getAppropriateTemplateFunctionKeys = getAppropriateTemplateFunctionKeys;
@@ -1,5 +1,11 @@
1
+ import { MitosisComponent } from '../../types/mitosis-component';
1
2
  import { MitosisNode } from '../../types/mitosis-node';
2
3
  import { TranspilerGenerator } from '../../types/transpiler';
3
4
  import { AngularBlockOptions, ToAngularOptions } from './types';
4
- export declare const blockToAngular: (json: MitosisNode, options?: ToAngularOptions, blockOptions?: AngularBlockOptions) => string;
5
+ export declare const blockToAngular: ({ root, json, options, blockOptions, }: {
6
+ root: MitosisComponent;
7
+ json: MitosisNode;
8
+ options?: ToAngularOptions | undefined;
9
+ blockOptions?: AngularBlockOptions | undefined;
10
+ }) => string;
5
11
  export declare const componentToAngular: TranspilerGenerator<ToAngularOptions>;
@@ -52,7 +52,9 @@ var strip_meta_properties_1 = require("../../helpers/strip-meta-properties");
52
52
  var strip_state_and_props_refs_1 = require("../../helpers/strip-state-and-props-refs");
53
53
  var collect_css_1 = require("../../helpers/styles/collect-css");
54
54
  var helpers_1 = require("../../helpers/styles/helpers");
55
+ var traverse_nodes_1 = require("../../helpers/traverse-nodes");
55
56
  var plugins_1 = require("../../modules/plugins");
57
+ var symbol_processor_1 = require("../../symbols/symbol-processor");
56
58
  var mitosis_node_1 = require("../../types/mitosis-node");
57
59
  var function_1 = require("fp-ts/lib/function");
58
60
  var lodash_1 = require("lodash");
@@ -63,13 +65,13 @@ var on_mount_1 = require("../helpers/on-mount");
63
65
  var helpers_2 = require("./helpers");
64
66
  var types_1 = require("./types");
65
67
  var mappers = {
66
- Fragment: function (json, options) {
68
+ Fragment: function (root, json, options) {
67
69
  return "<ng-container>".concat(json.children
68
- .map(function (item) { return (0, exports.blockToAngular)(item, options); })
70
+ .map(function (item) { return (0, exports.blockToAngular)({ root: root, json: item, options: options }); })
69
71
  .join('\n'), "</ng-container>");
70
72
  },
71
- Slot: function (json, options) {
72
- var renderChildren = function () { var _a; return (_a = json.children) === null || _a === void 0 ? void 0 : _a.map(function (item) { return (0, exports.blockToAngular)(item, options); }).join('\n'); };
73
+ Slot: function (root, json, options) {
74
+ var renderChildren = function () { var _a; return (_a = json.children) === null || _a === void 0 ? void 0 : _a.map(function (item) { return (0, exports.blockToAngular)({ root: root, json: item, options: options }); }).join('\n'); };
73
75
  return "<ng-content ".concat(Object.entries(__assign(__assign({}, json.bindings), json.properties))
74
76
  .map(function (_a) {
75
77
  var binding = _a[0], value = _a[1];
@@ -152,7 +154,7 @@ var processCodeBlockInTemplate = function (code) {
152
154
  return "useObjectDotValues".concat(stripped);
153
155
  }
154
156
  else if (code.includes('JSON.stringify')) {
155
- var obj = code.match(/JSON.stringify\([^)]*\)/g);
157
+ var obj = code.match(/JSON.stringify\((.*)\)/);
156
158
  return "useJsonStringify(".concat(obj, ")");
157
159
  }
158
160
  else if (code.includes(' as ')) {
@@ -183,13 +185,10 @@ var processEventBinding = function (key, code, nodeName, customArg) {
183
185
  var stringifyBinding = function (node, options, blockOptions) {
184
186
  return function (_a) {
185
187
  var key = _a[0], binding = _a[1];
186
- if ((binding === null || binding === void 0 ? void 0 : binding.type) === 'spread') {
188
+ if (options.state === 'inline-with-wrappers' && (binding === null || binding === void 0 ? void 0 : binding.type) === 'spread') {
187
189
  return;
188
190
  }
189
- if (key.startsWith('$')) {
190
- return;
191
- }
192
- if (key === 'key') {
191
+ if (key.startsWith('$') || key.startsWith('"') || key === 'key') {
193
192
  return;
194
193
  }
195
194
  var keyToUse = BINDINGS_MAPPER[key] || key;
@@ -212,11 +211,13 @@ var stringifyBinding = function (node, options, blockOptions) {
212
211
  return " [attr.".concat(keyToUse, "]=\"").concat(code, "\" ");
213
212
  }
214
213
  else {
215
- return "[".concat(keyToUse, "]=\"").concat(processCodeBlockInTemplate(code), "\"");
214
+ var codeToUse = options.state === 'inline-with-wrappers' ? processCodeBlockInTemplate(code) : code;
215
+ return "[".concat(keyToUse, "]=\"").concat(codeToUse, "\"");
216
216
  }
217
217
  };
218
218
  };
219
- var handleNgOutletBindings = function (node) {
219
+ var handleNgOutletBindings = function (node, options) {
220
+ var _a;
220
221
  var allProps = '';
221
222
  for (var key in node.properties) {
222
223
  if (key.startsWith('$')) {
@@ -235,21 +236,22 @@ var handleNgOutletBindings = function (node) {
235
236
  if (key.startsWith('$')) {
236
237
  continue;
237
238
  }
238
- var _a = node.bindings[key], code = _a.code, _b = _a.arguments, cusArgs = _b === void 0 ? ['event'] : _b;
239
- if (key.includes('props.')) {
240
- allProps += "".concat(key.replace('props.', ''), ": ").concat(code, ", ");
241
- }
242
- else if (key.includes('.')) {
243
- // TODO: handle arbitrary spread props
244
- allProps += "".concat(key.split('.')[1], ": ").concat(code, ", ");
239
+ var _b = node.bindings[key], code = _b.code, _c = _b.arguments, cusArgs = _c === void 0 ? ['event'] : _c;
240
+ if (options.state === 'class-properties') {
241
+ code = "this.".concat(code);
242
+ if (((_a = node.bindings[key]) === null || _a === void 0 ? void 0 : _a.type) === 'spread') {
243
+ allProps += "...".concat(code, ", ");
244
+ continue;
245
+ }
245
246
  }
246
- else if (key.startsWith('on')) {
247
- var _c = processEventBinding(key, code, node.name, cusArgs[0]), event_2 = _c.event, value = _c.value;
248
- allProps += "on".concat(event_2.charAt(0).toUpperCase() + event_2.slice(1), ": ").concat(value.replace(/\(.*?\)/g, ''), ", ");
247
+ var keyToUse = key.includes('-') ? "'".concat(key, "'") : key;
248
+ keyToUse = keyToUse.replace('state.', '').replace('props.', '');
249
+ if (key.startsWith('on')) {
250
+ var _d = processEventBinding(key, code, node.name, cusArgs[0]), event_2 = _d.event, value = _d.value;
251
+ allProps += "on".concat(event_2.charAt(0).toUpperCase() + event_2.slice(1), ": ").concat(value.replace(/\(.*?\)/g, ''), ".bind(this), ");
249
252
  }
250
253
  else {
251
- var codeToUse = processCodeBlockInTemplate(code);
252
- var keyToUse = key.includes('-') ? "'".concat(key, "'") : key;
254
+ var codeToUse = options.state === 'inline-with-wrappers' ? processCodeBlockInTemplate(code) : code;
253
255
  allProps += "".concat(keyToUse, ": ").concat(codeToUse, ", ");
254
256
  }
255
257
  }
@@ -261,15 +263,14 @@ var handleNgOutletBindings = function (node) {
261
263
  }
262
264
  return allProps;
263
265
  };
264
- var blockToAngular = function (json, options, blockOptions) {
265
- var _a, _b, _c, _d;
266
- if (options === void 0) { options = {}; }
267
- if (blockOptions === void 0) { blockOptions = {
266
+ var blockToAngular = function (_a) {
267
+ var _b, _c, _d, _e;
268
+ var root = _a.root, json = _a.json, _f = _a.options, options = _f === void 0 ? {} : _f, _g = _a.blockOptions, blockOptions = _g === void 0 ? {
268
269
  nativeAttributes: [],
269
- }; }
270
+ } : _g;
270
271
  var childComponents = (blockOptions === null || blockOptions === void 0 ? void 0 : blockOptions.childComponents) || [];
271
272
  if (mappers[json.name]) {
272
- return mappers[json.name](json, options);
273
+ return mappers[json.name](root, json, options);
273
274
  }
274
275
  if ((0, is_children_1.default)({ node: json })) {
275
276
  return "<ng-content></ng-content>";
@@ -277,38 +278,41 @@ var blockToAngular = function (json, options, blockOptions) {
277
278
  if (json.properties._text) {
278
279
  return json.properties._text;
279
280
  }
280
- var textCode = (_a = json.bindings._text) === null || _a === void 0 ? void 0 : _a.code;
281
+ var textCode = (_b = json.bindings._text) === null || _b === void 0 ? void 0 : _b.code;
281
282
  if (textCode) {
282
283
  if ((0, slots_1.isSlotProperty)(textCode)) {
283
284
  return "<ng-content select=\"[".concat((0, slots_1.toKebabSlot)(textCode), "]\"></ng-content>");
284
285
  }
285
286
  if (textCode.includes('JSON.stringify')) {
286
- var obj = textCode.replace('JSON.stringify', '');
287
- obj = obj.replace(/\(.*?\)/, '');
288
- return "{{useJsonStringify".concat(obj, "}}");
287
+ var obj = textCode.replace(/JSON.stringify\(\s*(\w+)\s*,?.*\)/, '$1');
288
+ return "{{".concat(obj, " | json}}");
289
289
  }
290
290
  return "{{".concat(textCode, "}}");
291
291
  }
292
292
  var str = '';
293
293
  if ((0, mitosis_node_1.checkIsForNode)(json)) {
294
294
  var indexName = json.scope.indexName;
295
- str += "<ng-container *ngFor=\"let ".concat(json.scope.forName, " of ").concat((_b = json.bindings.each) === null || _b === void 0 ? void 0 : _b.code).concat(indexName ? "; let ".concat(indexName, " = index") : '', "\">");
296
- str += json.children.map(function (item) { return (0, exports.blockToAngular)(item, options, blockOptions); }).join('\n');
295
+ str += "<ng-container *ngFor=\"let ".concat(json.scope.forName, " of ").concat((_c = json.bindings.each) === null || _c === void 0 ? void 0 : _c.code).concat(indexName ? "; let ".concat(indexName, " = index") : '', "\">");
296
+ str += json.children
297
+ .map(function (item) { return (0, exports.blockToAngular)({ root: root, json: item, options: options, blockOptions: blockOptions }); })
298
+ .join('\n');
297
299
  str += "</ng-container>";
298
300
  }
299
301
  else if (json.name === 'Show') {
300
- var condition = (_c = json.bindings.when) === null || _c === void 0 ? void 0 : _c.code;
301
- if (condition === null || condition === void 0 ? void 0 : condition.includes('typeof')) {
302
+ var condition = (_d = json.bindings.when) === null || _d === void 0 ? void 0 : _d.code;
303
+ if (options.state === 'inline-with-wrappers' && (condition === null || condition === void 0 ? void 0 : condition.includes('typeof'))) {
302
304
  var wordAfterTypeof = condition.split('typeof')[1].trim().split(' ')[0];
303
305
  condition = condition.replace("typeof ".concat(wordAfterTypeof), "useTypeOf(".concat(wordAfterTypeof, ")"));
304
306
  }
305
307
  str += "<ng-container *ngIf=\"".concat(condition, "\">");
306
- str += json.children.map(function (item) { return (0, exports.blockToAngular)(item, options, blockOptions); }).join('\n');
308
+ str += json.children
309
+ .map(function (item) { return (0, exports.blockToAngular)({ root: root, json: item, options: options, blockOptions: blockOptions }); })
310
+ .join('\n');
307
311
  str += "</ng-container>";
308
312
  // else condition
309
- if ((0, is_mitosis_node_1.isMitosisNode)((_d = json.meta) === null || _d === void 0 ? void 0 : _d.else)) {
313
+ if ((0, is_mitosis_node_1.isMitosisNode)((_e = json.meta) === null || _e === void 0 ? void 0 : _e.else)) {
310
314
  str += "<ng-container *ngIf=\"!(".concat(condition, ")\">");
311
- str += (0, exports.blockToAngular)(json.meta.else, options, blockOptions);
315
+ str += (0, exports.blockToAngular)({ root: root, json: json.meta.else, options: options, blockOptions: blockOptions });
312
316
  str += "</ng-container>";
313
317
  }
314
318
  }
@@ -316,8 +320,38 @@ var blockToAngular = function (json, options, blockOptions) {
316
320
  var elSelector = childComponents.find(function (impName) { return impName === json.name; })
317
321
  ? (0, lodash_1.kebabCase)(json.name)
318
322
  : json.name;
319
- var allProps = handleNgOutletBindings(json);
320
- str += "<ng-container *ngComponentOutlet=\"\n ".concat(elSelector.replace('state.', '').replace('props.', ''), ";\n inputs: { ").concat(allProps, " };\n content: myContent;\n \"> ");
323
+ var allProps = handleNgOutletBindings(json, options);
324
+ if (options.state === 'class-properties') {
325
+ var inputsPropsStateName = "mergedInputs_".concat((0, symbol_processor_1.hashCodeAsString)(allProps));
326
+ root.state[inputsPropsStateName] = {
327
+ code: 'null',
328
+ type: 'property',
329
+ };
330
+ if (!root.hooks.onMount
331
+ .map(function (hook) { return hook.code; })
332
+ .join('')
333
+ .includes(inputsPropsStateName)) {
334
+ root.hooks.onMount.push({
335
+ code: "this.".concat(inputsPropsStateName, " = {").concat(allProps, "}"),
336
+ onSSR: false,
337
+ });
338
+ }
339
+ if (root.hooks.onUpdate &&
340
+ root.hooks.onUpdate.length > 0 &&
341
+ !root.hooks.onUpdate
342
+ .map(function (hook) { return hook.code; })
343
+ .join('')
344
+ .includes(inputsPropsStateName)) {
345
+ root.hooks.onUpdate.push({
346
+ code: "this.".concat(inputsPropsStateName, " = {").concat(allProps, "}"),
347
+ });
348
+ }
349
+ allProps = "".concat(inputsPropsStateName);
350
+ }
351
+ else {
352
+ allProps = "{ ".concat(allProps, " }");
353
+ }
354
+ str += "<ng-container *ngComponentOutlet=\"\n ".concat(elSelector.replace('state.', '').replace('props.', ''), ";\n inputs: ").concat(allProps, ";\n content: myContent;\n \"> ");
321
355
  str += "</ng-container>";
322
356
  }
323
357
  else {
@@ -347,7 +381,9 @@ var blockToAngular = function (json, options, blockOptions) {
347
381
  }
348
382
  str += '>';
349
383
  if (json.children) {
350
- str += json.children.map(function (item) { return (0, exports.blockToAngular)(item, options, blockOptions); }).join('\n');
384
+ str += json.children
385
+ .map(function (item) { return (0, exports.blockToAngular)({ root: root, json: item, options: options, blockOptions: blockOptions }); })
386
+ .join('\n');
351
387
  }
352
388
  str += "</".concat(elSelector, ">");
353
389
  }
@@ -360,7 +396,7 @@ var traverseToGetAllDynamicComponents = function (json, options, blockOptions) {
360
396
  (0, traverse_1.default)(json).forEach(function (item) {
361
397
  if ((0, is_mitosis_node_1.isMitosisNode)(item) && item.name.includes('.') && item.name.split('.').length === 2) {
362
398
  var children = item.children
363
- .map(function (child) { return (0, exports.blockToAngular)(child, options, blockOptions); })
399
+ .map(function (child) { return (0, exports.blockToAngular)({ root: json, json: child, options: options, blockOptions: blockOptions }); })
364
400
  .join('\n');
365
401
  dynamicTemplate = "<ng-template #".concat(item.name.split('.')[1].toLowerCase() + 'Template', ">").concat(children, "</ng-template>");
366
402
  components.add(item.name);
@@ -382,6 +418,147 @@ var processAngularCode = function (_a) {
382
418
  }), function (newCode) { return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(newCode, { replaceWith: replaceWith }); });
383
419
  };
384
420
  };
421
+ var isASimpleProperty = function (code) {
422
+ var expressions = ['==', '===', '!=', '!==', '<', '>', '<=', '>='];
423
+ var invalidChars = ['{', '}', '(', ')', 'typeof'];
424
+ return !invalidChars.some(function (char) { return code.includes(char); }) && !expressions.includes(code);
425
+ };
426
+ var generateNewBindingName = function (index, name) {
427
+ return "node_".concat(index, "_").concat(name.replaceAll('.', '_').replaceAll('-', '_'));
428
+ };
429
+ var handleBindings = function (json, item, index, forName, indexName) {
430
+ var _a, _b, _c, _d, _e, _f;
431
+ for (var key in item.bindings) {
432
+ if (key.startsWith('"') ||
433
+ key.startsWith('$') ||
434
+ key === 'css' ||
435
+ key === 'ref' ||
436
+ isASimpleProperty(item.bindings[key].code)) {
437
+ continue;
438
+ }
439
+ var newBindingName = generateNewBindingName(index, item.name);
440
+ if (forName) {
441
+ if (item.name === 'For')
442
+ continue;
443
+ if (key.startsWith('on')) {
444
+ var _g = item.bindings[key].arguments, cusArgs = _g === void 0 ? ['event'] : _g;
445
+ var eventBindingName = "".concat(generateNewBindingName(index, item.name), "_event");
446
+ if (((_a = item.bindings[key]) === null || _a === void 0 ? void 0 : _a.code.trim().startsWith('{')) &&
447
+ ((_b = item.bindings[key]) === null || _b === void 0 ? void 0 : _b.code.trim().endsWith('}'))) {
448
+ var forAndIndex = "".concat(forName ? ", ".concat(forName) : '').concat(indexName ? ", ".concat(indexName) : '');
449
+ var eventArgs = "".concat(cusArgs.join(', ')).concat(forAndIndex);
450
+ json.state[eventBindingName] = {
451
+ code: "(".concat(eventArgs, ") => ").concat(item.bindings[key].code),
452
+ type: 'function',
453
+ };
454
+ item.bindings[key].code = "state.".concat(eventBindingName, "(").concat(eventArgs, ")");
455
+ json.state[newBindingName] = {
456
+ code: "(".concat(eventArgs, ") => (").concat(item.bindings[key].code, ")"),
457
+ type: 'function',
458
+ };
459
+ item.bindings[key].code = "state.".concat(newBindingName, "($").concat(eventArgs, ")");
460
+ }
461
+ }
462
+ else {
463
+ json.state[newBindingName] = {
464
+ code: "(".concat(forName).concat(indexName ? ", ".concat(indexName) : '', ") => (").concat(item.bindings[key].code, ")"),
465
+ type: 'function',
466
+ };
467
+ item.bindings[key].code = "state.".concat(newBindingName, "(").concat(forName).concat(indexName ? ", ".concat(indexName) : '', ")");
468
+ }
469
+ }
470
+ else if ((_c = item.bindings[key]) === null || _c === void 0 ? void 0 : _c.code) {
471
+ if (((_d = item.bindings[key]) === null || _d === void 0 ? void 0 : _d.type) !== 'spread' && !key.startsWith('on')) {
472
+ json.state[newBindingName] = { code: 'null', type: 'property' };
473
+ json.hooks['onMount'].push({
474
+ code: "state.".concat(newBindingName, " = ").concat(item.bindings[key].code),
475
+ onSSR: false,
476
+ });
477
+ json.hooks['onUpdate'] = json.hooks['onUpdate'] || [];
478
+ json.hooks['onUpdate'].push({
479
+ code: "state.".concat(newBindingName, " = ").concat(item.bindings[key].code),
480
+ });
481
+ item.bindings[key].code = "state.".concat(newBindingName);
482
+ }
483
+ else if (key.startsWith('on')) {
484
+ var _h = item.bindings[key].arguments, cusArgs = _h === void 0 ? ['event'] : _h;
485
+ if (((_e = item.bindings[key]) === null || _e === void 0 ? void 0 : _e.code.trim().startsWith('{')) &&
486
+ ((_f = item.bindings[key]) === null || _f === void 0 ? void 0 : _f.code.trim().endsWith('}'))) {
487
+ json.state[newBindingName] = {
488
+ code: "(".concat(cusArgs.join(', '), ") => ").concat(item.bindings[key].code),
489
+ type: 'function',
490
+ };
491
+ item.bindings[key].code = "state.".concat(newBindingName, "(").concat(cusArgs.join(', '), ")");
492
+ }
493
+ }
494
+ else {
495
+ json.state[newBindingName] = { code: "null", type: 'property' };
496
+ json.hooks['onMount'].push({
497
+ code: "state.".concat(newBindingName, " = {...(").concat(item.bindings[key].code, ")}"),
498
+ onSSR: false,
499
+ });
500
+ json.hooks['onUpdate'] = json.hooks['onUpdate'] || [];
501
+ json.hooks['onUpdate'].push({
502
+ code: "state.".concat(newBindingName, " = {...(").concat(item.bindings[key].code, ")}"),
503
+ });
504
+ item.bindings[newBindingName] = item.bindings[key];
505
+ item.bindings[key].code = "state.".concat(newBindingName);
506
+ delete item.bindings[key];
507
+ }
508
+ }
509
+ index++;
510
+ }
511
+ return index;
512
+ };
513
+ var handleProperties = function (json, item, index) {
514
+ for (var key in item.properties) {
515
+ if (key.startsWith('$') || isASimpleProperty(item.properties[key])) {
516
+ continue;
517
+ }
518
+ var newBindingName = generateNewBindingName(index, item.name);
519
+ json.state[newBindingName] = { code: '`' + "".concat(item.properties[key]) + '`', type: 'property' };
520
+ item.bindings[key] = { code: "state.".concat(newBindingName), type: 'single' };
521
+ delete item.properties[key];
522
+ index++;
523
+ }
524
+ return index;
525
+ };
526
+ var handleAngularBindings = function (json, item, index, _a) {
527
+ var _b = _a === void 0 ? {} : _a, forName = _b.forName, indexName = _b.indexName;
528
+ if ((0, is_children_1.default)({ node: item }))
529
+ return index;
530
+ index = handleBindings(json, item, index, forName, indexName);
531
+ index = handleProperties(json, item, index);
532
+ return index;
533
+ };
534
+ var classPropertiesPlugin = function () { return ({
535
+ json: {
536
+ pre: function (json) {
537
+ var lastId = 0;
538
+ (0, traverse_nodes_1.traverseNodes)(json, function (item) {
539
+ if ((0, is_mitosis_node_1.isMitosisNode)(item)) {
540
+ if (item.name === 'For') {
541
+ var forName_1 = item.scope.forName;
542
+ var indexName_1 = item.scope.indexName;
543
+ (0, traverse_nodes_1.traverseNodes)(item, function (child) {
544
+ if ((0, is_mitosis_node_1.isMitosisNode)(child)) {
545
+ child._traversed = true;
546
+ lastId = handleAngularBindings(json, child, lastId, {
547
+ forName: forName_1,
548
+ indexName: indexName_1,
549
+ });
550
+ }
551
+ });
552
+ }
553
+ else if (!item._traversed) {
554
+ lastId = handleAngularBindings(json, item, lastId);
555
+ }
556
+ }
557
+ });
558
+ return json;
559
+ },
560
+ },
561
+ }); };
385
562
  var componentToAngular = function (userOptions) {
386
563
  if (userOptions === void 0) { userOptions = {}; }
387
564
  return function (_a) {
@@ -446,6 +623,9 @@ var componentToAngular = function (userOptions) {
446
623
  }
447
624
  }),
448
625
  ], false);
626
+ if (options.state === 'class-properties') {
627
+ options.plugins.push(classPropertiesPlugin);
628
+ }
449
629
  if (options.plugins) {
450
630
  json = (0, plugins_1.runPreJsonPlugins)({ json: json, plugins: options.plugins });
451
631
  }
@@ -515,13 +695,20 @@ var componentToAngular = function (userOptions) {
515
695
  var template = json.children
516
696
  .map(function (item) {
517
697
  var _a, _b;
518
- var tmpl = (0, exports.blockToAngular)(item, options, {
519
- childComponents: childComponents,
520
- nativeAttributes: (_b = (_a = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _a === void 0 ? void 0 : _a.nativeAttributes) !== null && _b !== void 0 ? _b : [],
521
- });
522
- (0, helpers_2.getAppropriateTemplateFunctionKeys)(tmpl).forEach(function (key) {
523
- return helperFunctions.add(helpers_2.HELPER_FUNCTIONS[key]);
698
+ var tmpl = (0, exports.blockToAngular)({
699
+ root: json,
700
+ json: item,
701
+ options: options,
702
+ blockOptions: {
703
+ childComponents: childComponents,
704
+ nativeAttributes: (_b = (_a = useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.angular) === null || _a === void 0 ? void 0 : _a.nativeAttributes) !== null && _b !== void 0 ? _b : [],
705
+ },
524
706
  });
707
+ if (options.state === 'inline-with-wrappers') {
708
+ (0, helpers_2.getAppropriateTemplateFunctionKeys)(tmpl).forEach(function (key) {
709
+ return helperFunctions.add((0, helpers_2.HELPER_FUNCTIONS)(options.typescript)[key]);
710
+ });
711
+ }
525
712
  return tmpl;
526
713
  })
527
714
  .join('\n');
@@ -594,23 +781,25 @@ var componentToAngular = function (userOptions) {
594
781
  .filter(function (item) { return !(0, slots_1.isSlotProperty)(item) && item !== 'children'; })
595
782
  .map(function (item) {
596
783
  var propType = propsTypeRef ? "".concat(propsTypeRef, "[\"").concat(item, "\"]") : 'any';
597
- var propDeclaration = "@Input() ".concat(item, "!: ").concat(propType);
784
+ var propDeclaration = "@Input() ".concat(item).concat(options.typescript ? "!: ".concat(propType) : '');
598
785
  if (json.defaultProps && json.defaultProps.hasOwnProperty(item)) {
599
786
  propDeclaration += " = defaultProps[\"".concat(item, "\"]");
600
787
  }
601
788
  return propDeclaration;
602
789
  })
603
790
  .join('\n'), outputs.join('\n'), Array.from(domRefs)
604
- .map(function (refName) { return "@ViewChild('".concat(refName, "') ").concat(refName, "!: ElementRef"); })
791
+ .map(function (refName) {
792
+ return "@ViewChild('".concat(refName, "') ").concat(refName).concat(options.typescript ? '!: ElementRef' : '');
793
+ })
605
794
  .join('\n'), Array.from(dynamicComponents)
606
795
  .map(function (component) {
607
796
  return "@ViewChild('".concat(component
608
797
  .split('.')[1]
609
798
  .toLowerCase(), "Template', { static: true }) ").concat(component
610
799
  .split('.')[1]
611
- .toLowerCase(), "TemplateRef!: TemplateRef<any>");
800
+ .toLowerCase(), "TemplateRef").concat(options.typescript ? '!: TemplateRef<any>' : '');
612
801
  })
613
- .join('\n'), dynamicComponents.size ? 'myContent?: any[][];' : '', dataString, helperFunctions.size ? Array.from(helperFunctions).join('\n') : '', jsRefs
802
+ .join('\n'), dynamicComponents.size ? "myContent".concat(options.typescript ? '?: any[][];' : '') : '', dataString, helperFunctions.size ? Array.from(helperFunctions).join('\n') : '', jsRefs
614
803
  .map(function (ref) {
615
804
  var argument = json.refs[ref].argument;
616
805
  var typeParameter = json.refs[ref].typeParameter;
@@ -626,7 +815,9 @@ var componentToAngular = function (userOptions) {
626
815
  })
627
816
  .join('\n'), !hasConstructor && !dynamicComponents.size
628
817
  ? ''
629
- : "constructor(\n".concat(injectables.join(',\n')).concat(dynamicComponents.size ? '\nprivate vcRef: ViewContainerRef,\n' : '', ") {\n ").concat(!((_h = json.hooks) === null || _h === void 0 ? void 0 : _h.onInit)
818
+ : "constructor(\n".concat(injectables.join(',\n')).concat(dynamicComponents.size
819
+ ? "\nprivate vcRef".concat(options.typescript ? ': ViewContainerRef' : '', ",\n")
820
+ : '', ") {\n ").concat(!((_h = json.hooks) === null || _h === void 0 ? void 0 : _h.onInit)
630
821
  ? ''
631
822
  : "\n ".concat((_j = json.hooks.onInit) === null || _j === void 0 ? void 0 : _j.code, "\n "), "\n }\n "), !json.hooks.onMount.length && !dynamicComponents.size
632
823
  ? ''
@@ -640,7 +831,7 @@ var componentToAngular = function (userOptions) {
640
831
  .join(', '), "];\n ")
641
832
  : '', "\n }"), !((_k = json.hooks.onUpdate) === null || _k === void 0 ? void 0 : _k.length)
642
833
  ? ''
643
- : "ngAfterContentChecked() {\n ".concat(json.hooks.onUpdate.reduce(function (code, hook) {
834
+ : "ngOnChanges() {\n ".concat(json.hooks.onUpdate.reduce(function (code, hook) {
644
835
  code += hook.code;
645
836
  return code + '\n';
646
837
  }, ''), "\n }"), !json.hooks.onUnMount
@@ -1,6 +1,7 @@
1
1
  import { BaseTranspilerOptions } from '../../types/transpiler';
2
2
  export declare const BUILT_IN_COMPONENTS: Set<string>;
3
3
  export interface ToAngularOptions extends BaseTranspilerOptions {
4
+ state?: 'class-properties' | 'inline-with-wrappers';
4
5
  standalone?: boolean;
5
6
  preserveImports?: boolean;
6
7
  preserveFileExtensions?: boolean;
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DEFAULT_ANGULAR_OPTIONS = exports.BUILT_IN_COMPONENTS = void 0;
4
4
  exports.BUILT_IN_COMPONENTS = new Set(['Show', 'For', 'Fragment', 'Slot']);
5
5
  exports.DEFAULT_ANGULAR_OPTIONS = {
6
+ state: 'inline-with-wrappers',
6
7
  preserveImports: false,
7
8
  preserveFileExtensions: false,
8
9
  visuallyIgnoreHostElement: true,
@@ -60,7 +60,7 @@ var blockToMitosis = function (json, toMitosisOptions, component) {
60
60
  stylesType: 'emotion',
61
61
  type: 'dom',
62
62
  prettier: options.prettier,
63
- }, component);
63
+ }, component, true);
64
64
  }
65
65
  if ((0, mitosis_node_1.checkIsForNode)(json)) {
66
66
  var needsWrapper = json.children.length !== 1;
@@ -149,7 +149,7 @@ var componentToQwik = function (userOptions) {
149
149
  }
150
150
  catch (e) {
151
151
  console.error(e);
152
- return e.stack || String(e);
152
+ throw e;
153
153
  }
154
154
  };
155
155
  };
@@ -1,4 +1,4 @@
1
1
  import { MitosisComponent } from '../../types/mitosis-component';
2
2
  import { MitosisNode } from '../../types/mitosis-node';
3
3
  import { ToReactOptions } from './types';
4
- export declare const blockToReact: (json: MitosisNode, options: ToReactOptions, component: MitosisComponent, parentSlots?: any[]) => string;
4
+ export declare const blockToReact: (json: MitosisNode, options: ToReactOptions, component: MitosisComponent, insideJsx: boolean, parentSlots?: any[]) => string;
@@ -26,13 +26,13 @@ var html_tags_1 = require("../../constants/html_tags");
26
26
  var helpers_1 = require("./helpers");
27
27
  var state_1 = require("./state");
28
28
  var NODE_MAPPERS = {
29
- Slot: function (json, options, component, parentSlots) {
29
+ Slot: function (json, options, component, _insideJsx, parentSlots) {
30
30
  var _a, _b;
31
31
  var slotName = ((_a = json.bindings.name) === null || _a === void 0 ? void 0 : _a.code) || json.properties.name;
32
32
  var hasChildren = json.children.length;
33
33
  var renderChildren = function () {
34
34
  var _a;
35
- var childrenStr = (_a = json.children) === null || _a === void 0 ? void 0 : _a.map(function (item) { return (0, exports.blockToReact)(item, options, component); }).join('\n').trim();
35
+ var childrenStr = (_a = json.children) === null || _a === void 0 ? void 0 : _a.map(function (item) { return (0, exports.blockToReact)(item, options, component, true); }).join('\n').trim();
36
36
  /**
37
37
  * Ad-hoc way of figuring out if the children defaultProp is:
38
38
  * - a JSX element, e.g. `<div>foo</div>`
@@ -70,20 +70,26 @@ var NODE_MAPPERS = {
70
70
  Fragment: function (json, options, component) {
71
71
  var wrap = (0, helpers_1.wrapInFragment)(json) || (0, is_root_text_node_1.isRootTextNode)(json);
72
72
  return "".concat(wrap ? (0, helpers_1.getFragment)('open', options) : '').concat(json.children
73
- .map(function (item) { return (0, exports.blockToReact)(item, options, component); })
73
+ .map(function (item) { return (0, exports.blockToReact)(item, options, component, wrap); })
74
74
  .join('\n')).concat(wrap ? (0, helpers_1.getFragment)('close', options) : '');
75
75
  },
76
- For: function (_json, options, component) {
76
+ For: function (_json, options, component, insideJsx) {
77
77
  var _a;
78
78
  var json = _json;
79
79
  var wrap = (0, helpers_1.wrapInFragment)(json);
80
80
  var forArguments = (0, for_1.getForArguments)(json).join(', ');
81
- return "{".concat((0, helpers_1.processBinding)((_a = json.bindings.each) === null || _a === void 0 ? void 0 : _a.code, options), "?.map((").concat(forArguments, ") => (\n ").concat(wrap ? (0, helpers_1.openFrag)(options) : '').concat(json.children
81
+ var expression = "".concat((0, helpers_1.processBinding)((_a = json.bindings.each) === null || _a === void 0 ? void 0 : _a.code, options), "?.map((").concat(forArguments, ") => (\n ").concat(wrap ? (0, helpers_1.openFrag)(options) : '').concat(json.children
82
82
  .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
83
- .map(function (item) { return (0, exports.blockToReact)(item, options, component); })
84
- .join('\n')).concat(wrap ? (0, helpers_1.closeFrag)(options) : '', "\n ))}");
83
+ .map(function (item) { return (0, exports.blockToReact)(item, options, component, wrap); })
84
+ .join('\n')).concat(wrap ? (0, helpers_1.closeFrag)(options) : '', "\n ))");
85
+ if (insideJsx) {
86
+ return "{".concat(expression, "}");
87
+ }
88
+ else {
89
+ return expression;
90
+ }
85
91
  },
86
- Show: function (json, options, component) {
92
+ Show: function (json, options, component, insideJsx) {
87
93
  var _a;
88
94
  var wrap = (0, helpers_1.wrapInFragment)(json) ||
89
95
  (0, is_root_text_node_1.isRootTextNode)(json) ||
@@ -91,16 +97,22 @@ var NODE_MAPPERS = {
91
97
  // when `<Show><For>...</For></Show>`, we need to wrap the For generated code in a fragment
92
98
  // since it's a `.map()` call
93
99
  (json.children.length === 1 && ['For', 'Show'].includes(json.children[0].name));
94
- var wrapElse = json.meta.else &&
95
- ((0, helpers_1.wrapInFragment)(json.meta.else) || (0, mitosis_node_1.checkIsForNode)(json.meta.else));
96
- return "{".concat((0, helpers_1.processBinding)((_a = json.bindings.when) === null || _a === void 0 ? void 0 : _a.code, options), " ? (\n ").concat(wrap ? (0, helpers_1.openFrag)(options) : '').concat(json.children
100
+ var wrapElse = !!(json.meta.else &&
101
+ ((0, helpers_1.wrapInFragment)(json.meta.else) || (0, mitosis_node_1.checkIsForNode)(json.meta.else)));
102
+ var expression = "".concat((0, helpers_1.processBinding)((_a = json.bindings.when) === null || _a === void 0 ? void 0 : _a.code, options), " ? (\n ").concat(wrap ? (0, helpers_1.openFrag)(options) : '').concat(json.children
97
103
  .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
98
- .map(function (item) { return (0, exports.blockToReact)(item, options, component); })
104
+ .map(function (item) { return (0, exports.blockToReact)(item, options, component, wrap); })
99
105
  .join('\n')).concat(wrap ? (0, helpers_1.closeFrag)(options) : '', "\n ) : ").concat(!json.meta.else
100
106
  ? 'null'
101
107
  : (wrapElse ? (0, helpers_1.openFrag)(options) : '') +
102
- (0, exports.blockToReact)(json.meta.else, options, component) +
103
- (wrapElse ? (0, helpers_1.closeFrag)(options) : ''), "}");
108
+ (0, exports.blockToReact)(json.meta.else, options, component, wrapElse) +
109
+ (wrapElse ? (0, helpers_1.closeFrag)(options) : ''));
110
+ if (insideJsx) {
111
+ return "{".concat(expression, "}");
112
+ }
113
+ else {
114
+ return expression;
115
+ }
104
116
  },
105
117
  };
106
118
  var ATTTRIBUTE_MAPPERS = {
@@ -129,11 +141,11 @@ var BINDING_MAPPERS = __assign({ ref: function (ref, value, options) {
129
141
  var NATIVE_EVENT_MAPPER = {
130
142
  onClick: 'onPress',
131
143
  };
132
- var blockToReact = function (json, options, component, parentSlots) {
144
+ var blockToReact = function (json, options, component, insideJsx, parentSlots) {
133
145
  var _a, _b, _c, _d;
134
146
  if (parentSlots === void 0) { parentSlots = []; }
135
147
  if (NODE_MAPPERS[json.name]) {
136
- return NODE_MAPPERS[json.name](json, options, component, parentSlots);
148
+ return NODE_MAPPERS[json.name](json, options, component, insideJsx, parentSlots);
137
149
  }
138
150
  if (json.properties._text) {
139
151
  var text = json.properties._text;
@@ -235,7 +247,7 @@ var blockToReact = function (json, options, component, parentSlots) {
235
247
  if (!(value === null || value === void 0 ? void 0 : value.length)) {
236
248
  continue;
237
249
  }
238
- var reactComponents = value.map(function (node) { return (0, exports.blockToReact)(node, options, component); });
250
+ var reactComponents = value.map(function (node) { return (0, exports.blockToReact)(node, options, component, true); });
239
251
  var slotStringValue = reactComponents.length === 1 ? reactComponents[0] : "<>".concat(reactComponents.join('\n'), "</>");
240
252
  str += " ".concat(key, "={").concat(slotStringValue, "} ");
241
253
  }
@@ -253,7 +265,7 @@ var blockToReact = function (json, options, component, parentSlots) {
253
265
  var childrenNodes = '';
254
266
  if (json.children) {
255
267
  childrenNodes = json.children
256
- .map(function (item) { return (0, exports.blockToReact)(item, options, component, needsToRenderSlots); })
268
+ .map(function (item) { return (0, exports.blockToReact)(item, options, component, true, needsToRenderSlots); })
257
269
  .join('');
258
270
  }
259
271
  if (needsToRenderSlots.length) {
@@ -363,7 +363,7 @@ var _componentToReact = function (json, options, isSubComponent) {
363
363
  var componentArgs = ["props".concat(options.typescript ? ":".concat(propType) : ''), options.forwardRef]
364
364
  .filter(Boolean)
365
365
  .join(',');
366
- var componentBody = (0, dedent_1.dedent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n\n ", "\n \n ", "\n\n ", "\n\n ", "\n\n return (\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n );\n "], ["\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n\n ", "\n \n ", "\n\n ", "\n\n ", "\n\n return (\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n );\n "])), options.contextType === 'prop-drill'
366
+ var componentBody = (0, dedent_1.dedent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n return (\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n );\n "], ["\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n return (\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n );\n "])), options.contextType === 'prop-drill'
367
367
  ? "const ".concat(exports.contextPropDrillingKey, " = { ...props['").concat(exports.contextPropDrillingKey, "'] };")
368
368
  : '', hasStateArgument ? '' : refsString, hasState
369
369
  ? options.stateType === 'mobx'
@@ -410,7 +410,7 @@ var _componentToReact = function (json, options, isSubComponent) {
410
410
  str: json.hooks.onUnMount.code,
411
411
  options: options,
412
412
  }), "\n }\n }, [])")
413
- : '', wrap ? (0, helpers_2.openFrag)(options) : '', json.children.map(function (item) { return (0, blocks_1.blockToReact)(item, options, json, []); }).join('\n'), componentHasStyles && options.stylesType === 'styled-jsx'
413
+ : '', wrap ? (0, helpers_2.openFrag)(options) : '', json.children.map(function (item) { return (0, blocks_1.blockToReact)(item, options, json, wrap, []); }).join('\n'), componentHasStyles && options.stylesType === 'styled-jsx'
414
414
  ? "<style jsx>{`".concat(css, "`}</style>")
415
415
  : '', componentHasStyles && options.stylesType === 'style-tag'
416
416
  ? "<style>{`".concat(css, "`}</style>")
@@ -25,29 +25,44 @@ var html_tags_1 = require("../../constants/html_tags");
25
25
  var plugins_1 = require("../../modules/plugins");
26
26
  var on_mount_1 = require("../helpers/on-mount");
27
27
  var collect_class_string_1 = require("./collect-class-string");
28
- var blockToStencil = function (json, options) {
28
+ var blockToStencil = function (json, options, insideJsx) {
29
29
  var _a, _b, _c, _d;
30
30
  if (options === void 0) { options = {}; }
31
31
  if (json.properties._text) {
32
32
  return json.properties._text;
33
33
  }
34
34
  if ((_a = json.bindings._text) === null || _a === void 0 ? void 0 : _a.code) {
35
- return "{".concat(processBinding((_b = json.bindings) === null || _b === void 0 ? void 0 : _b._text.code), "}");
35
+ if (insideJsx) {
36
+ return "{".concat(processBinding(json.bindings._text.code), "}");
37
+ }
38
+ return processBinding((_b = json.bindings) === null || _b === void 0 ? void 0 : _b._text.code);
36
39
  }
37
40
  if ((0, mitosis_node_1.checkIsForNode)(json)) {
38
- var wrap = json.children.length !== 1;
41
+ var wrap_1 = json.children.length !== 1;
39
42
  var forArgs = (0, for_1.getForArguments)(json).join(', ');
40
- return "{".concat(processBinding((_c = json.bindings.each) === null || _c === void 0 ? void 0 : _c.code), "?.map((").concat(forArgs, ") => (\n ").concat(wrap ? '<>' : '').concat(json.children
43
+ var expression = "".concat(processBinding((_c = json.bindings.each) === null || _c === void 0 ? void 0 : _c.code), "?.map((").concat(forArgs, ") => (\n ").concat(wrap_1 ? '<>' : '').concat(json.children
41
44
  .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
42
- .map(function (item) { return blockToStencil(item, options); })
43
- .join('\n')).concat(wrap ? '</>' : '', "\n ))}");
45
+ .map(function (item) { return blockToStencil(item, options, wrap_1); })
46
+ .join('\n')).concat(wrap_1 ? '</>' : '', "\n ))");
47
+ if (insideJsx) {
48
+ return "{".concat(expression, "}");
49
+ }
50
+ else {
51
+ return expression;
52
+ }
44
53
  }
45
54
  else if (json.name === 'Show') {
46
- var wrap = json.children.length !== 1;
47
- return "{".concat(processBinding((_d = json.bindings.when) === null || _d === void 0 ? void 0 : _d.code), " ? (\n ").concat(wrap ? '<>' : '').concat(json.children
55
+ var wrap_2 = json.children.length !== 1;
56
+ var expression = "".concat(processBinding((_d = json.bindings.when) === null || _d === void 0 ? void 0 : _d.code), " ? (\n ").concat(wrap_2 ? '<>' : '').concat(json.children
48
57
  .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
49
- .map(function (item) { return blockToStencil(item, options); })
50
- .join('\n')).concat(wrap ? '</>' : '', "\n ) : ").concat(!json.meta.else ? 'null' : blockToStencil(json.meta.else, options), "}");
58
+ .map(function (item) { return blockToStencil(item, options, wrap_2); })
59
+ .join('\n')).concat(wrap_2 ? '</>' : '', "\n ) : ").concat(!json.meta.else ? 'null' : blockToStencil(json.meta.else, options, false));
60
+ if (insideJsx) {
61
+ return "{".concat(expression, "}");
62
+ }
63
+ else {
64
+ return expression;
65
+ }
51
66
  }
52
67
  var str = '';
53
68
  str += "<".concat(json.name, " ");
@@ -80,7 +95,7 @@ var blockToStencil = function (json, options) {
80
95
  }
81
96
  str += '>';
82
97
  if (json.children) {
83
- str += json.children.map(function (item) { return blockToStencil(item, options); }).join('\n');
98
+ str += json.children.map(function (item) { return blockToStencil(item, options, true); }).join('\n');
84
99
  }
85
100
  str += "</".concat(json.name, ">");
86
101
  return str;
@@ -132,7 +147,7 @@ var componentToStencil = function (_options) {
132
147
  console.warn('Could not format css', err);
133
148
  }
134
149
  }
135
- var str = (0, dedent_1.dedent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n\n import { Component, Prop, h, State, Fragment } from '@stencil/core';\n\n @Component({\n tag: '", "',\n ", "\n })\n export default class ", " {\n \n ", "\n\n ", "\n ", "\n \n ", "\n ", "\n ", "\n \n render() {\n return (", "\n \n ", "\n\n ", ")\n }\n }\n "], ["\n ", "\n\n import { Component, Prop, h, State, Fragment } from '@stencil/core';\n\n @Component({\n tag: '"
150
+ var str = (0, dedent_1.dedent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n\n import { Component, Prop, h, State, Fragment } from '@stencil/core';\n\n @Component({\n tag: '", "',\n ", "\n })\n export default class ", " {\n\n ", "\n\n ", "\n ", "\n\n ", "\n ", "\n ", "\n\n render() {\n return (", "\n\n ", "\n\n ", ")\n }\n }\n "], ["\n ", "\n\n import { Component, Prop, h, State, Fragment } from '@stencil/core';\n\n @Component({\n tag: '"
136
151
  /**
137
152
  * You can set the tagName in your Mitosis component as
138
153
  *
@@ -142,7 +157,7 @@ var componentToStencil = function (_options) {
142
157
  *
143
158
  * export default function ...
144
159
  */
145
- , "',\n ", "\n })\n export default class ", " {\n \n ", "\n\n ", "\n ", "\n \n ", "\n ", "\n ", "\n \n render() {\n return (", "\n \n ", "\n\n ", ")\n }\n }\n "])), (0, render_imports_1.renderPreComponent)({
160
+ , "',\n ", "\n })\n export default class ", " {\n\n ", "\n\n ", "\n ", "\n\n ", "\n ", "\n ", "\n\n render() {\n return (", "\n\n ", "\n\n ", ")\n }\n }\n "])), (0, render_imports_1.renderPreComponent)({
146
161
  explicitImportFileExtension: options.explicitImportFileExtension,
147
162
  component: json,
148
163
  target: 'stencil',
@@ -166,7 +181,7 @@ var componentToStencil = function (_options) {
166
181
  ? ''
167
182
  : "disconnectedCallback() { ".concat(processBinding(json.hooks.onUnMount.code), " }"), !((_d = json.hooks.onUpdate) === null || _d === void 0 ? void 0 : _d.length)
168
183
  ? ''
169
- : json.hooks.onUpdate.map(function (hook) { return "componentDidUpdate() { ".concat(processBinding(hook.code), " }"); }), wrap ? '<>' : '', json.children.map(function (item) { return blockToStencil(item, options); }).join('\n'), wrap ? '</>' : '');
184
+ : json.hooks.onUpdate.map(function (hook) { return "componentDidUpdate() { ".concat(processBinding(hook.code), " }"); }), wrap ? '<>' : '', json.children.map(function (item) { return blockToStencil(item, options, true); }).join('\n'), wrap ? '</>' : '');
170
185
  if (options.plugins) {
171
186
  str = (0, plugins_1.runPreCodePlugins)({ json: json, code: str, plugins: options.plugins });
172
187
  }
@@ -4,5 +4,5 @@ declare const types: typeof babel.types;
4
4
  /**
5
5
  * Parses a JSX element into a MitosisNode.
6
6
  */
7
- export declare const jsxElementToJson: (node: babel.types.JSXElement | babel.types.JSXText | babel.types.JSXFragment | babel.types.JSXExpressionContainer | babel.types.JSXSpreadChild) => MitosisNode | null;
7
+ export declare const jsxElementToJson: (node: babel.types.Expression | babel.types.JSX) => MitosisNode | null;
8
8
  export {};
@@ -68,77 +68,95 @@ var jsxElementToJson = function (node) {
68
68
  },
69
69
  });
70
70
  }
71
+ if (types.isJSXEmptyExpression(node)) {
72
+ return null;
73
+ }
71
74
  if (types.isJSXExpressionContainer(node)) {
72
- if (types.isJSXEmptyExpression(node.expression)) {
73
- return null;
74
- }
75
- // foo.map -> <For each={foo}>...</For>
76
- if (types.isCallExpression(node.expression) ||
77
- types.isOptionalCallExpression(node.expression)) {
78
- var callback = node.expression.arguments[0];
79
- if (types.isArrowFunctionExpression(callback)) {
80
- if (types.isIdentifier(callback.params[0])) {
81
- var forArguments = getForArguments(callback.params);
82
- return (0, create_mitosis_node_1.createMitosisNode)({
83
- name: 'For',
84
- bindings: {
85
- each: (0, bindings_1.createSingleBinding)({
86
- code: (0, generator_1.default)(node.expression.callee)
87
- .code // Remove .map or potentially ?.map
88
- .replace(/\??\.map$/, ''),
89
- }),
90
- },
91
- scope: forArguments,
92
- children: [(0, exports.jsxElementToJson)(callback.body)],
93
- });
94
- }
95
- }
96
- }
97
- // {foo && <div />} -> <Show when={foo}>...</Show>
98
- if (types.isLogicalExpression(node.expression)) {
99
- if (node.expression.operator === '&&') {
75
+ return (0, exports.jsxElementToJson)(node.expression);
76
+ }
77
+ if (types.isCallExpression(node) || types.isOptionalCallExpression(node)) {
78
+ var callback = node.arguments[0];
79
+ if (types.isArrowFunctionExpression(callback)) {
80
+ if (types.isIdentifier(callback.params[0])) {
81
+ var forArguments = getForArguments(callback.params);
100
82
  return (0, create_mitosis_node_1.createMitosisNode)({
101
- name: 'Show',
83
+ name: 'For',
102
84
  bindings: {
103
- when: (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(node.expression.left).code }),
85
+ each: (0, bindings_1.createSingleBinding)({
86
+ code: (0, generator_1.default)(node.callee)
87
+ .code // Remove .map or potentially ?.map
88
+ .replace(/\??\.map$/, ''),
89
+ }),
104
90
  },
105
- children: [(0, exports.jsxElementToJson)(node.expression.right)],
91
+ scope: forArguments,
92
+ children: [(0, exports.jsxElementToJson)(callback.body)],
106
93
  });
107
94
  }
108
- else {
109
- // TODO: good warning system for unsupported operators
110
- }
111
95
  }
112
- // {foo ? <div /> : <span />} -> <Show when={foo} else={<span />}>...</Show>
113
- if (types.isConditionalExpression(node.expression)) {
96
+ }
97
+ else if (types.isLogicalExpression(node)) {
98
+ // {foo && <div />} -> <Show when={foo}>...</Show>
99
+ if (node.operator === '&&') {
114
100
  return (0, create_mitosis_node_1.createMitosisNode)({
115
101
  name: 'Show',
116
- meta: {
117
- else: (0, exports.jsxElementToJson)(node.expression.alternate),
118
- },
119
102
  bindings: {
120
- when: (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(node.expression.test).code }),
103
+ when: (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(node.left).code }),
121
104
  },
122
- children: [(0, exports.jsxElementToJson)(node.expression.consequent)],
105
+ children: [(0, exports.jsxElementToJson)(node.right)],
123
106
  });
124
107
  }
125
- // TODO: support {foo ? bar : baz}
108
+ else {
109
+ // TODO: good warning system for unsupported operators
110
+ }
111
+ }
112
+ else if (types.isConditionalExpression(node)) {
113
+ // {foo ? <div /> : <span />} -> <Show when={foo} else={<span />}>...</Show>
114
+ var child = (0, exports.jsxElementToJson)(node.consequent);
126
115
  return (0, create_mitosis_node_1.createMitosisNode)({
116
+ name: 'Show',
117
+ meta: {
118
+ else: (0, exports.jsxElementToJson)(node.alternate),
119
+ },
127
120
  bindings: {
128
- _text: (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(node.expression).code }),
121
+ when: (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(node.test).code }),
129
122
  },
123
+ children: child === null ? [] : [child],
130
124
  });
131
125
  }
132
- if (types.isJSXFragment(node)) {
126
+ else if (types.isJSXFragment(node)) {
133
127
  return (0, create_mitosis_node_1.createMitosisNode)({
134
128
  name: 'Fragment',
135
129
  children: node.children.map(exports.jsxElementToJson).filter(nullable_1.checkIsDefined),
136
130
  });
137
131
  }
138
- // TODO: support spread attributes
139
- if (types.isJSXSpreadChild(node)) {
132
+ else if (types.isJSXSpreadChild(node)) {
133
+ // TODO: support spread attributes
140
134
  return null;
141
135
  }
136
+ else if (types.isNullLiteral(node) || types.isBooleanLiteral(node)) {
137
+ return null;
138
+ }
139
+ else if (types.isNumericLiteral(node)) {
140
+ return (0, create_mitosis_node_1.createMitosisNode)({
141
+ properties: {
142
+ _text: String(node.value),
143
+ },
144
+ });
145
+ }
146
+ else if (types.isStringLiteral(node)) {
147
+ return (0, create_mitosis_node_1.createMitosisNode)({
148
+ properties: {
149
+ _text: node.value,
150
+ },
151
+ });
152
+ }
153
+ if (!types.isJSXElement(node)) {
154
+ return (0, create_mitosis_node_1.createMitosisNode)({
155
+ bindings: {
156
+ _text: (0, bindings_1.createSingleBinding)({ code: (0, generator_1.default)(node).code }),
157
+ },
158
+ });
159
+ }
142
160
  var nodeName = (0, generator_1.default)(node.openingElement.name).code;
143
161
  if (nodeName === 'Show') {
144
162
  var whenAttr = node.openingElement.attributes.find(function (item) { return types.isJSXAttribute(item) && item.name.name === 'when'; });
@@ -322,7 +322,10 @@ var componentFunctionToJson = function (node, context) {
322
322
  if (theReturn) {
323
323
  var value = theReturn.argument;
324
324
  if (types.isJSXElement(value) || types.isJSXFragment(value)) {
325
- children.push((0, element_parser_1.jsxElementToJson)(value));
325
+ var jsxElement = (0, element_parser_1.jsxElementToJson)(value);
326
+ if (jsxElement) {
327
+ children.push(jsxElement);
328
+ }
326
329
  }
327
330
  }
328
331
  var localExports = context.builder.component.exports;
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.3.4",
25
+ "version": "0.3.6",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {