@builder.io/mitosis 0.0.80 → 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 (49) 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/helpers/context.d.ts +2 -0
  4. package/dist/src/generators/helpers/context.js +10 -2
  5. package/dist/src/generators/html.js +42 -20
  6. package/dist/src/generators/marko/generate.js +21 -15
  7. package/dist/src/generators/mitosis.d.ts +2 -1
  8. package/dist/src/generators/mitosis.js +5 -5
  9. package/dist/src/generators/qwik/component-generator.js +1 -1
  10. package/dist/src/generators/qwik/src-generator.js +2 -0
  11. package/dist/src/generators/react/generator.d.ts +2 -1
  12. package/dist/src/generators/react/generator.js +73 -20
  13. package/dist/src/generators/solid/index.js +17 -7
  14. package/dist/src/generators/svelte/blocks.js +15 -6
  15. package/dist/src/generators/svelte/svelte.js +15 -11
  16. package/dist/src/generators/vue/blocks.js +12 -6
  17. package/dist/src/generators/vue/compositionApi.js +16 -2
  18. package/dist/src/generators/vue/helpers.js +45 -22
  19. package/dist/src/generators/vue/optionsApi.js +25 -15
  20. package/dist/src/generators/vue/vue.js +5 -2
  21. package/dist/src/helpers/babel-transform.js +30 -19
  22. package/dist/src/helpers/patterns.d.ts +1 -0
  23. package/dist/src/helpers/patterns.js +5 -3
  24. package/dist/src/helpers/plugins/process-code.js +6 -6
  25. package/dist/src/helpers/process-http-requests.js +3 -3
  26. package/dist/src/helpers/replace-identifiers.d.ts +5 -3
  27. package/dist/src/helpers/replace-identifiers.js +124 -21
  28. package/dist/src/helpers/slots.js +3 -1
  29. package/dist/src/helpers/strip-state-and-props-refs.d.ts +23 -4
  30. package/dist/src/helpers/strip-state-and-props-refs.js +57 -53
  31. package/dist/src/helpers/styles/collect-css.js +2 -2
  32. package/dist/src/helpers/styles/helpers.js +10 -11
  33. package/dist/src/helpers/typescript.d.ts +1 -0
  34. package/dist/src/helpers/typescript.js +3 -0
  35. package/dist/src/parsers/jsx/element-parser.js +3 -2
  36. package/dist/src/parsers/jsx/function-parser.js +3 -10
  37. package/dist/src/parsers/jsx/helpers.d.ts +4 -0
  38. package/dist/src/parsers/jsx/helpers.js +11 -1
  39. package/dist/src/parsers/jsx/state.d.ts +1 -1
  40. package/dist/src/parsers/jsx/state.js +85 -39
  41. package/dist/src/parsers/svelte/helpers/post-process.d.ts +10 -5
  42. package/dist/src/parsers/svelte/helpers/post-process.js +51 -16
  43. package/dist/src/parsers/svelte/html/element.js +29 -11
  44. package/dist/src/parsers/svelte/html/slot.js +3 -8
  45. package/dist/src/parsers/svelte/instance/reactive.js +10 -1
  46. package/dist/src/parsers/svelte/instance/references.js +2 -2
  47. package/dist/src/types/mitosis-component.d.ts +1 -1
  48. package/dist/tsconfig.build.tsbuildinfo +1 -1
  49. package/package.json +1 -1
@@ -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 ")
@@ -66,7 +66,6 @@ var traverse_1 = __importDefault(require("traverse"));
66
66
  var is_mitosis_node_1 = require("../../helpers/is-mitosis-node");
67
67
  var filter_empty_text_nodes_1 = require("../../helpers/filter-empty-text-nodes");
68
68
  var create_mitosis_node_1 = require("../../helpers/create-mitosis-node");
69
- var context_1 = require("../helpers/context");
70
69
  var babel_transform_1 = require("../../helpers/babel-transform");
71
70
  var core_1 = require("@babel/core");
72
71
  var lodash_1 = require("lodash");
@@ -80,12 +79,14 @@ var S = __importStar(require("fp-ts/string"));
80
79
  var helpers_2 = require("./state/helpers");
81
80
  var merge_options_1 = require("../../helpers/merge-options");
82
81
  var process_code_1 = require("../../helpers/plugins/process-code");
82
+ var context_1 = require("../helpers/context");
83
+ var typescript_1 = require("../../helpers/typescript");
83
84
  // Transform <foo.bar key="value" /> to <component :is="foo.bar" key="value" />
84
85
  function processDynamicComponents(json, options) {
85
86
  var found = false;
86
87
  (0, traverse_1.default)(json).forEach(function (node) {
87
88
  if ((0, is_mitosis_node_1.isMitosisNode)(node)) {
88
- if (node.name.includes('.')) {
89
+ if (node.name.includes('.') && !node.name.endsWith('.Provider')) {
89
90
  node.bindings.component = { code: node.name };
90
91
  node.name = 'Dynamic';
91
92
  found = true;
@@ -163,6 +164,14 @@ var preProcessBlockCode = function (_a) {
163
164
  }
164
165
  }
165
166
  };
167
+ var ATTTRIBUTE_MAPPERS = {
168
+ for: 'htmlFor',
169
+ };
170
+ var transformAttributeName = function (name) {
171
+ if ((0, typescript_1.objectHasKey)(ATTTRIBUTE_MAPPERS, name))
172
+ return ATTTRIBUTE_MAPPERS[name];
173
+ return name;
174
+ };
166
175
  var blockToSolid = function (_a) {
167
176
  var _b, _c;
168
177
  var json = _a.json, options = _a.options, component = _a.component;
@@ -196,7 +205,8 @@ var blockToSolid = function (_a) {
196
205
  }
197
206
  for (var key in json.properties) {
198
207
  var value = json.properties[key];
199
- str += " ".concat(key, "=\"").concat(value, "\" ");
208
+ var newKey = transformAttributeName(key);
209
+ str += " ".concat(newKey, "=\"").concat(value, "\" ");
200
210
  }
201
211
  for (var key in json.bindings) {
202
212
  var _d = json.bindings[key], code = _d.code, _e = _d.arguments, cusArg = _e === void 0 ? ['event'] : _e, type = _d.type;
@@ -234,7 +244,8 @@ var blockToSolid = function (_a) {
234
244
  },
235
245
  });
236
246
  }
237
- str += " ".concat(key, "={").concat(useValue, "} ");
247
+ var newKey = transformAttributeName(key);
248
+ str += " ".concat(newKey, "={").concat(useValue, "} ");
238
249
  }
239
250
  }
240
251
  if (jsx_1.selfClosingTags.has(json.name)) {
@@ -317,17 +328,16 @@ var componentToSolid = function (passedOptions) {
317
328
  });
318
329
  var state = (0, state_1.getState)({ json: json, options: options });
319
330
  var componentsUsed = (0, get_components_used_1.getComponentsUsed)(json);
320
- var componentHasContext = (0, context_1.hasContext)(json);
321
331
  var hasShowComponent = componentsUsed.has('Show');
322
332
  var hasForComponent = componentsUsed.has('For');
323
333
  var solidJSImports = (0, Array_1.uniq)(S.Eq)(__spreadArray(__spreadArray([
324
- componentHasContext ? 'useContext' : undefined,
334
+ (0, context_1.hasGetContext)(json) ? 'useContext' : undefined,
325
335
  hasShowComponent ? 'Show' : undefined,
326
336
  hasForComponent ? 'For' : undefined,
327
337
  ((_b = json.hooks.onMount) === null || _b === void 0 ? void 0 : _b.code) ? 'onMount' : undefined
328
338
  ], (((_c = json.hooks.onUpdate) === null || _c === void 0 ? void 0 : _c.length) ? ['on', 'createEffect'] : []), true), ((_d = state === null || state === void 0 ? void 0 : state.import.solidjs) !== null && _d !== void 0 ? _d : []), true).filter(nullable_1.checkIsDefined));
329
339
  var storeImports = (_e = state === null || state === void 0 ? void 0 : state.import.store) !== null && _e !== void 0 ? _e : [];
330
- var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n ", "\n ", "\n ", "\n\n function ", "(props) {\n ", "\n \n ", "\n ", "\n\n ", "\n ", "\n\n return (", "\n ", "\n ", "\n ", ")\n }\n\n export default ", ";\n "], ["\n ", "\n ", "\n ", "\n ", "\n ", "\n\n function ", "(props) {\n ", "\n \n ", "\n ", "\n\n ", "\n ", "\n\n return (", "\n ", "\n ", "\n ", ")\n }\n\n export default ", ";\n "])), solidJSImports.length > 0 ? "import { ".concat(solidJSImports.join(', '), " } from 'solid-js';") : '', !foundDynamicComponents ? '' : "import { Dynamic } from 'solid-js/web';", storeImports.length > 0 ? "import { ".concat(storeImports.join(', '), " } from 'solid-js/store';") : '', !componentHasStyles && options.stylesType === 'styled-components'
340
+ var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n ", "\n ", "\n ", "\n\n function ", "(props) {\n ", "\n\n ", "\n ", "\n\n ", "\n ", "\n\n return (", "\n ", "\n ", "\n ", ")\n }\n\n export default ", ";\n "], ["\n ", "\n ", "\n ", "\n ", "\n ", "\n\n function ", "(props) {\n ", "\n\n ", "\n ", "\n\n ", "\n ", "\n\n return (", "\n ", "\n ", "\n ", ")\n }\n\n export default ", ";\n "])), solidJSImports.length > 0 ? "import { ".concat(solidJSImports.join(', '), " } from 'solid-js';") : '', !foundDynamicComponents ? '' : "import { Dynamic } from 'solid-js/web';", storeImports.length > 0 ? "import { ".concat(storeImports.join(', '), " } from 'solid-js/store';") : '', !componentHasStyles && options.stylesType === 'styled-components'
331
341
  ? ''
332
342
  : "import { css } from \"solid-styled-components\";", (0, render_imports_1.renderPreComponent)({ component: json, target: 'solid' }), json.name, (_f = state === null || state === void 0 ? void 0 : state.str) !== null && _f !== void 0 ? _f : '', getRefsString(json), getContextString(json, options), !((_g = json.hooks.onMount) === null || _g === void 0 ? void 0 : _g.code) ? '' : "onMount(() => { ".concat(json.hooks.onMount.code, " })"), json.hooks.onUpdate
333
343
  ? json.hooks.onUpdate
@@ -53,15 +53,24 @@ var mappers = {
53
53
  : '', "\n{/if}");
54
54
  },
55
55
  Slot: function (_a) {
56
- var _b, _c;
56
+ var _b, _c, _d;
57
57
  var json = _a.json, options = _a.options, parentComponent = _a.parentComponent;
58
- if (!json.bindings.name) {
58
+ var slotName = ((_b = json.bindings.name) === null || _b === void 0 ? void 0 : _b.code) || json.properties.name;
59
+ var renderChildren = function () {
60
+ var _a;
61
+ return (_a = json.children) === null || _a === void 0 ? void 0 : _a.map(function (item) { return (0, exports.blockToSvelte)({ json: item, options: options, parentComponent: parentComponent }); }).join('\n');
62
+ };
63
+ if (!slotName) {
59
64
  var key = Object.keys(json.bindings).find(Boolean);
60
- if (!key)
61
- return '<slot />';
62
- return "\n <span #".concat(key, ">\n ").concat((_b = json.bindings[key]) === null || _b === void 0 ? void 0 : _b.code, "\n </span>\n ");
65
+ if (!key) {
66
+ if (!((_c = json.children) === null || _c === void 0 ? void 0 : _c.length)) {
67
+ return '<slot/>';
68
+ }
69
+ return "<slot>".concat(renderChildren(), "</slot>");
70
+ }
71
+ return "\n <span #".concat(key, ">\n ").concat((_d = json.bindings[key]) === null || _d === void 0 ? void 0 : _d.code, "\n </span>\n ");
63
72
  }
64
- return "<slot name=\"".concat((0, slots_1.stripSlotPrefix)(json.bindings.name.code).toLowerCase(), "\">").concat((_c = json.children) === null || _c === void 0 ? void 0 : _c.map(function (item) { return (0, exports.blockToSvelte)({ json: item, options: options, parentComponent: parentComponent }); }).join('\n'), "</slot>");
73
+ return "<slot name=\"".concat((0, slots_1.stripSlotPrefix)(slotName).toLowerCase(), "\">").concat(renderChildren(), "</slot>");
65
74
  },
66
75
  };
67
76
  var BINDINGS_MAPPER = {
@@ -35,7 +35,6 @@ var babel_transform_1 = require("../../helpers/babel-transform");
35
35
  var function_1 = require("fp-ts/lib/function");
36
36
  var context_1 = require("../helpers/context");
37
37
  var slots_1 = require("../../helpers/slots");
38
- var json5_1 = __importDefault(require("json5"));
39
38
  var functions_1 = require("../helpers/functions");
40
39
  var merge_options_1 = require("../../helpers/merge-options");
41
40
  var process_code_1 = require("../../helpers/plugins/process-code");
@@ -106,7 +105,7 @@ var DEFAULT_OPTIONS = {
106
105
  };
107
106
  var componentToSvelte = function (userProvidedOptions) {
108
107
  return function (_a) {
109
- var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
108
+ var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
110
109
  var component = _a.component;
111
110
  var options = (0, merge_options_1.mergeOptions)(DEFAULT_OPTIONS, userProvidedOptions);
112
111
  options.plugins = __spreadArray(__spreadArray([], (options.plugins || []), true), [
@@ -171,21 +170,25 @@ var componentToSvelte = function (userProvidedOptions) {
171
170
  if ((_d = (_c = json.hooks.onMount) === null || _c === void 0 ? void 0 : _c.code) === null || _d === void 0 ? void 0 : _d.length) {
172
171
  svelteImports.push('onMount');
173
172
  }
174
- if ((_e = json.hooks.onUpdate) === null || _e === void 0 ? void 0 : _e.length) {
173
+ if ((_f = (_e = json.hooks.onUpdate) === null || _e === void 0 ? void 0 : _e.filter(function (x) { return !x.deps; })) === null || _f === void 0 ? void 0 : _f.length) {
175
174
  svelteImports.push('afterUpdate');
176
175
  }
177
- if ((_g = (_f = json.hooks.onUnMount) === null || _f === void 0 ? void 0 : _f.code) === null || _g === void 0 ? void 0 : _g.length) {
176
+ if ((_h = (_g = json.hooks.onUnMount) === null || _g === void 0 ? void 0 : _g.code) === null || _h === void 0 ? void 0 : _h.length) {
178
177
  svelteImports.push('onDestroy');
179
178
  }
180
- if ((0, context_1.hasContext)(component)) {
181
- svelteImports.push('getContext', 'setContext');
179
+ if ((0, context_1.hasGetContext)(component)) {
180
+ svelteImports.push('getContext');
182
181
  }
183
- str += (0, dedent_1.default)(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n <script ", ">\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 </script>\n\n ", "\n\n ", "\n "], ["\n <script ", ">\n ", "\n ", "\n\n ", "\n\n ", "\n ", "\n "
182
+ if ((0, context_1.hasSetContext)(component)) {
183
+ svelteImports.push('setContext');
184
+ }
185
+ str += (0, dedent_1.default)(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n <script ", ">\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 </script>\n\n ", "\n\n ", "\n "], ["\n <script ", ">\n ", "\n ", "\n\n ", "\n\n ", "\n ", "\n "
184
186
  // https://svelte.dev/repl/bd9b56891f04414982517bbd10c52c82?version=3.31.0
185
- , "\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n ", "\n \n ", "\n\n ", "\n\n ", "\n </script>\n\n ", "\n\n ", "\n "])), tsLangAttribute, !svelteImports.length ? '' : "import { ".concat(svelteImports.sort().join(', '), " } from 'svelte'"), !svelteStoreImports.length
187
+ , "\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n ", "\n </script>\n\n ", "\n\n ", "\n "])), tsLangAttribute, !svelteImports.length ? '' : "import { ".concat(svelteImports.sort().join(', '), " } from 'svelte'"), !svelteStoreImports.length
186
188
  ? ''
187
189
  : "import { ".concat(svelteStoreImports.sort().join(', '), " } from 'svelte/store'"), (0, render_imports_1.renderPreComponent)({ component: json, target: 'svelte' }), !hasData || options.stateType === 'variables' ? '' : "import onChange from 'on-change'", props
188
190
  .map(function (name) {
191
+ var _a;
189
192
  if (name === 'children') {
190
193
  return '';
191
194
  }
@@ -194,7 +197,7 @@ var componentToSvelte = function (userProvidedOptions) {
194
197
  propDeclaration += ": ".concat(json.propsTypeRef.split(' |')[0], "['").concat(name, "']");
195
198
  }
196
199
  if (json.defaultProps && json.defaultProps.hasOwnProperty(name)) {
197
- propDeclaration += "=".concat(json5_1.default.stringify(json.defaultProps[name]));
200
+ propDeclaration += "=".concat((_a = json.defaultProps[name]) === null || _a === void 0 ? void 0 : _a.code);
198
201
  }
199
202
  propDeclaration += ';';
200
203
  return propDeclaration;
@@ -207,14 +210,14 @@ var componentToSvelte = function (userProvidedOptions) {
207
210
  ? dataString.length < 4
208
211
  ? ''
209
212
  : "let state = onChange(".concat(dataString, ", () => state = state)")
210
- : dataString, (_j = (_h = json.hooks.onInit) === null || _h === void 0 ? void 0 : _h.code) !== null && _j !== void 0 ? _j : '', !((_k = json.hooks.onMount) === null || _k === void 0 ? void 0 : _k.code) ? '' : "onMount(() => { ".concat(json.hooks.onMount.code, " });"), ((_l = json.hooks.onUpdate) === null || _l === void 0 ? void 0 : _l.map(function (_a, index) {
213
+ : dataString, (_k = (_j = json.hooks.onInit) === null || _j === void 0 ? void 0 : _j.code) !== null && _k !== void 0 ? _k : '', !((_l = json.hooks.onMount) === null || _l === void 0 ? void 0 : _l.code) ? '' : "onMount(() => { ".concat(json.hooks.onMount.code, " });"), ((_m = json.hooks.onUpdate) === null || _m === void 0 ? void 0 : _m.map(function (_a, index) {
211
214
  var code = _a.code, deps = _a.deps;
212
215
  if (!deps) {
213
216
  return "afterUpdate(() => { ".concat(code, " });");
214
217
  }
215
218
  var fnName = "onUpdateFn_".concat(index);
216
219
  return "\n function ".concat(fnName, "() {\n ").concat(code, "\n }\n $: ").concat(fnName, "(...").concat(deps, ")\n ");
217
- }).join(';')) || '', !((_m = json.hooks.onUnMount) === null || _m === void 0 ? void 0 : _m.code) ? '' : "onDestroy(() => { ".concat(json.hooks.onUnMount.code, " });"), json.children
220
+ }).join(';')) || '', !((_o = json.hooks.onUnMount) === null || _o === void 0 ? void 0 : _o.code) ? '' : "onDestroy(() => { ".concat(json.hooks.onUnMount.code, " });"), json.children
218
221
  .map(function (item) {
219
222
  return (0, blocks_1.blockToSvelte)({
220
223
  json: item,
@@ -245,6 +248,7 @@ var componentToSvelte = function (userProvidedOptions) {
245
248
  console.warn({ string: str }, err);
246
249
  }
247
250
  }
251
+ str = str.replace(/<script>\n<\/script>/g, '').trim();
248
252
  str = (0, plugins_1.runPostCodePlugins)(str, options.plugins);
249
253
  return str;
250
254
  };
@@ -173,14 +173,20 @@ var NODE_MAPPERS = {
173
173
  }
174
174
  },
175
175
  Slot: function (json, options) {
176
- var _a, _b;
177
- if (!json.bindings.name) {
176
+ var _a, _b, _c;
177
+ var slotName = ((_a = json.bindings.name) === null || _a === void 0 ? void 0 : _a.code) || json.properties.name;
178
+ var renderChildren = function () { var _a; return (_a = json.children) === null || _a === void 0 ? void 0 : _a.map(function (item) { return (0, exports.blockToVue)(item, options); }).join('\n'); };
179
+ if (!slotName) {
178
180
  var key = Object.keys(json.bindings).find(Boolean);
179
- if (!key)
180
- return '<slot />';
181
- return "\n <template #".concat(key, ">\n ").concat((_a = json.bindings[key]) === null || _a === void 0 ? void 0 : _a.code, "\n </template>\n ");
181
+ if (!key) {
182
+ if (!((_b = json.children) === null || _b === void 0 ? void 0 : _b.length)) {
183
+ return '<slot/>';
184
+ }
185
+ return "<slot>".concat(renderChildren(), "</slot>");
186
+ }
187
+ return "\n <template #".concat(key, ">\n ").concat((_c = json.bindings[key]) === null || _c === void 0 ? void 0 : _c.code, "\n </template>\n ");
182
188
  }
183
- return "<slot name=\"".concat((0, slots_1.stripSlotPrefix)(json.bindings.name.code).toLowerCase(), "\">").concat((_b = json.children) === null || _b === void 0 ? void 0 : _b.map(function (item) { return (0, exports.blockToVue)(item, options); }).join('\n'), "</slot>");
189
+ return "<slot name=\"".concat((0, slots_1.stripSlotPrefix)(slotName).toLowerCase(), "\">").concat(renderChildren(), "</slot>");
184
190
  },
185
191
  };
186
192
  var stringifyBinding = function (node) {
@@ -14,12 +14,22 @@ var lodash_1 = require("lodash");
14
14
  var get_state_object_string_1 = require("../../helpers/get-state-object-string");
15
15
  var helpers_1 = require("./helpers");
16
16
  var strip_state_and_props_refs_1 = require("../../helpers/strip-state-and-props-refs");
17
+ var helpers_2 = require("./helpers");
17
18
  var getCompositionPropDefinition = function (_a) {
18
19
  var options = _a.options, component = _a.component, props = _a.props;
19
20
  var str = 'const props = ';
20
21
  if (component.defaultProps) {
21
22
  var generic = options.typescript ? "<".concat(component.propsTypeRef, ">") : '';
22
- str += "withDefaults(defineProps".concat(generic, "(), ").concat(json5_1.default.stringify(component.defaultProps), ")");
23
+ var defalutPropsString = props
24
+ .map(function (prop) {
25
+ var _a;
26
+ var value = component.defaultProps.hasOwnProperty(prop)
27
+ ? (_a = component.defaultProps[prop]) === null || _a === void 0 ? void 0 : _a.code
28
+ : '{}';
29
+ return "".concat(prop, ": ").concat(value);
30
+ })
31
+ .join(',');
32
+ str += "withDefaults(defineProps".concat(generic, "(), {").concat(defalutPropsString, "})");
23
33
  }
24
34
  else if (options.typescript && component.propsTypeRef && component.propsTypeRef !== 'any') {
25
35
  str += "defineProps<".concat(component.propsTypeRef, ">()");
@@ -76,7 +86,11 @@ function generateCompositionApiScript(component, options, template, props, onUpd
76
86
  var computedCode = "const ".concat(key, " = computed(").concat(getterAsFunction, ")");
77
87
  return computedCode;
78
88
  }).join('\n')) || '', (onUpdateWithoutDeps === null || onUpdateWithoutDeps === void 0 ? void 0 : onUpdateWithoutDeps.map(function (hook) { return "onUpdated(() => ".concat(hook.code, ")"); }).join('\n')) || '', (onUpdateWithDeps === null || onUpdateWithDeps === void 0 ? void 0 : onUpdateWithDeps.map(function (hook) {
79
- return "watch(() => ".concat(hook.deps, ", (").concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(hook.deps), ") => { ").concat(hook.code, " })");
89
+ return "watch(() => ".concat((0, helpers_2.processBinding)({
90
+ code: hook.deps || '',
91
+ options: options,
92
+ json: component,
93
+ }), ", (").concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(hook.deps), ") => { ").concat(hook.code, " }, {immediate: true})");
80
94
  }).join('\n')) || '', methods !== null && methods !== void 0 ? methods : '');
81
95
  return str;
82
96
  }
@@ -28,6 +28,7 @@ var babel_transform_1 = require("../../helpers/babel-transform");
28
28
  var core_1 = require("@babel/core");
29
29
  var lodash_1 = require("lodash");
30
30
  var patterns_1 = require("../../helpers/patterns");
31
+ var replace_identifiers_1 = require("../../helpers/replace-identifiers");
31
32
  var addPropertiesToJson = function (properties) {
32
33
  return function (json) { return (__assign(__assign({}, json), { properties: __assign(__assign({}, json.properties), properties) })); };
33
34
  };
@@ -101,32 +102,54 @@ function processRefs(input, component, options) {
101
102
  },
102
103
  });
103
104
  }
105
+ function prefixMethodsWithThis(input, component, options) {
106
+ if (options.api === 'options') {
107
+ var allMethodNames = Object.entries(component.state)
108
+ .filter(function (_a) {
109
+ var _key = _a[0], value = _a[1];
110
+ return (value === null || value === void 0 ? void 0 : value.type) === 'function';
111
+ })
112
+ .map(function (_a) {
113
+ var key = _a[0];
114
+ return key;
115
+ });
116
+ if (!allMethodNames.length)
117
+ return input;
118
+ return (0, replace_identifiers_1.replaceIdentifiers)({ code: input, from: allMethodNames, to: function (name) { return "this.".concat(name); } });
119
+ }
120
+ else {
121
+ return input;
122
+ }
123
+ }
104
124
  // TODO: migrate all stripStateAndPropsRefs to use this here
105
125
  // to properly replace context refs
106
126
  var processBinding = function (_a) {
107
127
  var code = _a.code, options = _a.options, json = _a.json, _b = _a.preserveGetter, preserveGetter = _b === void 0 ? false : _b;
108
- return (0, function_1.pipe)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
109
- includeState: true,
110
- // we don't want to process `props` in the Composition API because it has a `props` ref,
111
- // therefore we can keep pointing to `props.${value}`
112
- includeProps: options.api === 'options',
113
- replaceWith: function (name) {
114
- switch (options.api) {
115
- case 'composition':
116
- return name;
117
- case 'options':
118
- if (name === 'children' || name.startsWith('children.')) {
119
- return 'this.$slots.default';
120
- }
121
- return "this.".concat(name);
122
- }
123
- },
124
- }), function (x) {
125
- var wasGetter = x.match(patterns_1.GETTER);
126
- return (0, function_1.pipe)(x,
127
- // workaround so that getter code is valid and parseable by babel.
128
- patterns_1.stripGetter, function (code) { return processRefs(code, json, options); }, function (code) { return (preserveGetter && wasGetter ? "get ".concat(code) : code); });
129
- });
128
+ try {
129
+ return (0, function_1.pipe)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
130
+ includeState: true,
131
+ // we don't want to process `props` in the Composition API because it has a `props` ref,
132
+ // therefore we can keep pointing to `props.${value}`
133
+ includeProps: options.api === 'options',
134
+ replaceWith: function (name) {
135
+ switch (options.api) {
136
+ case 'composition':
137
+ return name;
138
+ case 'options':
139
+ if (name === 'children' || name.startsWith('children.')) {
140
+ return 'this.$slots.default';
141
+ }
142
+ return "this.".concat(name);
143
+ }
144
+ },
145
+ }), function (x) {
146
+ return (0, function_1.pipe)(x, function (code) { return processRefs(code, json, options); }, function (code) { return prefixMethodsWithThis(code, json, options); }, function (code) { return (preserveGetter === false ? (0, patterns_1.stripGetter)(code) : code); });
147
+ });
148
+ }
149
+ catch (e) {
150
+ console.log('could not process bindings in ', { code: code });
151
+ throw e;
152
+ }
130
153
  };
131
154
  exports.processBinding = processBinding;
132
155
  var getContextValue = function (_a) {
@@ -24,7 +24,7 @@ var helpers_1 = require("./helpers");
24
24
  function getContextInjectString(component, options) {
25
25
  var str = '{';
26
26
  for (var key in component.context.get) {
27
- str += "\n ".concat(key, ": \"").concat((0, helpers_1.encodeQuotes)(component.context.get[key].name), "\",\n ");
27
+ str += "\n ".concat(key, ": ").concat((0, helpers_1.encodeQuotes)(component.context.get[key].name), ",\n ");
28
28
  }
29
29
  str += '}';
30
30
  return str;
@@ -112,20 +112,30 @@ function generateOptionsApiScript(component, options, path, template, props, onU
112
112
  })) === null || _a === void 0 ? void 0 : _a[0]; })
113
113
  .filter(nullable_1.checkIsDefined);
114
114
  var componentsUsed = (0, lodash_1.uniq)(__spreadArray(__spreadArray([], componentsUsedInTemplate, true), importedComponents, true));
115
- var propsDefinition = Array.from(props).filter(function (prop) { return prop !== 'children' && prop !== 'class'; });
116
- // add default props (if set)
117
- if (component.defaultProps) {
118
- propsDefinition = propsDefinition.reduce(function (propsDefinition, curr) {
119
- var _a;
120
- return ((propsDefinition[curr] = ((_a = component.defaultProps) === null || _a === void 0 ? void 0 : _a.hasOwnProperty(curr))
121
- ? { default: component.defaultProps[curr] }
122
- : {}),
123
- propsDefinition);
124
- }, {});
125
- }
115
+ var getPropDefinition = function (_a) {
116
+ var component = _a.component, props = _a.props;
117
+ var propsDefinition = Array.from(props).filter(function (prop) { return prop !== 'children' && prop !== 'class'; });
118
+ var str = 'props: ';
119
+ if (component.defaultProps) {
120
+ var defalutPropsString = propsDefinition
121
+ .map(function (prop) {
122
+ var _a;
123
+ var value = component.defaultProps.hasOwnProperty(prop)
124
+ ? (_a = component.defaultProps[prop]) === null || _a === void 0 ? void 0 : _a.code
125
+ : '{}';
126
+ return "".concat(prop, ": { default: ").concat(value, " }");
127
+ })
128
+ .join(',');
129
+ str += "{".concat(defalutPropsString, "}");
130
+ }
131
+ else {
132
+ str += "".concat(json5_1.default.stringify(propsDefinition));
133
+ }
134
+ return "".concat(str, ",");
135
+ };
126
136
  return "\n export default {\n ".concat(!component.name
127
137
  ? ''
128
- : "name: '".concat(path && ((_a = options.namePrefix) === null || _a === void 0 ? void 0 : _a.call(options, path)) ? ((_b = options.namePrefix) === null || _b === void 0 ? void 0 : _b.call(options, path)) + '-' : '').concat((0, lodash_1.kebabCase)(component.name), "',"), "\n ").concat(generateComponents(componentsUsed, options), "\n ").concat(props.length ? "props: ".concat(json5_1.default.stringify(propsDefinition), ",") : '', "\n ").concat(dataString.length < 4
138
+ : "name: '".concat(path && ((_a = options.namePrefix) === null || _a === void 0 ? void 0 : _a.call(options, path)) ? ((_b = options.namePrefix) === null || _b === void 0 ? void 0 : _b.call(options, path)) + '-' : '').concat((0, lodash_1.kebabCase)(component.name), "',"), "\n ").concat(generateComponents(componentsUsed, options), "\n ").concat(props.length ? getPropDefinition({ component: component, props: props }) : '', "\n ").concat(dataString.length < 4
129
139
  ? ''
130
140
  : "\n data: () => (".concat(dataString, "),\n "), "\n\n ").concat((0, lodash_1.size)(component.context.set)
131
141
  ? "provide() {\n return ".concat((0, helpers_1.getContextProvideString)(component, options), "\n },")
@@ -140,14 +150,14 @@ function generateOptionsApiScript(component, options, path, template, props, onU
140
150
  : '', "\n ").concat(onUpdateWithDeps.length
141
151
  ? "watch: {\n ".concat(onUpdateWithDeps
142
152
  .map(function (hook, index) {
143
- return "".concat((0, helpers_1.getOnUpdateHookName)(index), "() {\n ").concat(hook.code, "\n }\n ");
153
+ return "".concat((0, helpers_1.getOnUpdateHookName)(index), ": { handler() { ").concat(hook.code, " }, immediate: true }");
144
154
  })
145
155
  .join(','), "\n },")
146
156
  : '', "\n ").concat(component.hooks.onUnMount
147
157
  ? "unmounted() {\n ".concat(component.hooks.onUnMount.code, "\n },")
148
158
  : '', "\n\n ").concat(getterString.length < 4
149
159
  ? ''
150
- : " \n computed: ".concat(getterString, ",\n "), "\n ").concat(functionsString.length < 4
160
+ : "\n computed: ".concat(getterString, ",\n "), "\n ").concat(functionsString.length < 4
151
161
  ? ''
152
162
  : "\n methods: ".concat(functionsString, ",\n "), "\n ").concat(Object.entries(component.meta.vueConfig || {})
153
163
  .map(function (_a) {