@builder.io/mitosis 0.0.81 → 0.0.82

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.
Files changed (37) hide show
  1. package/dist/src/generators/alpine/generate.js +3 -2
  2. package/dist/src/generators/angular.js +67 -28
  3. package/dist/src/generators/html.js +42 -20
  4. package/dist/src/generators/marko/generate.js +21 -15
  5. package/dist/src/generators/mitosis.d.ts +2 -1
  6. package/dist/src/generators/mitosis.js +5 -5
  7. package/dist/src/generators/qwik/src-generator.js +2 -0
  8. package/dist/src/generators/react/generator.d.ts +2 -1
  9. package/dist/src/generators/react/generator.js +73 -20
  10. package/dist/src/generators/solid/index.js +14 -3
  11. package/dist/src/generators/svelte/blocks.js +15 -6
  12. package/dist/src/generators/svelte/svelte.js +4 -4
  13. package/dist/src/generators/vue/blocks.js +12 -6
  14. package/dist/src/generators/vue/compositionApi.js +10 -1
  15. package/dist/src/generators/vue/helpers.js +45 -22
  16. package/dist/src/generators/vue/optionsApi.js +23 -13
  17. package/dist/src/helpers/babel-transform.js +30 -19
  18. package/dist/src/helpers/patterns.d.ts +1 -0
  19. package/dist/src/helpers/patterns.js +5 -3
  20. package/dist/src/helpers/plugins/process-code.js +6 -6
  21. package/dist/src/helpers/replace-identifiers.d.ts +5 -3
  22. package/dist/src/helpers/replace-identifiers.js +124 -21
  23. package/dist/src/helpers/slots.js +3 -1
  24. package/dist/src/helpers/strip-state-and-props-refs.d.ts +23 -4
  25. package/dist/src/helpers/strip-state-and-props-refs.js +57 -53
  26. package/dist/src/helpers/typescript.d.ts +1 -0
  27. package/dist/src/helpers/typescript.js +3 -0
  28. package/dist/src/parsers/jsx/element-parser.js +3 -2
  29. package/dist/src/parsers/jsx/function-parser.js +3 -10
  30. package/dist/src/parsers/jsx/helpers.d.ts +4 -0
  31. package/dist/src/parsers/jsx/helpers.js +11 -1
  32. package/dist/src/parsers/jsx/state.d.ts +1 -1
  33. package/dist/src/parsers/jsx/state.js +85 -39
  34. package/dist/src/parsers/svelte/html/slot.js +3 -8
  35. package/dist/src/types/mitosis-component.d.ts +1 -1
  36. package/dist/tsconfig.build.tsbuildinfo +1 -1
  37. package/package.json +1 -1
@@ -165,18 +165,19 @@ var componentToAlpine = function (options) {
165
165
  if (options.plugins) {
166
166
  json = (0, plugins_1.runPostJsonPlugins)(json, options.plugins);
167
167
  }
168
+ var componentName = (0, lodash_1.camelCase)(json.name) || 'MyComponent';
168
169
  var stateObjectString = getStateObjectString(json);
169
170
  // Set x-data on root element
170
171
  json.children[0].properties['x-data'] = options.inlineState
171
172
  ? stateObjectString
172
- : "".concat((0, lodash_1.camelCase)(json.name), "()");
173
+ : "".concat(componentName, "()");
173
174
  if ((0, render_update_hooks_1.hasRootUpdateHook)(json)) {
174
175
  json.children[0].properties['x-effect'] = 'onUpdate';
175
176
  }
176
177
  var str = css.trim().length ? "<style>".concat(css, "</style>") : '';
177
178
  str += json.children.map(function (item) { return blockToAlpine(item, options); }).join('\n');
178
179
  if (!options.inlineState) {
179
- str += "<script>\n ".concat((0, babel_transform_1.babelTransformCode)("document.addEventListener('alpine:init', () => {\n Alpine.data('".concat((0, lodash_1.camelCase)(json.name), "', () => (").concat(stateObjectString, "))\n })")), "\n </script>");
180
+ str += "<script>\n ".concat((0, babel_transform_1.babelTransformCode)("document.addEventListener('alpine:init', () => {\n Alpine.data('".concat(componentName, "', () => (").concat(stateObjectString, "))\n })")), "\n </script>");
180
181
  }
181
182
  if (options.plugins) {
182
183
  str = (0, plugins_1.runPreCodePlugins)(str, options.plugins);
@@ -29,7 +29,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
30
  exports.componentToAngular = exports.blockToAngular = void 0;
31
31
  var dedent_1 = __importDefault(require("dedent"));
32
- var json5_1 = __importDefault(require("json5"));
33
32
  var standalone_1 = require("prettier/standalone");
34
33
  var collect_css_1 = require("../helpers/styles/collect-css");
35
34
  var fast_clone_1 = require("../helpers/fast-clone");
@@ -53,6 +52,7 @@ var slots_1 = require("../helpers/slots");
53
52
  var get_custom_imports_1 = require("../helpers/get-custom-imports");
54
53
  var get_components_used_1 = require("../helpers/get-components-used");
55
54
  var is_upper_case_1 = require("../helpers/is-upper-case");
55
+ var replace_identifiers_1 = require("../helpers/replace-identifiers");
56
56
  var html_tags_1 = require("../constants/html_tags");
57
57
  var function_1 = require("fp-ts/lib/function");
58
58
  var merge_options_1 = require("../helpers/merge-options");
@@ -65,11 +65,12 @@ var mappers = {
65
65
  .join('\n'), "</ng-container>");
66
66
  },
67
67
  Slot: function (json, options) {
68
- return "<ng-content ".concat(Object.entries(json.bindings)
68
+ 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'); };
69
+ return "<ng-content ".concat(Object.entries(__assign(__assign({}, json.bindings), json.properties))
69
70
  .map(function (_a) {
70
71
  var binding = _a[0], value = _a[1];
71
72
  if (value && binding === 'name') {
72
- var selector = (0, function_1.pipe)(value.code, slots_1.stripSlotPrefix, lodash_1.kebabCase);
73
+ var selector = (0, function_1.pipe)((0, lodash_1.isString)(value) ? value : value.code, slots_1.stripSlotPrefix, lodash_1.kebabCase);
73
74
  return "select=\"[".concat(selector, "]\"");
74
75
  }
75
76
  })
@@ -80,7 +81,7 @@ var mappers = {
80
81
  return value.code;
81
82
  }
82
83
  })
83
- .join('\n'), "</ng-content>");
84
+ .join('\n')).concat(renderChildren(), "</ng-content>");
84
85
  },
85
86
  };
86
87
  var generateNgModule = function (content, name, componentsUsed, component, bootstrapMapper) {
@@ -155,7 +156,8 @@ var blockToAngular = function (json, options, blockOptions) {
155
156
  var _e = json.bindings[key], code = _e.code, _f = _e.arguments, cusArgs = _f === void 0 ? ['event'] : _f;
156
157
  // TODO: proper babel transform to replace. Util for this
157
158
  if (key.startsWith('on')) {
158
- var event_1 = key.replace('on', '').toLowerCase();
159
+ var event_1 = key.replace('on', '');
160
+ event_1 = event_1.charAt(0).toLowerCase() + event_1.slice(1);
159
161
  if (event_1 === 'change' && json.name === 'input' /* todo: other tags */) {
160
162
  event_1 = 'input';
161
163
  }
@@ -202,6 +204,17 @@ var blockToAngular = function (json, options, blockOptions) {
202
204
  return str;
203
205
  };
204
206
  exports.blockToAngular = blockToAngular;
207
+ var processAngularCode = function (_a) {
208
+ var contextVars = _a.contextVars, outputVars = _a.outputVars, domRefs = _a.domRefs, stateVars = _a.stateVars, replaceWith = _a.replaceWith;
209
+ return function (code) {
210
+ return (0, function_1.pipe)((0, strip_state_and_props_refs_1.DO_NOT_USE_VARS_TRANSFORMS)(code, {
211
+ contextVars: contextVars,
212
+ domRefs: domRefs,
213
+ outputVars: outputVars,
214
+ stateVars: stateVars,
215
+ }), function (newCode) { return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(newCode, { replaceWith: replaceWith }); });
216
+ };
217
+ };
205
218
  var componentToAngular = function (userOptions) {
206
219
  if (userOptions === void 0) { userOptions = {}; }
207
220
  return function (_a) {
@@ -220,25 +233,38 @@ var componentToAngular = function (userOptions) {
220
233
  var options = (0, merge_options_1.mergeOptions)(__assign(__assign({}, DEFAULT_OPTIONS), userOptions));
221
234
  options.plugins = __spreadArray(__spreadArray([], (options.plugins || []), true), [
222
235
  (0, process_code_1.CODE_PROCESSOR_PLUGIN)(function (codeType) {
223
- var domRefs = (0, get_refs_1.getRefs)(json);
224
236
  switch (codeType) {
225
237
  case 'hooks':
226
- return function (code) {
227
- return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
228
- replaceWith: 'this.',
229
- contextVars: contextVars,
230
- outputVars: outputVars,
231
- domRefs: Array.from(domRefs),
232
- stateVars: stateVars,
238
+ return (0, function_1.flow)(processAngularCode({
239
+ replaceWith: 'this',
240
+ contextVars: contextVars,
241
+ outputVars: outputVars,
242
+ domRefs: Array.from((0, get_refs_1.getRefs)(json)),
243
+ stateVars: stateVars,
244
+ }), function (code) {
245
+ var allMethodNames = Object.entries(json.state)
246
+ .filter(function (_a) {
247
+ var _ = _a[0], value = _a[1];
248
+ return (value === null || value === void 0 ? void 0 : value.type) === 'function' || (value === null || value === void 0 ? void 0 : value.type) === 'method';
249
+ })
250
+ .map(function (_a) {
251
+ var key = _a[0];
252
+ return key;
233
253
  });
234
- };
254
+ return (0, replace_identifiers_1.replaceIdentifiers)({
255
+ code: code,
256
+ from: allMethodNames,
257
+ to: function (name) { return "this.".concat(name); },
258
+ });
259
+ });
235
260
  case 'bindings':
236
261
  return function (code) {
237
- return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
262
+ var newLocal = processAngularCode({
238
263
  contextVars: [],
239
264
  outputVars: outputVars,
240
265
  domRefs: [], // the template doesn't need the this keyword.
241
- }).replace(/"/g, '&quot;');
266
+ })(code);
267
+ return newLocal.replace(/"/g, '&quot;');
242
268
  };
243
269
  case 'hooks-deps':
244
270
  case 'state':
@@ -321,15 +347,13 @@ var componentToAngular = function (userOptions) {
321
347
  (0, strip_meta_properties_1.stripMetaProperties)(json);
322
348
  var dataString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
323
349
  format: 'class',
324
- valueMapper: function (code) {
325
- return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
326
- replaceWith: 'this.',
327
- contextVars: contextVars,
328
- outputVars: outputVars,
329
- domRefs: Array.from(domRefs),
330
- stateVars: stateVars,
331
- });
332
- },
350
+ valueMapper: processAngularCode({
351
+ replaceWith: 'this',
352
+ contextVars: contextVars,
353
+ outputVars: outputVars,
354
+ domRefs: Array.from(domRefs),
355
+ stateVars: stateVars,
356
+ }),
333
357
  });
334
358
  // Preparing built in component metadata parameters
335
359
  var componentMetadata = __assign(__assign({ selector: "'".concat((0, lodash_1.kebabCase)(json.name || 'my-component'), ", ").concat(json.name, "'"), template: "`\n ".concat((0, indent_1.indent)(template, 8).replace(/`/g, '\\`').replace(/\$\{/g, '\\${'), "\n `") }, (css.length
@@ -348,7 +372,22 @@ var componentToAngular = function (userOptions) {
348
372
  var key = _a[0], value = _a[1];
349
373
  componentMetadata[key] = value;
350
374
  });
351
- var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n import { ", " ", " Component ", "", " } from '@angular/core';\n ", "\n\n ", "\n ", "\n ", "\n\n @Component({\n ", "\n })\n export class ", " {\n ", "\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n }\n "], ["\n import { ", " ", " Component ", "", " } from '@angular/core';\n ", "\n\n ", "\n ", "\n ", "\n\n @Component({\n ", "\n })\n export class ", " {\n ", "\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n }\n "])), outputs.length ? 'Output, EventEmitter, \n' : '', ((_g = options === null || options === void 0 ? void 0 : options.experimental) === null || _g === void 0 ? void 0 : _g.inject) ? 'Inject, forwardRef,' : '', domRefs.size ? ', ViewChild, ElementRef' : '', props.size ? ', Input' : '', options.standalone ? "import { CommonModule } from '@angular/common';" : '', json.types ? json.types.join('\n') : '', !json.defaultProps ? '' : "const defaultProps = ".concat(json5_1.default.stringify(json.defaultProps), "\n"), (0, render_imports_1.renderPreComponent)({
375
+ var getPropsDefinition = function (_a) {
376
+ var json = _a.json;
377
+ if (!json.defaultProps)
378
+ return '';
379
+ var defalutPropsString = Object.keys(json.defaultProps)
380
+ .map(function (prop) {
381
+ var _a;
382
+ var value = json.defaultProps.hasOwnProperty(prop)
383
+ ? (_a = json.defaultProps[prop]) === null || _a === void 0 ? void 0 : _a.code
384
+ : '{}';
385
+ return "".concat(prop, ": ").concat(value);
386
+ })
387
+ .join(',');
388
+ return "const defaultProps = {".concat(defalutPropsString, "};\n");
389
+ };
390
+ var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n import { ", " ", " Component ", "", " } from '@angular/core';\n ", "\n\n ", "\n ", "\n ", "\n\n @Component({\n ", "\n })\n export class ", " {\n ", "\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n }\n "], ["\n import { ", " ", " Component ", "", " } from '@angular/core';\n ", "\n\n ", "\n ", "\n ", "\n\n @Component({\n ", "\n })\n export class ", " {\n ", "\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n }\n "])), outputs.length ? 'Output, EventEmitter, \n' : '', ((_g = options === null || options === void 0 ? void 0 : options.experimental) === null || _g === void 0 ? void 0 : _g.inject) ? 'Inject, forwardRef,' : '', domRefs.size ? ', ViewChild, ElementRef' : '', props.size ? ', Input' : '', options.standalone ? "import { CommonModule } from '@angular/common';" : '', json.types ? json.types.join('\n') : '', getPropsDefinition({ json: json }), (0, render_imports_1.renderPreComponent)({
352
391
  component: json,
353
392
  target: 'angular',
354
393
  excludeMitosisComponents: !options.standalone && !options.preserveImports,
@@ -377,13 +416,13 @@ var componentToAngular = function (userOptions) {
377
416
  var argument = json.refs[ref].argument;
378
417
  var typeParameter = json.refs[ref].typeParameter;
379
418
  return "private _".concat(ref).concat(typeParameter ? ": ".concat(typeParameter) : '').concat(argument
380
- ? " = ".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(argument, {
419
+ ? " = ".concat(processAngularCode({
381
420
  replaceWith: 'this.',
382
421
  contextVars: contextVars,
383
422
  outputVars: outputVars,
384
423
  domRefs: Array.from(domRefs),
385
424
  stateVars: stateVars,
386
- }))
425
+ })(argument))
387
426
  : '', ";");
388
427
  })
389
428
  .join('\n'), !hasConstructor
@@ -55,6 +55,7 @@ var strip_meta_properties_1 = require("../helpers/strip-meta-properties");
55
55
  var remove_surrounding_block_1 = require("../helpers/remove-surrounding-block");
56
56
  var render_imports_1 = require("../helpers/render-imports");
57
57
  var for_1 = require("../helpers/nodes/for");
58
+ var function_1 = require("fp-ts/lib/function");
58
59
  var isAttribute = function (key) {
59
60
  return /-/.test(key);
60
61
  };
@@ -157,6 +158,22 @@ var createGlobalId = function (name, options) {
157
158
  options.namesMap[name] = newNameNum;
158
159
  return "".concat(name).concat(options.prefix ? "-".concat(options.prefix) : '', "-").concat(newNameNum);
159
160
  };
161
+ var deprecatedStripStateAndPropsRefs = function (code, _a) {
162
+ var context = _a.context, contextVars = _a.contextVars, domRefs = _a.domRefs, includeProps = _a.includeProps, includeState = _a.includeState, outputVars = _a.outputVars, replaceWith = _a.replaceWith, stateVars = _a.stateVars;
163
+ return (0, function_1.pipe)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
164
+ includeProps: includeProps,
165
+ includeState: includeState,
166
+ replaceWith: replaceWith,
167
+ }), function (newCode) {
168
+ return (0, strip_state_and_props_refs_1.DO_NOT_USE_VARS_TRANSFORMS)(newCode, {
169
+ context: context,
170
+ contextVars: contextVars,
171
+ domRefs: domRefs,
172
+ outputVars: outputVars,
173
+ stateVars: stateVars,
174
+ });
175
+ });
176
+ };
160
177
  // TODO: overloaded function
161
178
  var updateReferencesInCode = function (code, options, blockOptions) {
162
179
  var _a, _b;
@@ -165,23 +182,22 @@ var updateReferencesInCode = function (code, options, blockOptions) {
165
182
  var context = (blockOptions === null || blockOptions === void 0 ? void 0 : blockOptions.context) || 'this.';
166
183
  if ((_a = options === null || options === void 0 ? void 0 : options.experimental) === null || _a === void 0 ? void 0 : _a.updateReferencesInCode) {
167
184
  return (_b = options === null || options === void 0 ? void 0 : options.experimental) === null || _b === void 0 ? void 0 : _b.updateReferencesInCode(code, options, {
168
- stripStateAndPropsRefs: strip_state_and_props_refs_1.stripStateAndPropsRefs,
185
+ stripStateAndPropsRefs: deprecatedStripStateAndPropsRefs,
169
186
  });
170
187
  }
171
188
  if (options.format === 'class') {
172
- return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
189
+ return (0, function_1.pipe)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
173
190
  includeProps: false,
174
191
  includeState: true,
175
192
  replaceWith: context + 'state.',
176
- context: context,
177
- }), {
178
- // TODO: replace with `this.` and add setters that call this.update()
179
- includeProps: true,
180
- includeState: false,
181
- replaceWith: context + 'props.',
182
- contextVars: contextVars,
183
- context: context,
184
- });
193
+ }), function (newCode) {
194
+ return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(newCode, {
195
+ // TODO: replace with `this.` and add setters that call this.update()
196
+ includeProps: true,
197
+ includeState: false,
198
+ replaceWith: context + 'props.',
199
+ });
200
+ }, function (newCode) { return (0, strip_state_and_props_refs_1.DO_NOT_USE_CONTEXT_VARS_TRANSFORMS)({ code: newCode, context: context, contextVars: contextVars }); });
185
201
  }
186
202
  return code;
187
203
  };
@@ -598,19 +614,25 @@ var componentToCustomElement = function (options) {
598
614
  ? "this.context = ".concat(setContext[0].ref)
599
615
  : '', "\n\n ").concat(!((_o = (_m = json.hooks) === null || _m === void 0 ? void 0 : _m.onInit) === null || _o === void 0 ? void 0 : _o.code) ? '' : 'this.onInitOnce = false;', "\n\n this.state = ").concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
600
616
  valueMapper: function (value) {
601
- return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(addUpdateAfterSetInCode(value, useOptions, 'self.update'), {
617
+ return (0, function_1.pipe)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(addUpdateAfterSetInCode(value, useOptions, 'self.update'), {
602
618
  includeProps: false,
603
619
  includeState: true,
604
620
  // TODO: if it's an arrow function it's this.state.
605
621
  replaceWith: 'self.state.',
606
- }), {
607
- // TODO: replace with `this.` and add setters that call this.update()
608
- includeProps: true,
609
- includeState: false,
610
- replaceWith: 'self.props.',
611
- contextVars: contextVars,
612
- // correctly ref the class not state object
613
- context: 'self.',
622
+ }), function (newCode) {
623
+ return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(newCode, {
624
+ // TODO: replace with `this.` and add setters that call this.update()
625
+ includeProps: true,
626
+ includeState: false,
627
+ replaceWith: 'self.props.',
628
+ });
629
+ }, function (code) {
630
+ return (0, strip_state_and_props_refs_1.DO_NOT_USE_CONTEXT_VARS_TRANSFORMS)({
631
+ code: code,
632
+ contextVars: contextVars,
633
+ // correctly ref the class not state object
634
+ context: 'self.',
635
+ });
614
636
  });
615
637
  },
616
638
  }), ";\n if (!this.props) {\n this.props = {};\n }\n ").concat(!componentHasProps
@@ -108,21 +108,27 @@ var blockToMarko = function (json, options) {
108
108
  };
109
109
  function processBinding(json, code, type) {
110
110
  if (type === void 0) { type = 'attribute'; }
111
- return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
112
- replaceWith: type === 'state' ? 'input.' : type === 'class' ? 'this.input.' : 'input.',
113
- includeProps: true,
114
- includeState: false,
115
- }), {
116
- replaceWith: function (key) {
117
- var isProperty = getStatePropertyNames(json).includes(key);
118
- if (isProperty) {
119
- return (type === 'state' || type === 'class' ? 'this.state.' : 'state.') + key;
120
- }
121
- return (type === 'class' || type === 'state' ? 'this.' : 'component.') + key;
122
- },
123
- includeProps: false,
124
- includeState: true,
125
- });
111
+ try {
112
+ return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
113
+ replaceWith: type === 'state' ? 'input.' : type === 'class' ? 'this.input.' : 'input.',
114
+ includeProps: true,
115
+ includeState: false,
116
+ }), {
117
+ replaceWith: function (key) {
118
+ var isProperty = getStatePropertyNames(json).includes(key);
119
+ if (isProperty) {
120
+ return (type === 'state' || type === 'class' ? 'this.state.' : 'state.') + key;
121
+ }
122
+ return (type === 'class' || type === 'state' ? 'this.' : 'component.') + key;
123
+ },
124
+ includeProps: false,
125
+ includeState: true,
126
+ });
127
+ }
128
+ catch (error) {
129
+ console.error('Marko: could not process binding', code);
130
+ return code;
131
+ }
126
132
  }
127
133
  var componentToMarko = function (userOptions) {
128
134
  if (userOptions === void 0) { userOptions = {}; }
@@ -1,8 +1,9 @@
1
1
  import { BaseTranspilerOptions, TranspilerGenerator } from '../types/transpiler';
2
+ import { MitosisComponent } from '../types/mitosis-component';
2
3
  import { MitosisNode } from '../types/mitosis-node';
3
4
  export interface ToMitosisOptions extends BaseTranspilerOptions {
4
5
  format: 'react' | 'legacy';
5
6
  }
6
7
  export declare const DEFAULT_FORMAT: ToMitosisOptions['format'];
7
- export declare const blockToMitosis: (json: MitosisNode, toMitosisOptions?: Partial<ToMitosisOptions>) => string;
8
+ export declare const blockToMitosis: (json: MitosisNode, toMitosisOptions: Partial<ToMitosisOptions> | undefined, component: MitosisComponent) => string;
8
9
  export declare const componentToMitosis: TranspilerGenerator<Partial<ToMitosisOptions>>;
@@ -46,7 +46,7 @@ exports.DEFAULT_FORMAT = 'legacy';
46
46
  var isValidAttributeName = function (str) {
47
47
  return Boolean(str && /^[$a-z0-9\-_:]+$/i.test(str));
48
48
  };
49
- var blockToMitosis = function (json, toMitosisOptions) {
49
+ var blockToMitosis = function (json, toMitosisOptions, component) {
50
50
  var _a, _b, _c, _d, _e, _f;
51
51
  if (toMitosisOptions === void 0) { toMitosisOptions = {}; }
52
52
  var options = __assign({ format: exports.DEFAULT_FORMAT }, toMitosisOptions);
@@ -56,11 +56,11 @@ var blockToMitosis = function (json, toMitosisOptions) {
56
56
  stateType: 'useState',
57
57
  stylesType: 'emotion',
58
58
  prettier: options.prettier,
59
- });
59
+ }, component);
60
60
  }
61
61
  if ((0, mitosis_node_1.checkIsForNode)(json)) {
62
62
  var needsWrapper = json.children.length !== 1;
63
- return "<For each={".concat((_a = json.bindings.each) === null || _a === void 0 ? void 0 : _a.code, "}>\n {(").concat(json.scope.forName, ", index) =>\n ").concat(needsWrapper ? '<>' : '', "\n ").concat(json.children.map(function (child) { return (0, exports.blockToMitosis)(child, options); }), "}\n ").concat(needsWrapper ? '</>' : '', "\n </For>");
63
+ return "<For each={".concat((_a = json.bindings.each) === null || _a === void 0 ? void 0 : _a.code, "}>\n {(").concat(json.scope.forName, ", index) =>\n ").concat(needsWrapper ? '<>' : '', "\n ").concat(json.children.map(function (child) { return (0, exports.blockToMitosis)(child, options, component); }), "}\n ").concat(needsWrapper ? '</>' : '', "\n </For>");
64
64
  }
65
65
  if (json.properties._text) {
66
66
  return json.properties._text;
@@ -106,7 +106,7 @@ var blockToMitosis = function (json, toMitosisOptions) {
106
106
  }
107
107
  str += '>';
108
108
  if (json.children) {
109
- str += json.children.map(function (item) { return (0, exports.blockToMitosis)(item, options); }).join('\n');
109
+ str += json.children.map(function (item) { return (0, exports.blockToMitosis)(item, options, component); }).join('\n');
110
110
  }
111
111
  str += "</".concat(json.name, ">");
112
112
  return str;
@@ -159,7 +159,7 @@ var componentToMitosis = function (toMitosisOptions) {
159
159
  ? ''
160
160
  : "import { ".concat(!hasState ? '' : 'useStore, ', " ").concat(!refs.length ? '' : 'useRef, ', " ").concat(mitosisComponents.join(', '), " } from '@builder.io/mitosis';"), !otherComponents.length ? '' : "import { ".concat(otherComponents.join(','), " } from '@components';"), json.types ? json.types.join('\n') : '', (0, render_imports_1.renderPreComponent)({ component: json, target: 'mitosis' }), stringifiedUseMetadata && stringifiedUseMetadata !== '{}'
161
161
  ? "".concat(jsx_1.METADATA_HOOK_NAME, "(").concat(stringifiedUseMetadata, ")")
162
- : '', component.name, !hasState ? '' : "const state = useStore(".concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json), ");"), getRefsString(json, refs), !((_b = json.hooks.onMount) === null || _b === void 0 ? void 0 : _b.code) ? '' : "onMount(() => { ".concat(json.hooks.onMount.code, " })"), !((_c = json.hooks.onUnMount) === null || _c === void 0 ? void 0 : _c.code) ? '' : "onUnMount(() => { ".concat(json.hooks.onUnMount.code, " })"), addWrapper ? '<>' : '', json.children.map(function (item) { return (0, exports.blockToMitosis)(item, options); }).join('\n'), addWrapper ? '</>' : '');
162
+ : '', component.name, !hasState ? '' : "const state = useStore(".concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json), ");"), getRefsString(json, refs), !((_b = json.hooks.onMount) === null || _b === void 0 ? void 0 : _b.code) ? '' : "onMount(() => { ".concat(json.hooks.onMount.code, " })"), !((_c = json.hooks.onUnMount) === null || _c === void 0 ? void 0 : _c.code) ? '' : "onUnMount(() => { ".concat(json.hooks.onUnMount.code, " })"), addWrapper ? '<>' : '', json.children.map(function (item) { return (0, exports.blockToMitosis)(item, options, component); }).join('\n'), addWrapper ? '</>' : '');
163
163
  if (options.prettier !== false) {
164
164
  try {
165
165
  str = (0, standalone_1.format)(str, {
@@ -358,6 +358,8 @@ var SrcBuilder = /** @class */ (function () {
358
358
  if (value) {
359
359
  if (key === 'innerHTML')
360
360
  key = 'dangerouslySetInnerHTML';
361
+ if (key === 'for')
362
+ key = 'htmlFor';
361
363
  if (key === 'dataSet')
362
364
  return; // ignore
363
365
  if (self.isJSX) {
@@ -1,7 +1,8 @@
1
1
  import { TranspilerGenerator } from '../../types/transpiler';
2
+ import { MitosisComponent } from '../../types/mitosis-component';
2
3
  import { MitosisNode } from '../../types/mitosis-node';
3
4
  import { ToReactOptions } from './types';
4
5
  export declare const contextPropDrillingKey = "_context";
5
- export declare const blockToReact: (json: MitosisNode, options: ToReactOptions, parentSlots?: any[]) => string;
6
+ export declare const blockToReact: (json: MitosisNode, options: ToReactOptions, component: MitosisComponent, parentSlots?: any[]) => string;
6
7
  export declare const componentToPreact: TranspilerGenerator<ToReactOptions>;
7
8
  export declare const componentToReact: TranspilerGenerator<ToReactOptions>;
@@ -67,50 +67,90 @@ var isRootSpecialNode = function (json) {
67
67
  };
68
68
  var wrapInFragment = function (json) { return json.children.length !== 1; };
69
69
  var NODE_MAPPERS = {
70
- Slot: function (json, options, parentSlots) {
70
+ Slot: function (json, options, component, parentSlots) {
71
71
  var _a;
72
- if (!json.bindings.name) {
72
+ var _b, _c;
73
+ var slotName = ((_b = json.bindings.name) === null || _b === void 0 ? void 0 : _b.code) || json.properties.name;
74
+ var hasChildren = json.children.length;
75
+ var renderChildren = function () {
76
+ var _a;
77
+ 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();
78
+ /**
79
+ * Ad-hoc way of figuring out if the children defaultProp is:
80
+ * - a JSX element, e.g. `<div>foo</div>`
81
+ * - a JS expression, e.g. `true`, `false`
82
+ * - a string, e.g. `'Default text'`
83
+ *
84
+ * and correctly wrapping it in quotes when appropriate.
85
+ */
86
+ if (childrenStr.startsWith("<") && childrenStr.endsWith(">")) {
87
+ return childrenStr;
88
+ }
89
+ else if (['false', 'true', 'null', 'undefined'].includes(childrenStr)) {
90
+ return childrenStr;
91
+ }
92
+ else {
93
+ return "\"".concat(childrenStr, "\"");
94
+ }
95
+ };
96
+ if (!slotName) {
73
97
  // TODO: update MitosisNode for simple code
74
98
  var key = Object.keys(json.bindings).find(Boolean);
75
99
  if (key && parentSlots) {
76
100
  var propKey = (0, lodash_1.camelCase)('Slot' + key[0].toUpperCase() + key.substring(1));
77
- parentSlots.push({ key: propKey, value: (_a = json.bindings[key]) === null || _a === void 0 ? void 0 : _a.code });
101
+ parentSlots.push({ key: propKey, value: (_c = json.bindings[key]) === null || _c === void 0 ? void 0 : _c.code });
78
102
  return '';
79
103
  }
104
+ if (hasChildren) {
105
+ component.defaultProps = __assign(__assign({}, (component.defaultProps || {})), { children: {
106
+ code: renderChildren(),
107
+ type: 'property',
108
+ } });
109
+ }
80
110
  return "{".concat((0, helpers_2.processBinding)('props.children', options), "}");
81
111
  }
82
- var slotProp = (0, helpers_2.processBinding)(json.bindings.name.code, options).replace('name=', '');
112
+ var slotProp = (0, helpers_2.processBinding)(slotName, options).replace('name=', '');
113
+ if (!slotProp.startsWith('props.slot')) {
114
+ slotProp = "props.slot".concat((0, lodash_1.upperFirst)((0, lodash_1.camelCase)(slotProp)));
115
+ }
116
+ if (hasChildren) {
117
+ component.defaultProps = __assign(__assign({}, (component.defaultProps || {})), (_a = {}, _a[slotProp.replace('props.', '')] = {
118
+ code: renderChildren(),
119
+ type: 'property',
120
+ }, _a));
121
+ }
83
122
  return "{".concat(slotProp, "}");
84
123
  },
85
- Fragment: function (json, options) {
124
+ Fragment: function (json, options, component) {
86
125
  var wrap = wrapInFragment(json);
87
126
  return "".concat(wrap ? getFragment('open', options) : '').concat(json.children
88
- .map(function (item) { return (0, exports.blockToReact)(item, options); })
127
+ .map(function (item) { return (0, exports.blockToReact)(item, options, component); })
89
128
  .join('\n')).concat(wrap ? getFragment('close', options) : '');
90
129
  },
91
- For: function (_json, options) {
130
+ For: function (_json, options, component) {
92
131
  var _a;
93
132
  var json = _json;
94
133
  var wrap = wrapInFragment(json);
95
134
  var forArguments = (0, for_1.getForArguments)(json).join(', ');
96
135
  return "{".concat((0, helpers_2.processBinding)((_a = json.bindings.each) === null || _a === void 0 ? void 0 : _a.code, options), "?.map((").concat(forArguments, ") => (\n ").concat(wrap ? openFrag(options) : '').concat(json.children
97
136
  .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
98
- .map(function (item) { return (0, exports.blockToReact)(item, options); })
137
+ .map(function (item) { return (0, exports.blockToReact)(item, options, component); })
99
138
  .join('\n')).concat(wrap ? closeFrag(options) : '', "\n ))}");
100
139
  },
101
- Show: function (json, options) {
140
+ Show: function (json, options, component) {
102
141
  var _a;
103
142
  var wrap = wrapInFragment(json);
104
143
  return "{".concat((0, helpers_2.processBinding)((_a = json.bindings.when) === null || _a === void 0 ? void 0 : _a.code, options), " ? (\n ").concat(wrap ? openFrag(options) : '').concat(json.children
105
144
  .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
106
- .map(function (item) { return (0, exports.blockToReact)(item, options); })
107
- .join('\n')).concat(wrap ? closeFrag(options) : '', "\n ) : ").concat(!json.meta.else ? 'null' : (0, exports.blockToReact)(json.meta.else, options), "}");
145
+ .map(function (item) { return (0, exports.blockToReact)(item, options, component); })
146
+ .join('\n')).concat(wrap ? closeFrag(options) : '', "\n ) : ").concat(!json.meta.else ? 'null' : (0, exports.blockToReact)(json.meta.else, options, component), "}");
108
147
  },
109
148
  };
110
149
  var ATTTRIBUTE_MAPPERS = {
111
150
  spellcheck: 'spellCheck',
112
151
  autocapitalize: 'autoCapitalize',
113
152
  autocomplete: 'autoComplete',
153
+ for: 'htmlFor',
114
154
  };
115
155
  // TODO: Maybe in the future allow defining `string | function` as values
116
156
  var BINDING_MAPPERS = __assign({ ref: function (ref, value, options) {
@@ -126,10 +166,10 @@ var BINDING_MAPPERS = __assign({ ref: function (ref, value, options) {
126
166
  }, innerHTML: function (_key, value) {
127
167
  return ['dangerouslySetInnerHTML', "{__html: ".concat(value.replace(/\s+/g, ' '), "}")];
128
168
  } }, ATTTRIBUTE_MAPPERS);
129
- var blockToReact = function (json, options, parentSlots) {
169
+ var blockToReact = function (json, options, component, parentSlots) {
130
170
  var _a, _b, _c;
131
171
  if (NODE_MAPPERS[json.name]) {
132
- return NODE_MAPPERS[json.name](json, options, parentSlots);
172
+ return NODE_MAPPERS[json.name](json, options, component, parentSlots);
133
173
  }
134
174
  if (json.properties._text) {
135
175
  var text = json.properties._text;
@@ -150,7 +190,7 @@ var blockToReact = function (json, options, parentSlots) {
150
190
  for (var key in json.properties) {
151
191
  var value = (json.properties[key] || '').replace(/"/g, '&quot;').replace(/\n/g, '\\n');
152
192
  if (key === 'class') {
153
- str += " className=\"".concat(value, "\" ");
193
+ str = "".concat(str.trim(), " className=\"").concat(value, "\" ");
154
194
  }
155
195
  else if (BINDING_MAPPERS[key]) {
156
196
  var mapper = BINDING_MAPPERS[key];
@@ -217,7 +257,7 @@ var blockToReact = function (json, options, parentSlots) {
217
257
  var childrenNodes = '';
218
258
  if (json.children) {
219
259
  childrenNodes = json.children
220
- .map(function (item) { return (0, exports.blockToReact)(item, options, needsToRenderSlots); })
260
+ .map(function (item) { return (0, exports.blockToReact)(item, options, component, needsToRenderSlots); })
221
261
  .join('\n');
222
262
  }
223
263
  if (needsToRenderSlots.length) {
@@ -226,7 +266,7 @@ var blockToReact = function (json, options, parentSlots) {
226
266
  str += " ".concat(key, "={").concat(value, "} ");
227
267
  });
228
268
  }
229
- str += '>';
269
+ str = str.trim() + '>';
230
270
  if (json.children) {
231
271
  str += childrenNodes;
232
272
  }
@@ -428,6 +468,21 @@ var _componentToReact = function (json, options, isSubComponent) {
428
468
  var nativeStyles = stylesType === 'react-native' && componentHasStyles && (0, react_native_1.collectReactNativeStyles)(json);
429
469
  var propType = json.propsTypeRef || 'any';
430
470
  var propsArgs = "props".concat(options.typescript ? ":".concat(propType) : '');
471
+ var getPropsDefinition = function (_a) {
472
+ var json = _a.json;
473
+ if (!json.defaultProps)
474
+ return '';
475
+ var defalutPropsString = Object.keys(json.defaultProps)
476
+ .map(function (prop) {
477
+ var _a;
478
+ var value = json.defaultProps.hasOwnProperty(prop)
479
+ ? (_a = json.defaultProps[prop]) === null || _a === void 0 ? void 0 : _a.code
480
+ : '{}';
481
+ return "".concat(prop, ": ").concat(value);
482
+ })
483
+ .join(',');
484
+ return "".concat(json.name || 'MyComponent', ".defaultProps = {").concat(defalutPropsString, "};");
485
+ };
431
486
  var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "", "", "function ", "(", "", ") {\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 ", "", "", "function ", "(", "", ") {\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 "])), options.preact
432
487
  ? "\n /** @jsx h */\n import { h, Fragment } from 'preact';\n "
433
488
  : options.type !== 'native'
@@ -472,13 +527,11 @@ var _componentToReact = function (json, options, isSubComponent) {
472
527
  str: json.hooks.onUnMount.code,
473
528
  options: options,
474
529
  }), "\n }\n }, [])")
475
- : '', wrap ? openFrag(options) : '', json.children.map(function (item) { return (0, exports.blockToReact)(item, options); }).join('\n'), componentHasStyles && stylesType === 'styled-jsx'
530
+ : '', wrap ? openFrag(options) : '', json.children.map(function (item) { return (0, exports.blockToReact)(item, options, json, []); }).join('\n'), componentHasStyles && stylesType === 'styled-jsx'
476
531
  ? "<style jsx>{`".concat(css, "`}</style>")
477
532
  : componentHasStyles && stylesType === 'style-tag'
478
533
  ? "<style>{`".concat(css, "`}</style>")
479
- : '', wrap ? closeFrag(options) : '', isForwardRef ? ')' : '', !json.defaultProps
480
- ? ''
481
- : "".concat(json.name || 'MyComponent', ".defaultProps = ").concat(json5_1.default.stringify(json.defaultProps), ";"), !nativeStyles
534
+ : '', wrap ? closeFrag(options) : '', isForwardRef ? ')' : '', getPropsDefinition({ json: json }), !nativeStyles
482
535
  ? ''
483
536
  : "\n const styles = StyleSheet.create(".concat(json5_1.default.stringify(nativeStyles), ");\n "), styledComponentsCode ? styledComponentsCode : '', stateType === 'mobx'
484
537
  ? "\n const observed".concat(json.name || 'MyComponent', " = observer(").concat(json.name || 'MyComponent', ");\n export default observed").concat(json.name || 'MyComponent', ";\n ")