@builder.io/mitosis 0.0.74 → 0.0.76

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 (55) hide show
  1. package/dist/src/generators/angular.d.ts +2 -0
  2. package/dist/src/generators/angular.js +16 -5
  3. package/dist/src/generators/lit/generate.js +0 -3
  4. package/dist/src/generators/marko/generate.js +0 -3
  5. package/dist/src/generators/qwik/component-generator.js +9 -8
  6. package/dist/src/generators/qwik/directives.js +4 -0
  7. package/dist/src/generators/qwik/jsx.d.ts +1 -1
  8. package/dist/src/generators/qwik/jsx.js +5 -9
  9. package/dist/src/generators/react/generator.js +15 -11
  10. package/dist/src/generators/solid/index.js +24 -26
  11. package/dist/src/generators/stencil/generate.js +0 -3
  12. package/dist/src/generators/svelte/blocks.d.ts +10 -0
  13. package/dist/src/generators/svelte/blocks.js +188 -0
  14. package/dist/src/generators/svelte/helpers.d.ts +2 -0
  15. package/dist/src/generators/svelte/helpers.js +11 -0
  16. package/dist/src/generators/svelte/index.d.ts +1 -0
  17. package/dist/src/generators/svelte/index.js +5 -0
  18. package/dist/src/generators/svelte/svelte.d.ts +3 -0
  19. package/dist/src/generators/svelte/svelte.js +250 -0
  20. package/dist/src/generators/svelte/types.d.ts +4 -0
  21. package/dist/src/generators/svelte/types.js +2 -0
  22. package/dist/src/generators/vue/blocks.js +13 -23
  23. package/dist/src/generators/vue/compositionApi.js +23 -83
  24. package/dist/src/generators/vue/helpers.d.ts +10 -4
  25. package/dist/src/generators/vue/helpers.js +100 -18
  26. package/dist/src/generators/vue/optionsApi.js +6 -28
  27. package/dist/src/generators/vue/types.d.ts +5 -2
  28. package/dist/src/generators/vue/vue.d.ts +1 -4
  29. package/dist/src/generators/vue/vue.js +70 -49
  30. package/dist/src/helpers/is-children.js +3 -2
  31. package/dist/src/helpers/merge-options.d.ts +7 -0
  32. package/dist/src/helpers/merge-options.js +31 -0
  33. package/dist/src/helpers/plugins/process-code.d.ts +8 -0
  34. package/dist/src/helpers/plugins/process-code.js +69 -0
  35. package/dist/src/helpers/render-imports.d.ts +11 -3
  36. package/dist/src/helpers/render-imports.js +18 -4
  37. package/dist/src/helpers/replace-identifiers.js +18 -8
  38. package/dist/src/helpers/typescript.d.ts +1 -1
  39. package/dist/src/parsers/angular.js +4 -3
  40. package/dist/src/parsers/builder.d.ts +2 -2
  41. package/dist/src/parsers/jsx/ast.d.ts +2 -0
  42. package/dist/src/parsers/jsx/component-types.d.ts +2 -0
  43. package/dist/src/parsers/jsx/element-parser.d.ts +3 -1
  44. package/dist/src/parsers/jsx/exports.d.ts +4 -2
  45. package/dist/src/parsers/jsx/function-parser.d.ts +2 -0
  46. package/dist/src/parsers/jsx/helpers.d.ts +2 -0
  47. package/dist/src/parsers/jsx/imports.d.ts +2 -0
  48. package/dist/src/parsers/jsx/metadata.d.ts +2 -0
  49. package/dist/src/parsers/jsx/state.d.ts +2 -0
  50. package/dist/src/targets.d.ts +1 -1
  51. package/dist/src/types/mitosis-component.d.ts +8 -7
  52. package/dist/tsconfig.build.tsbuildinfo +1 -1
  53. package/package.json +3 -3
  54. package/dist/src/generators/svelte.d.ts +0 -14
  55. package/dist/src/generators/svelte.js +0 -455
@@ -0,0 +1,3 @@
1
+ import { TranspilerGenerator } from '../../types/transpiler';
2
+ import { ToSvelteOptions } from './types';
3
+ export declare const componentToSvelte: TranspilerGenerator<ToSvelteOptions>;
@@ -0,0 +1,250 @@
1
+ "use strict";
2
+ var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
3
+ if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
4
+ return cooked;
5
+ };
6
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
7
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
8
+ if (ar || !(i in from)) {
9
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
10
+ ar[i] = from[i];
11
+ }
12
+ }
13
+ return to.concat(ar || Array.prototype.slice.call(from));
14
+ };
15
+ var __importDefault = (this && this.__importDefault) || function (mod) {
16
+ return (mod && mod.__esModule) ? mod : { "default": mod };
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.componentToSvelte = void 0;
20
+ var dedent_1 = __importDefault(require("dedent"));
21
+ var standalone_1 = require("prettier/standalone");
22
+ var traverse_1 = __importDefault(require("traverse"));
23
+ var collect_css_1 = require("../../helpers/styles/collect-css");
24
+ var helpers_1 = require("../../helpers/styles/helpers");
25
+ var fast_clone_1 = require("../../helpers/fast-clone");
26
+ var get_props_1 = require("../../helpers/get-props");
27
+ var get_refs_1 = require("../../helpers/get-refs");
28
+ var get_state_object_string_1 = require("../../helpers/get-state-object-string");
29
+ var is_mitosis_node_1 = require("../../helpers/is-mitosis-node");
30
+ var render_imports_1 = require("../../helpers/render-imports");
31
+ var strip_state_and_props_refs_1 = require("../../helpers/strip-state-and-props-refs");
32
+ var plugins_1 = require("../../modules/plugins");
33
+ var strip_meta_properties_1 = require("../../helpers/strip-meta-properties");
34
+ var getters_to_functions_1 = require("../../helpers/getters-to-functions");
35
+ var babel_transform_1 = require("../../helpers/babel-transform");
36
+ var function_1 = require("fp-ts/lib/function");
37
+ var context_1 = require("../helpers/context");
38
+ var slots_1 = require("../../helpers/slots");
39
+ var json5_1 = __importDefault(require("json5"));
40
+ var functions_1 = require("../helpers/functions");
41
+ var merge_options_1 = require("../../helpers/merge-options");
42
+ var process_code_1 = require("../../helpers/plugins/process-code");
43
+ var helpers_2 = require("./helpers");
44
+ var blocks_1 = require("./blocks");
45
+ var getContextCode = function (json) {
46
+ var contextGetters = json.context.get;
47
+ return Object.keys(contextGetters)
48
+ .map(function (key) { return "let ".concat(key, " = getContext(").concat(contextGetters[key].name, ".key);"); })
49
+ .join('\n');
50
+ };
51
+ var setContextCode = function (json) {
52
+ var contextSetters = json.context.set;
53
+ return Object.keys(contextSetters)
54
+ .map(function (key) {
55
+ var _a = contextSetters[key], ref = _a.ref, value = _a.value, name = _a.name;
56
+ return "setContext(".concat(value ? "".concat(name, ".key") : name, ", ").concat(value
57
+ ? (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)((0, get_state_object_string_1.stringifyContextValue)(value))
58
+ : ref
59
+ ? (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(ref)
60
+ : 'undefined', ");");
61
+ })
62
+ .join('\n');
63
+ };
64
+ /**
65
+ * Replace
66
+ * <input value={state.name} onChange={event => state.name = event.target.value}
67
+ * with
68
+ * <input bind:value={state.name}/>
69
+ * when easily identified, for more idiomatic svelte code
70
+ */
71
+ var useBindValue = function (json, options) {
72
+ function normalizeStr(str) {
73
+ return str
74
+ .trim()
75
+ .replace(/\n|\r/g, '')
76
+ .replace(/^{/, '')
77
+ .replace(/}$/, '')
78
+ .replace(/;$/, '')
79
+ .replace(/\s+/g, '');
80
+ }
81
+ (0, traverse_1.default)(json).forEach(function (item) {
82
+ if ((0, is_mitosis_node_1.isMitosisNode)(item)) {
83
+ var _a = item.bindings, value = _a.value, onChange = _a.onChange;
84
+ if (value && onChange) {
85
+ var _b = onChange.arguments, cusArgs = _b === void 0 ? ['event'] : _b;
86
+ if (normalizeStr(onChange.code) === "".concat(normalizeStr(value.code), "=").concat(cusArgs[0], ".target.value")) {
87
+ delete item.bindings.value;
88
+ delete item.bindings.onChange;
89
+ item.bindings['bind:value'] = value;
90
+ }
91
+ }
92
+ }
93
+ });
94
+ };
95
+ /**
96
+ * Removes all `this.` references.
97
+ */
98
+ var stripThisRefs = function (str) {
99
+ return str.replace(/this\.([a-zA-Z_\$0-9]+)/g, '$1');
100
+ };
101
+ var DEFAULT_OPTIONS = {
102
+ stateType: 'variables',
103
+ prettier: true,
104
+ plugins: [functions_1.FUNCTION_HACK_PLUGIN],
105
+ };
106
+ var transformHookCode = function (options) { return function (hookCode) {
107
+ return (0, function_1.pipe)((0, helpers_2.stripStateAndProps)(hookCode, options), babel_transform_1.babelTransformCode);
108
+ }; };
109
+ var componentToSvelte = function (userProvidedOptions) {
110
+ return function (_a) {
111
+ var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
112
+ var component = _a.component;
113
+ var options = (0, merge_options_1.mergeOptions)(DEFAULT_OPTIONS, userProvidedOptions);
114
+ options.plugins = __spreadArray(__spreadArray([], (options.plugins || []), true), [
115
+ (0, process_code_1.CODE_PROCESSOR_PLUGIN)(function (codeType) {
116
+ switch (codeType) {
117
+ case 'hooks':
118
+ return transformHookCode(options);
119
+ case 'bindings':
120
+ case 'hooks-deps':
121
+ case 'state':
122
+ case 'properties':
123
+ return function (c) { return c; };
124
+ }
125
+ }),
126
+ ], false);
127
+ // Make a copy we can safely mutate, similar to babel's toolchain
128
+ var json = (0, fast_clone_1.fastClone)(component);
129
+ json = (0, plugins_1.runPreJsonPlugins)(json, options.plugins);
130
+ var refs = Array.from((0, get_refs_1.getRefs)(json));
131
+ useBindValue(json, options);
132
+ (0, getters_to_functions_1.gettersToFunctions)(json);
133
+ var props = Array.from((0, get_props_1.getProps)(json)).filter(function (prop) { return !(0, slots_1.isSlotProperty)(prop); });
134
+ json = (0, plugins_1.runPostJsonPlugins)(json, options.plugins);
135
+ var css = (0, collect_css_1.collectCss)(json);
136
+ (0, strip_meta_properties_1.stripMetaProperties)(json);
137
+ var dataString = (0, function_1.pipe)((0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
138
+ data: true,
139
+ functions: false,
140
+ getters: false,
141
+ format: options.stateType === 'proxies' ? 'object' : 'variables',
142
+ keyPrefix: options.stateType === 'variables' ? 'let ' : '',
143
+ valueMapper: function (code) { return (0, helpers_2.stripStateAndProps)(code, options); },
144
+ }), babel_transform_1.babelTransformCode);
145
+ var getterString = (0, function_1.pipe)((0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
146
+ data: false,
147
+ getters: true,
148
+ functions: false,
149
+ format: 'variables',
150
+ keyPrefix: '$: ',
151
+ valueMapper: function (code) {
152
+ return (0, function_1.pipe)(code.replace(/^get ([a-zA-Z_\$0-9]+)/, '$1 = ').replace(/\)/, ') => '), function (str) { return (0, helpers_2.stripStateAndProps)(str, options); }, stripThisRefs);
153
+ },
154
+ }), babel_transform_1.babelTransformCode);
155
+ var functionsString = (0, function_1.pipe)((0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
156
+ data: false,
157
+ getters: false,
158
+ functions: true,
159
+ format: 'variables',
160
+ valueMapper: function (code) { return (0, function_1.pipe)((0, helpers_2.stripStateAndProps)(code, options), stripThisRefs); },
161
+ }), babel_transform_1.babelTransformCode);
162
+ var hasData = dataString.length > 4;
163
+ var str = '';
164
+ var tsLangAttribute = options.typescript ? "lang='ts'" : '';
165
+ if (options.typescript && ((_b = json.types) === null || _b === void 0 ? void 0 : _b.length)) {
166
+ str += (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n <script context='module' ", ">\n ", "\n </script>\n \n\n \n\n "], ["\n <script context='module' ", ">\n ", "\n </script>\n \\n\n \\n\n "])), tsLangAttribute, json.types ? json.types.join('\n\n') + '\n' : '');
167
+ }
168
+ // prepare svelte imports
169
+ var svelteImports = [];
170
+ if ((_d = (_c = json.hooks.onMount) === null || _c === void 0 ? void 0 : _c.code) === null || _d === void 0 ? void 0 : _d.length) {
171
+ svelteImports.push('onMount');
172
+ }
173
+ if ((_e = json.hooks.onUpdate) === null || _e === void 0 ? void 0 : _e.length) {
174
+ svelteImports.push('afterUpdate');
175
+ }
176
+ if ((_g = (_f = json.hooks.onUnMount) === null || _f === void 0 ? void 0 : _f.code) === null || _g === void 0 ? void 0 : _g.length) {
177
+ svelteImports.push('onDestroy');
178
+ }
179
+ if ((0, context_1.hasContext)(component)) {
180
+ svelteImports.push('getContext', 'setContext');
181
+ }
182
+ 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 </script>\n\n ", "\n\n ", "\n "], ["\n <script ", ">\n ", "\n ", "\n\n ", "\n ", "\n "
183
+ // https://svelte.dev/repl/bd9b56891f04414982517bbd10c52c82?version=3.31.0
184
+ , "\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'"), (0, render_imports_1.renderPreComponent)({ component: json, target: 'svelte' }), !hasData || options.stateType === 'variables' ? '' : "import onChange from 'on-change'", props
185
+ .map(function (name) {
186
+ if (name === 'children') {
187
+ return '';
188
+ }
189
+ var propDeclaration = "export let ".concat(name);
190
+ if (options.typescript && json.propsTypeRef && json.propsTypeRef !== 'any') {
191
+ propDeclaration += ": ".concat(json.propsTypeRef.split(' |')[0], "['").concat(name, "']");
192
+ }
193
+ if (json.defaultProps && json.defaultProps.hasOwnProperty(name)) {
194
+ propDeclaration += "=".concat(json5_1.default.stringify(json.defaultProps[name]));
195
+ }
196
+ propDeclaration += ';';
197
+ return propDeclaration;
198
+ })
199
+ .join('\n'),
200
+ // https://svelte.dev/repl/bd9b56891f04414982517bbd10c52c82?version=3.31.0
201
+ (0, helpers_1.hasStyle)(json)
202
+ ? "\n function mitosis_styling (node, vars) {\n Object.entries(vars || {}).forEach(([ p, v ]) => {\n if (p.startsWith('--')) {\n node.style.setProperty(p, v);\n } else {\n node.style[p] = v;\n }\n })\n }\n "
203
+ : '', getContextCode(json), setContextCode(json), functionsString.length < 4 ? '' : functionsString, getterString.length < 4 ? '' : getterString, refs.map(function (ref) { return "let ".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(ref)); }).join('\n'), options.stateType === 'proxies'
204
+ ? dataString.length < 4
205
+ ? ''
206
+ : "let state = onChange(".concat(dataString, ", () => state = state)")
207
+ : dataString, (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)((_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) {
208
+ var code = _a.code, deps = _a.deps;
209
+ if (!deps) {
210
+ return "afterUpdate(() => { ".concat(code, " });");
211
+ }
212
+ var fnName = "onUpdateFn_".concat(index);
213
+ return "\n function ".concat(fnName, "() {\n ").concat(code, "\n }\n $: ").concat(fnName, "(...").concat((0, helpers_2.stripStateAndProps)(deps, options), ")\n ");
214
+ }).join(';')) || '', !((_m = json.hooks.onUnMount) === null || _m === void 0 ? void 0 : _m.code) ? '' : "onDestroy(() => { ".concat(json.hooks.onUnMount.code, " });"), json.children
215
+ .map(function (item) {
216
+ return (0, blocks_1.blockToSvelte)({
217
+ json: item,
218
+ options: options,
219
+ parentComponent: json,
220
+ });
221
+ })
222
+ .join('\n'), !css.trim().length
223
+ ? ''
224
+ : "<style>\n ".concat(css, "\n </style>"));
225
+ str = (0, plugins_1.runPreCodePlugins)(str, options.plugins);
226
+ if (options.prettier !== false) {
227
+ try {
228
+ str = (0, standalone_1.format)(str, {
229
+ parser: 'svelte',
230
+ plugins: [
231
+ // To support running in browsers
232
+ require('prettier/parser-html'),
233
+ require('prettier/parser-postcss'),
234
+ require('prettier/parser-babel'),
235
+ require('prettier/parser-typescript'),
236
+ require('prettier-plugin-svelte'),
237
+ ],
238
+ });
239
+ }
240
+ catch (err) {
241
+ console.warn('Could not prettify');
242
+ console.warn({ string: str }, err);
243
+ }
244
+ }
245
+ str = (0, plugins_1.runPostCodePlugins)(str, options.plugins);
246
+ return str;
247
+ };
248
+ };
249
+ exports.componentToSvelte = componentToSvelte;
250
+ var templateObject_1, templateObject_2;
@@ -0,0 +1,4 @@
1
+ import { BaseTranspilerOptions } from '../../types/transpiler';
2
+ export declare type ToSvelteOptions = BaseTranspilerOptions & {
3
+ stateType?: 'proxies' | 'variables';
4
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -23,7 +23,6 @@ var is_mitosis_node_1 = require("../../helpers/is-mitosis-node");
23
23
  var remove_surrounding_block_1 = require("../../helpers/remove-surrounding-block");
24
24
  var replace_identifiers_1 = require("../../helpers/replace-identifiers");
25
25
  var slots_1 = require("../../helpers/slots");
26
- var strip_state_and_props_refs_1 = require("../../helpers/strip-state-and-props-refs");
27
26
  var jsx_1 = require("../../parsers/jsx");
28
27
  var helpers_1 = require("./helpers");
29
28
  var SPECIAL_PROPERTIES = {
@@ -48,7 +47,7 @@ var NODE_MAPPERS = {
48
47
  var _c;
49
48
  var json = _json;
50
49
  var keyValue = json.bindings.key || { code: 'index' };
51
- var forValue = "(".concat(json.scope.forName, ", index) in ").concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)((_c = json.bindings.each) === null || _c === void 0 ? void 0 : _c.code));
50
+ var forValue = "(".concat(json.scope.forName, ", index) in ").concat((_c = json.bindings.each) === null || _c === void 0 ? void 0 : _c.code);
52
51
  if (options.vueVersion >= 3) {
53
52
  // TODO: tmk key goes on different element (parent vs child) based on Vue 2 vs Vue 3
54
53
  return "<template :key=\"".concat((0, helpers_1.encodeQuotes)((keyValue === null || keyValue === void 0 ? void 0 : keyValue.code) || 'index'), "\" v-for=\"").concat((0, helpers_1.encodeQuotes)(forValue), "\">\n ").concat(json.children.map(function (item) { return (0, exports.blockToVue)(item, options); }).join('\n'), "\n </template>");
@@ -64,7 +63,7 @@ var NODE_MAPPERS = {
64
63
  Show: function (json, options, scope) {
65
64
  var _a, _b, _c, _d, _e, _f;
66
65
  var _g, _h;
67
- var ifValue = (0, slots_1.replaceSlotsInString)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)((_g = json.bindings.when) === null || _g === void 0 ? void 0 : _g.code), function (slotName) { return "$slots.".concat(slotName); });
66
+ var ifValue = (0, slots_1.replaceSlotsInString)(((_g = json.bindings.when) === null || _g === void 0 ? void 0 : _g.code) || '', function (slotName) { return "$slots.".concat(slotName); });
68
67
  var defaultShowTemplate = "\n <template ".concat(SPECIAL_PROPERTIES.V_IF, "=\"").concat((0, helpers_1.encodeQuotes)(ifValue), "\">\n ").concat(json.children.map(function (item) { return (0, exports.blockToVue)(item, options); }).join('\n'), "\n </template>\n ").concat((0, is_mitosis_node_1.isMitosisNode)(json.meta.else)
69
68
  ? "\n <template ".concat(SPECIAL_PROPERTIES.V_ELSE, ">\n ").concat((0, exports.blockToVue)(json.meta.else, options), "\n </template>")
70
69
  : '', "\n ");
@@ -109,7 +108,7 @@ var NODE_MAPPERS = {
109
108
  var isLast = idx === children_1.length - 1;
110
109
  var innerBlock = child.children.filter(filter_empty_text_nodes_1.filterEmptyTextNodes)[0];
111
110
  if (!isLast) {
112
- var childIfValue = (0, function_1.pipe)((_c = child.bindings.when) === null || _c === void 0 ? void 0 : _c.code, strip_state_and_props_refs_1.stripStateAndPropsRefs);
111
+ var childIfValue = (_c = child.bindings.when) === null || _c === void 0 ? void 0 : _c.code;
113
112
  var elseIfString = (0, function_1.pipe)(innerBlock, (0, helpers_1.addPropertiesToJson)((_a = {}, _a[SPECIAL_PROPERTIES.V_ELSE_IF] = childIfValue, _a)), function (block) { return (0, exports.blockToVue)(block, options); });
114
113
  return elseIfString;
115
114
  }
@@ -152,7 +151,7 @@ var NODE_MAPPERS = {
152
151
  *
153
152
  */
154
153
  var ifString = (0, function_1.pipe)(elseBlock, (0, helpers_1.addPropertiesToJson)((_b = {}, _b[SPECIAL_PROPERTIES.V_IF] = (0, helpers_1.invertBooleanExpression)(ifValue), _b)), function (block) { return (0, exports.blockToVue)(block, options); });
155
- var childIfValue = (0, function_1.pipe)((_h = firstChild.bindings.when) === null || _h === void 0 ? void 0 : _h.code, strip_state_and_props_refs_1.stripStateAndPropsRefs, helpers_1.invertBooleanExpression);
154
+ var childIfValue = (0, function_1.pipe)(((_h = firstChild.bindings.when) === null || _h === void 0 ? void 0 : _h.code) || '', helpers_1.invertBooleanExpression);
156
155
  var elseIfString = (0, function_1.pipe)(childElseBlock, (0, helpers_1.addPropertiesToJson)((_c = {}, _c[SPECIAL_PROPERTIES.V_ELSE_IF] = childIfValue, _c)), function (block) { return (0, exports.blockToVue)(block, options); });
157
156
  var firstChildOfFirstChild = firstChild.children.filter(filter_empty_text_nodes_1.filterEmptyTextNodes)[0];
158
157
  var elseString = firstChildOfFirstChild
@@ -181,8 +180,7 @@ var NODE_MAPPERS = {
181
180
  return '<slot />';
182
181
  return "\n <template #".concat(key, ">\n ").concat((_a = json.bindings[key]) === null || _a === void 0 ? void 0 : _a.code, "\n </template>\n ");
183
182
  }
184
- var strippedTextCode = (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(json.bindings.name.code);
185
- return "<slot name=\"".concat((0, slots_1.stripSlotPrefix)(strippedTextCode).toLowerCase(), "\">").concat((_b = json.children) === null || _b === void 0 ? void 0 : _b.map(function (item) { return (0, exports.blockToVue)(item, options); }).join('\n'), "</slot>");
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>");
186
184
  },
187
185
  };
188
186
  var stringifyBinding = function (node) {
@@ -193,15 +191,13 @@ var stringifyBinding = function (node) {
193
191
  return ''; // we handle this after
194
192
  }
195
193
  else if (key === 'class') {
196
- return " :class=\"_classStringToObject(".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(value === null || value === void 0 ? void 0 : value.code, {
197
- replaceWith: '',
198
- }), ")\" ");
194
+ return " :class=\"_classStringToObject(".concat(value === null || value === void 0 ? void 0 : value.code, ")\" ");
199
195
  // TODO: support dynamic classes as objects somehow like Vue requires
200
196
  // https://vuejs.org/v2/guide/class-and-style.html
201
197
  }
202
198
  else {
203
199
  // TODO: proper babel transform to replace. Util for this
204
- var useValue = (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(value === null || value === void 0 ? void 0 : value.code);
200
+ var useValue = (value === null || value === void 0 ? void 0 : value.code) || '';
205
201
  if (key.startsWith('on')) {
206
202
  var _c = value.arguments, cusArgs = _c === void 0 ? ['event'] : _c;
207
203
  var event_1 = key.replace('on', '').toLowerCase();
@@ -254,11 +250,10 @@ var blockToVue = function (node, options, scope) {
254
250
  }
255
251
  var textCode = (_a = node.bindings._text) === null || _a === void 0 ? void 0 : _a.code;
256
252
  if (textCode) {
257
- var strippedTextCode = (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(textCode);
258
- if ((0, slots_1.isSlotProperty)(strippedTextCode)) {
259
- return "<slot name=\"".concat((0, slots_1.stripSlotPrefix)(strippedTextCode).toLowerCase(), "\"/>");
253
+ if ((0, slots_1.isSlotProperty)(textCode)) {
254
+ return "<slot name=\"".concat((0, slots_1.stripSlotPrefix)(textCode).toLowerCase(), "\"/>");
260
255
  }
261
- return "{{".concat(strippedTextCode, "}}");
256
+ return "{{".concat(textCode, "}}");
262
257
  }
263
258
  var str = '';
264
259
  str += "<".concat(node.name, " ");
@@ -274,22 +269,17 @@ var blockToVue = function (node, options, scope) {
274
269
  str += " ".concat(key, "=\"").concat((0, helpers_1.encodeQuotes)(value), "\" ");
275
270
  }
276
271
  }
277
- var stringifiedBindings = Object.entries(node.bindings)
278
- .map(function (_a) {
279
- var k = _a[0], v = _a[1];
280
- return stringifyBinding(node)([k, v]);
281
- })
282
- .join('');
272
+ var stringifiedBindings = Object.entries(node.bindings).map(stringifyBinding(node)).join('');
283
273
  str += stringifiedBindings;
284
274
  // spreads
285
275
  var spreads = (0, lodash_1.filter)(node.bindings, function (binding) { return (binding === null || binding === void 0 ? void 0 : binding.type) === 'spread'; }).map(function (value) { return value === null || value === void 0 ? void 0 : value.code; });
286
276
  if (spreads === null || spreads === void 0 ? void 0 : spreads.length) {
287
277
  if (spreads.length > 1) {
288
278
  var spreadsString = "{...".concat(spreads.join(', ...'), "}");
289
- str += " v-bind=\"".concat((0, helpers_1.encodeQuotes)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(spreadsString)), "\"");
279
+ str += " v-bind=\"".concat((0, helpers_1.encodeQuotes)(spreadsString), "\"");
290
280
  }
291
281
  else {
292
- str += " v-bind=\"".concat((0, helpers_1.encodeQuotes)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(spreads.join(''))), "\"");
282
+ str += " v-bind=\"".concat((0, helpers_1.encodeQuotes)(spreads.join('')), "\"");
293
283
  }
294
284
  }
295
285
  if (jsx_1.selfClosingTags.has(node.name)) {
@@ -3,15 +3,6 @@ var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cook
3
3
  if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
4
4
  return cooked;
5
5
  };
6
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
7
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
8
- if (ar || !(i in from)) {
9
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
10
- ar[i] = from[i];
11
- }
12
- }
13
- return to.concat(ar || Array.prototype.slice.call(from));
14
- };
15
6
  var __importDefault = (this && this.__importDefault) || function (mod) {
16
7
  return (mod && mod.__esModule) ? mod : { "default": mod };
17
8
  };
@@ -20,11 +11,9 @@ exports.generateCompositionApiScript = void 0;
20
11
  var dedent_1 = __importDefault(require("dedent"));
21
12
  var json5_1 = __importDefault(require("json5"));
22
13
  var lodash_1 = require("lodash");
23
- var babel_transform_1 = require("../../helpers/babel-transform");
24
14
  var get_state_object_string_1 = require("../../helpers/get-state-object-string");
25
- var strip_state_and_props_refs_1 = require("../../helpers/strip-state-and-props-refs");
26
- var core_1 = require("@babel/core");
27
15
  var helpers_1 = require("./helpers");
16
+ var strip_state_and_props_refs_1 = require("../../helpers/strip-state-and-props-refs");
28
17
  var getCompositionPropDefinition = function (_a) {
29
18
  var options = _a.options, component = _a.component, props = _a.props;
30
19
  var str = 'const props = ';
@@ -40,48 +29,6 @@ var getCompositionPropDefinition = function (_a) {
40
29
  }
41
30
  return str;
42
31
  };
43
- function shouldAppendValueToRef(path) {
44
- var parent = path.parent, node = path.node;
45
- if (core_1.types.isFunctionDeclaration(parent) && parent.id === node) {
46
- return false;
47
- }
48
- if (core_1.types.isCallExpression(parent)) {
49
- return false;
50
- }
51
- var isMemberExpression = core_1.types.isMemberExpression(parent);
52
- if (isMemberExpression &&
53
- core_1.types.isThisExpression(parent.object) &&
54
- core_1.types.isProgram(path.scope.block) &&
55
- path.scope.hasReference(node.name)) {
56
- return false;
57
- }
58
- if (isMemberExpression &&
59
- core_1.types.isIdentifier(parent.object) &&
60
- core_1.types.isIdentifier(parent.property) &&
61
- parent.property.name === node.name) {
62
- return false;
63
- }
64
- if (Object.keys(path.scope.bindings).includes(path.node.name)) {
65
- return false;
66
- }
67
- if (path.parentPath.listKey === 'arguments' || path.parentPath.listKey === 'params') {
68
- return false;
69
- }
70
- return true;
71
- }
72
- function appendValueToRefs(input, component, options) {
73
- var refKeys = Object.keys(component.refs);
74
- var stateKeys = Object.keys((0, lodash_1.pickBy)(component.state, function (i) { return (i === null || i === void 0 ? void 0 : i.type) === 'property'; }));
75
- var allKeys = __spreadArray(__spreadArray([], refKeys, true), stateKeys, true);
76
- var output = (0, helpers_1.processBinding)({ code: input, options: options, json: component, includeProps: false });
77
- return (0, babel_transform_1.babelTransformExpression)(output, {
78
- Identifier: function (path) {
79
- if (allKeys.includes(path.node.name) && shouldAppendValueToRef(path)) {
80
- path.replaceWith(core_1.types.identifier("".concat(path.node.name, ".value")));
81
- }
82
- },
83
- });
84
- }
85
32
  function generateCompositionApiScript(component, options, template, props, onUpdateWithDeps, onUpdateWithoutDeps) {
86
33
  var _a, _b, _c, _d, _e, _f, _g;
87
34
  var refs = (0, get_state_object_string_1.getStateObjectStringFromComponent)(component, {
@@ -89,53 +36,46 @@ function generateCompositionApiScript(component, options, template, props, onUpd
89
36
  functions: false,
90
37
  getters: false,
91
38
  format: 'variables',
92
- valueMapper: function (code) {
93
- return (0, helpers_1.processBinding)({ code: "ref(".concat(code, ")"), options: options, json: component });
94
- },
39
+ valueMapper: function (code) { return "ref(".concat(code, ")"); },
95
40
  keyPrefix: 'const',
96
41
  });
97
42
  var methods = (0, get_state_object_string_1.getStateObjectStringFromComponent)(component, {
98
43
  data: false,
99
44
  getters: false,
100
45
  functions: true,
101
- valueMapper: function (code) { return (0, helpers_1.processBinding)({ code: code, options: options, json: component, includeProps: false }); },
102
46
  format: 'variables',
103
47
  });
104
48
  if (template.includes('_classStringToObject')) {
105
- methods += " function _classStringToObject(str) {\n const obj = {};\n if (typeof str !== 'string') { return obj }\n const classNames = str.trim().split(/\\s+/);\n for (const name of classNames) {\n obj[name] = true;\n }\n return obj;\n } ";
49
+ methods += " function _classStringToObject(str) {\n const obj = {};\n if (typeof str !== 'string') { return obj }\n const classNames = str.trim().split(/\\s+/);\n for (const name of classNames) {\n obj[name] = true;\n }\n return obj;\n } ";
106
50
  }
107
51
  var getterKeys = Object.keys((0, lodash_1.pickBy)(component.state, function (i) { return (i === null || i === void 0 ? void 0 : i.type) === 'getter'; }));
108
- var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\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 ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n "])), props.length ? getCompositionPropDefinition({ component: component, props: props, options: options }) : '', refs, (_a = Object.keys(component.context.get)) === null || _a === void 0 ? void 0 : _a.map(function (key) { return "const ".concat(key, " = inject(").concat(component.context.get[key].name, ")"); }).join('\n'), (_b = Object.keys(component.context.set)) === null || _b === void 0 ? void 0 : _b.map(function (key) { return "provide(".concat(component.context.set[key].name, ", ").concat(component.context.set[key].ref, ")"); }).join('\n'), (_c = Object.keys(component.refs)) === null || _c === void 0 ? void 0 : _c.map(function (key) {
52
+ var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\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 ", "\n ", "\n ", "\n ", "\n\n ", "\n\n ", "\n ", "\n "])), props.length ? getCompositionPropDefinition({ component: component, props: props, options: options }) : '', refs, (_a = Object.keys(component.context.get)) === null || _a === void 0 ? void 0 : _a.map(function (key) { return "const ".concat(key, " = inject(").concat(component.context.get[key].name, ")"); }).join('\n'), (_b = Object.values(component.context.set)) === null || _b === void 0 ? void 0 : _b.map(function (contextSet) {
53
+ return "provide(".concat(contextSet.name, ", ").concat((0, helpers_1.getContextValue)({
54
+ json: component,
55
+ options: options,
56
+ })(contextSet), ")");
57
+ }).join('\n'), (_c = Object.keys(component.refs)) === null || _c === void 0 ? void 0 : _c.map(function (key) {
109
58
  if (options.typescript) {
110
59
  return "const ".concat(key, " = ref<").concat(component.refs[key].typeParameter, ">()");
111
60
  }
112
61
  else {
113
62
  return "const ".concat(key, " = ref(null)");
114
63
  }
115
- }).join('\n'), appendValueToRefs((_e = (_d = component.hooks.onInit) === null || _d === void 0 ? void 0 : _d.code) !== null && _e !== void 0 ? _e : '', component, options), !((_f = component.hooks.onMount) === null || _f === void 0 ? void 0 : _f.code)
116
- ? ''
117
- : "onMounted(() => { ".concat(appendValueToRefs(component.hooks.onMount.code, component, options), "})"), !((_g = component.hooks.onUnMount) === null || _g === void 0 ? void 0 : _g.code)
64
+ }).join('\n'), (_e = (_d = component.hooks.onInit) === null || _d === void 0 ? void 0 : _d.code) !== null && _e !== void 0 ? _e : '', !((_f = component.hooks.onMount) === null || _f === void 0 ? void 0 : _f.code) ? '' : "onMounted(() => { ".concat(component.hooks.onMount.code, "})"), !((_g = component.hooks.onUnMount) === null || _g === void 0 ? void 0 : _g.code)
118
65
  ? ''
119
- : "onMounted(() => { ".concat(appendValueToRefs(component.hooks.onUnMount.code, component, options), "})"), !getterKeys
120
- ? ''
121
- : getterKeys
122
- .map(function (key) {
123
- var _a, _b;
124
- var code = (_b = (_a = component.state[key]) === null || _a === void 0 ? void 0 : _a.code) === null || _b === void 0 ? void 0 : _b.toString();
125
- return !code
126
- ? ''
127
- : "const ".concat(key, " = computed(").concat(appendValueToRefs(code.replace(key, '').replace('get ()', '() =>'), component, options), ")");
128
- })
129
- .join('\n'), !(onUpdateWithoutDeps === null || onUpdateWithoutDeps === void 0 ? void 0 : onUpdateWithoutDeps.length)
130
- ? ''
131
- : onUpdateWithoutDeps.map(function (hook) {
132
- return "onUpdated(() => ".concat(appendValueToRefs(hook.code, component, options), ")");
133
- }), !(onUpdateWithDeps === null || onUpdateWithDeps === void 0 ? void 0 : onUpdateWithDeps.length)
134
- ? ''
135
- : onUpdateWithDeps.map(function (hook) {
136
- return appendValueToRefs("watch(".concat(hook.deps, ", (").concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(hook.deps), ") => { ").concat(hook.code, "})\n"), component, options);
137
- }), (methods === null || methods === void 0 ? void 0 : methods.length) ? appendValueToRefs(methods, component, options) : '');
138
- str = str.replace(/this\./g, ''); // strip this elsewhere (e.g. functions)
66
+ : "onUnmounted(() => { ".concat(component.hooks.onUnMount.code, "})"), (getterKeys === null || getterKeys === void 0 ? void 0 : getterKeys.map(function (key) {
67
+ var _a, _b;
68
+ var code = (_b = (_a = component.state[key]) === null || _a === void 0 ? void 0 : _a.code) === null || _b === void 0 ? void 0 : _b.toString();
69
+ if (!code) {
70
+ return '';
71
+ }
72
+ // transform `foo() { return this.bar }` to `() => { return bar.value }`
73
+ var getterAsFunction = code.replace(key, '').trim().replace(/^\(\)/, '() =>');
74
+ var computedCode = "const ".concat(key, " = computed(").concat(getterAsFunction, ")");
75
+ return computedCode;
76
+ }).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) {
77
+ return "watch(() => ".concat(hook.deps, ", (").concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(hook.deps), ") => { ").concat(hook.code, " })");
78
+ }).join('\n')) || '', methods !== null && methods !== void 0 ? methods : '');
139
79
  return str;
140
80
  }
141
81
  exports.generateCompositionApiScript = generateCompositionApiScript;
@@ -1,4 +1,5 @@
1
- import { MitosisComponent } from '../../types/mitosis-component';
1
+ import { Nullable } from '../../helpers/nullable';
2
+ import { ContextSetInfo, MitosisComponent } from '../../types/mitosis-component';
2
3
  import { MitosisNode } from '../../types/mitosis-node';
3
4
  import { ToVueOptions } from './types';
4
5
  export declare const addPropertiesToJson: (properties: MitosisNode['properties']) => (json: MitosisNode) => MitosisNode;
@@ -8,9 +9,14 @@ export declare const invertBooleanExpression: (expression: string) => string;
8
9
  export declare function encodeQuotes(string: string): string;
9
10
  export declare const renameMitosisComponentsToKebabCase: (str: string) => string;
10
11
  export declare function getContextNames(json: MitosisComponent): string[];
11
- export declare function processBinding({ code, options, json, includeProps, }: {
12
+ export declare const processBinding: ({ code, options, json, includeProps, }: {
12
13
  code: string;
13
14
  options: ToVueOptions;
14
15
  json: MitosisComponent;
15
- includeProps?: boolean;
16
- }): string;
16
+ includeProps?: boolean | undefined;
17
+ }) => string;
18
+ export declare const getContextValue: ({ options, json }: {
19
+ options: ToVueOptions;
20
+ json: MitosisComponent;
21
+ }) => ({ name, ref, value }: ContextSetInfo) => Nullable<string>;
22
+ export declare const getContextProvideString: (json: MitosisComponent, options: ToVueOptions) => string;