@builder.io/mitosis 0.0.56-39 → 0.0.56-41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  import { BaseTranspilerOptions, Transpiler } from '../../types/transpiler';
2
2
  export interface ToMarkoOptions extends BaseTranspilerOptions {
3
3
  }
4
- export declare const componentToMarko: (options?: ToMarkoOptions) => Transpiler;
4
+ export declare const componentToMarko: (userOptions?: ToMarkoOptions) => Transpiler;
5
5
  /**
6
6
  * Convert marko expressions to valid html
7
7
  *
@@ -3,6 +3,17 @@ 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 __assign = (this && this.__assign) || function () {
7
+ __assign = Object.assign || function(t) {
8
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
9
+ s = arguments[i];
10
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
11
+ t[p] = s[p];
12
+ }
13
+ return t;
14
+ };
15
+ return __assign.apply(this, arguments);
16
+ };
6
17
  var __importDefault = (this && this.__importDefault) || function (mod) {
7
18
  return (mod && mod.__esModule) ? mod : { "default": mod };
8
19
  };
@@ -24,28 +35,68 @@ var indent_1 = require("../../helpers/indent");
24
35
  var map_refs_1 = require("../../helpers/map-refs");
25
36
  var dash_case_1 = require("../../helpers/dash-case");
26
37
  var has_props_1 = require("../../helpers/has-props");
38
+ var function_literal_prefix_1 = require("../../constants/function-literal-prefix");
39
+ var method_literal_prefix_1 = require("../../constants/method-literal-prefix");
40
+ var patterns_1 = require("../../helpers/patterns");
41
+ var get_refs_1 = require("../../helpers/get-refs");
27
42
  // Having issues with this, so off for now
28
43
  var USE_MARKO_PRETTIER = false;
44
+ function getStateTypeOfValue(value) {
45
+ if (typeof value === 'string') {
46
+ if (value.startsWith(function_literal_prefix_1.functionLiteralPrefix)) {
47
+ return 'function';
48
+ }
49
+ else if (value.startsWith(method_literal_prefix_1.methodLiteralPrefix)) {
50
+ var isGet = Boolean(value.replace(method_literal_prefix_1.methodLiteralPrefix, '').match(patterns_1.GETTER));
51
+ if (isGet) {
52
+ return 'getter';
53
+ }
54
+ return 'method';
55
+ }
56
+ }
57
+ return 'property';
58
+ }
59
+ /**
60
+ * Return the names of methods and functions on state
61
+ */
62
+ function getStateMethodNames(json) {
63
+ return Object.keys(json.state).filter(function (key) {
64
+ var value = json.state[key];
65
+ var type = getStateTypeOfValue(value);
66
+ return type === 'function' || type === 'method';
67
+ });
68
+ }
69
+ /**
70
+ * Return the names of getter and functions on state
71
+ */
72
+ function getStateGetterNames(json) {
73
+ return Object.keys(json.state).filter(function (key) { return getStateTypeOfValue(json.state[key]) === 'getter'; });
74
+ }
75
+ /**
76
+ * Return the names of properties (basic literal values) on state
77
+ */
78
+ function getStatePropertyNames(json) {
79
+ return Object.keys(json.state).filter(function (key) { return getStateTypeOfValue(json.state[key]) === 'property'; });
80
+ }
29
81
  var blockToMarko = function (json, options) {
30
82
  var _a, _b, _c, _d, _e;
31
- if (options === void 0) { options = {}; }
32
83
  if (json.properties._text) {
33
84
  return json.properties._text;
34
85
  }
35
86
  if ((_a = json.bindings._text) === null || _a === void 0 ? void 0 : _a.code) {
36
- return "${".concat(processBinding((_b = json.bindings) === null || _b === void 0 ? void 0 : _b._text.code), "}");
87
+ return "${".concat(processBinding(options.component, (_b = json.bindings) === null || _b === void 0 ? void 0 : _b._text.code), "}");
37
88
  }
38
89
  if (json.name === 'Fragment') {
39
90
  return json.children.map(function (child) { return blockToMarko(child, options); }).join('\n');
40
91
  }
41
92
  if (json.name === 'For') {
42
- return "<for|".concat(json.properties._forName, "| of=(").concat(processBinding((_c = json.bindings.each) === null || _c === void 0 ? void 0 : _c.code), ")>\n ").concat(json.children
93
+ return "<for|".concat(json.properties._forName, "| of=(").concat(processBinding(options.component, (_c = json.bindings.each) === null || _c === void 0 ? void 0 : _c.code), ")>\n ").concat(json.children
43
94
  .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
44
95
  .map(function (item) { return blockToMarko(item, options); })
45
96
  .join('\n'), "\n </for>");
46
97
  }
47
98
  else if (json.name === 'Show') {
48
- return "<if(".concat(processBinding((_d = json.bindings.when) === null || _d === void 0 ? void 0 : _d.code), ")>\n ").concat(json.children
99
+ return "<if(".concat(processBinding(options.component, (_d = json.bindings.when) === null || _d === void 0 ? void 0 : _d.code), ")>\n ").concat(json.children
49
100
  .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
50
101
  .map(function (item) { return blockToMarko(item, options); })
51
102
  .join('\n'), "</if>\n ").concat(!json.meta.else ? '' : "<else>".concat(blockToMarko(json.meta.else, options), "</else>"));
@@ -69,14 +120,14 @@ var blockToMarko = function (json, options) {
69
120
  continue;
70
121
  }
71
122
  if (key === 'ref') {
72
- str += " ref=((el) => this.".concat(code, " = el) ");
123
+ str += " key=\"".concat(code, "\" ");
73
124
  }
74
125
  else if (key.startsWith('on')) {
75
126
  var useKey = key === 'onChange' && json.name === 'input' ? 'onInput' : key;
76
- str += " ".concat((0, dash_case_1.dashCase)(useKey), "=(").concat(cusArgs.join(','), " => ").concat(processBinding(code), ") ");
127
+ str += " ".concat((0, dash_case_1.dashCase)(useKey), "=(").concat(cusArgs.join(','), " => ").concat(processBinding(options.component, code), ") ");
77
128
  }
78
129
  else {
79
- str += " ".concat(key, "=(").concat(processBinding(code), ") ");
130
+ str += " ".concat(key, "=(").concat(processBinding(options.component, code), ") ");
80
131
  }
81
132
  }
82
133
  if (jsx_1.selfClosingTags.has(json.name)) {
@@ -89,29 +140,37 @@ var blockToMarko = function (json, options) {
89
140
  str += "</".concat((0, dash_case_1.dashCase)(json.name), ">");
90
141
  return str;
91
142
  };
92
- function processBinding(code, type) {
143
+ function processBinding(json, code, type) {
93
144
  if (type === void 0) { type = 'attribute'; }
94
145
  return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
95
146
  replaceWith: type === 'state' ? 'input.' : type === 'class' ? 'this.input.' : 'input.',
96
147
  includeProps: true,
97
148
  includeState: false,
98
149
  }), {
99
- replaceWith: type === 'state' ? 'this.' : type === 'class' ? 'this.state.' : 'state.',
150
+ replaceWith: function (key) {
151
+ var isProperty = getStatePropertyNames(json).includes(key);
152
+ if (isProperty) {
153
+ return (type === 'state' || type === 'class' ? 'this.state.' : 'state.') + key;
154
+ }
155
+ return (type === 'class' || type === 'state' ? 'this.' : 'component.') + key;
156
+ },
100
157
  includeProps: false,
101
158
  includeState: true,
102
159
  });
103
160
  }
104
- var componentToMarko = function (options) {
105
- if (options === void 0) { options = {}; }
161
+ var componentToMarko = function (userOptions) {
162
+ if (userOptions === void 0) { userOptions = {}; }
106
163
  return function (_a) {
107
164
  var _b, _c, _d;
108
165
  var component = _a.component;
109
166
  var json = (0, fast_clone_1.fastClone)(component);
167
+ var options = __assign(__assign({}, userOptions), { component: json });
110
168
  if (options.plugins) {
111
169
  json = (0, plugins_1.runPreJsonPlugins)(json, options.plugins);
112
170
  }
113
171
  var css = (0, collect_css_1.collectCss)(json);
114
- (0, map_refs_1.mapRefs)(component, function (refName) { return "this.".concat(refName); });
172
+ var domRefs = (0, get_refs_1.getRefs)(json);
173
+ (0, map_refs_1.mapRefs)(json, function (refName) { return "this.".concat(refName); });
115
174
  if (options.plugins) {
116
175
  json = (0, plugins_1.runPostJsonPlugins)(json, options.plugins);
117
176
  }
@@ -119,13 +178,19 @@ var componentToMarko = function (options) {
119
178
  var dataString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
120
179
  format: 'object',
121
180
  data: true,
181
+ functions: false,
182
+ getters: false,
183
+ valueMapper: function (code) { return processBinding(json, code, 'state'); },
184
+ });
185
+ var thisHasProps = (0, has_props_1.hasProps)(json);
186
+ var methodsString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
187
+ format: 'class',
188
+ data: false,
122
189
  functions: true,
123
190
  getters: true,
124
- valueMapper: function (code) { return processBinding(code, 'state'); },
191
+ valueMapper: function (code) { return processBinding(json, code, 'class'); },
125
192
  });
126
- var thisHasProps = (0, has_props_1.hasProps)(json);
127
- var methodsString = '';
128
- var hasState = dataString.length > 5;
193
+ var hasState = dataString.trim().length > 5;
129
194
  if (options.prettier !== false) {
130
195
  try {
131
196
  css = (0, standalone_1.format)(css, {
@@ -137,15 +202,17 @@ var componentToMarko = function (options) {
137
202
  console.warn('Could not format css', err);
138
203
  }
139
204
  }
140
- var jsString = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n\n class {\n ", "\n\n ", "\n \n ", "\n ", "\n ", "\n }\n "], ["\n ", "\n\n class {\n ", "\n\n ", "\n \n ", "\n ", "\n ", "\n }\n "])), (0, render_imports_1.renderPreComponent)({ component: json, target: 'marko' }), methodsString, !hasState
205
+ var jsString = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n\n class {\n ", "\n\n ", "\n\n ", "\n \n ", "\n ", "\n ", "\n }\n "], ["\n ", "\n\n class {\n ", "\n\n ", "\n\n ", "\n \n ", "\n ", "\n ", "\n }\n "])), (0, render_imports_1.renderPreComponent)({ component: json, target: 'marko' }), methodsString, !hasState
141
206
  ? ''
142
- : "onCreate() {\n ".concat(thisHasProps ? 'const input = this.input;' : '', "\n this.state = ").concat(dataString, "\n }"), !((_b = json.hooks.onMount) === null || _b === void 0 ? void 0 : _b.code)
207
+ : "onCreate(".concat(thisHasProps ? 'input' : '', ") {\n this.state = ").concat(dataString, "\n }"), Array.from(domRefs)
208
+ .map(function (refName) { return "get ".concat(refName, "() { \n return this.getEl('").concat(refName, "')\n }"); })
209
+ .join('\n'), !((_b = json.hooks.onMount) === null || _b === void 0 ? void 0 : _b.code)
143
210
  ? ''
144
- : "onMount() { ".concat(processBinding(json.hooks.onMount.code, 'class'), " }"), !((_c = json.hooks.onUnMount) === null || _c === void 0 ? void 0 : _c.code)
211
+ : "onMount() { ".concat(processBinding(json, json.hooks.onMount.code, 'class'), " }"), !((_c = json.hooks.onUnMount) === null || _c === void 0 ? void 0 : _c.code)
145
212
  ? ''
146
- : "onDestroy() { ".concat(processBinding(json.hooks.onUnMount.code, 'class'), " }"), !((_d = json.hooks.onUpdate) === null || _d === void 0 ? void 0 : _d.length)
213
+ : "onDestroy() { ".concat(processBinding(json, json.hooks.onUnMount.code, 'class'), " }"), !((_d = json.hooks.onUpdate) === null || _d === void 0 ? void 0 : _d.length)
147
214
  ? ''
148
- : json.hooks.onUpdate.map(function (hook) { return "onRender() { ".concat(processBinding(hook.code, 'class'), " }"); }));
215
+ : json.hooks.onUpdate.map(function (hook) { return "onRender() { ".concat(processBinding(json, hook.code, 'class'), " }"); }));
149
216
  var htmlString = json.children.map(function (item) { return blockToMarko(item, options); }).join('\n');
150
217
  var cssString = css.length
151
218
  ? "style { \n ".concat((0, indent_1.indent)(css, 2).trim(), "\n}")
@@ -158,7 +158,9 @@ var componentToMitosis = function (toMitosisOptions) {
158
158
  // TODO: smart only pull in imports as needed
159
159
  var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n ", "\n ", "\n\n ", "\n\n ", "\n\n export default function ", "(props) {\n ", "\n ", "\n\n ", "\n\n ", "\n\n return (", "\n ", "\n ", ")\n }\n\n "], ["\n ", "\n ", "\n ", "\n ", "\n\n ", "\n\n ", "\n\n export default function ", "(props) {\n ", "\n ", "\n\n ", "\n\n ", "\n\n return (", "\n ", "\n ", ")\n }\n\n "])), !needsMitosisCoreImport
160
160
  ? ''
161
- : "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') : '', json.interfaces ? (_b = json.interfaces) === null || _b === void 0 ? void 0 : _b.join('\n') : '', (0, render_imports_1.renderPreComponent)({ component: json, target: 'mitosis' }), stringifiedUseMetadata !== '{}' ? "".concat(jsx_1.METADATA_HOOK_NAME, "(").concat(stringifiedUseMetadata, ")") : '', component.name, !hasState ? '' : "const state = useStore(".concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json), ");"), getRefsString(json, refs), !((_c = json.hooks.onMount) === null || _c === void 0 ? void 0 : _c.code) ? '' : "onMount(() => { ".concat(json.hooks.onMount.code, " })"), !((_d = json.hooks.onUnMount) === null || _d === void 0 ? void 0 : _d.code) ? '' : "onUnMount(() => { ".concat(json.hooks.onUnMount.code, " })"), addWrapper ? '<>' : '', json.children.map(function (item) { return (0, exports.blockToMitosis)(item, options); }).join('\n'), addWrapper ? '</>' : '');
161
+ : "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') : '', json.interfaces ? (_b = json.interfaces) === null || _b === void 0 ? void 0 : _b.join('\n') : '', (0, render_imports_1.renderPreComponent)({ component: json, target: 'mitosis' }), stringifiedUseMetadata && stringifiedUseMetadata !== '{}'
162
+ ? "".concat(jsx_1.METADATA_HOOK_NAME, "(").concat(stringifiedUseMetadata, ")")
163
+ : '', component.name, !hasState ? '' : "const state = useStore(".concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json), ");"), getRefsString(json, refs), !((_c = json.hooks.onMount) === null || _c === void 0 ? void 0 : _c.code) ? '' : "onMount(() => { ".concat(json.hooks.onMount.code, " })"), !((_d = json.hooks.onUnMount) === null || _d === void 0 ? void 0 : _d.code) ? '' : "onUnMount(() => { ".concat(json.hooks.onUnMount.code, " })"), addWrapper ? '<>' : '', json.children.map(function (item) { return (0, exports.blockToMitosis)(item, options); }).join('\n'), addWrapper ? '</>' : '');
162
164
  if (options.prettier !== false) {
163
165
  try {
164
166
  str = (0, standalone_1.format)(str, {
@@ -88,7 +88,9 @@ exports.components = {
88
88
  }
89
89
  var omitFields = ['link', 'openInNewTab', 'text'];
90
90
  var hasLink = node.properties.link || node.bindings.link;
91
- return (0, create_mitosis_node_1.createMitosisNode)(__assign(__assign({}, node), { name: hasLink ? 'a' : node.properties.$tagName || 'span', properties: __assign(__assign({}, (0, lodash_1.omit)(node.properties, omitFields)), properties), bindings: __assign(__assign({}, (0, lodash_1.omit)(node.bindings, omitFields)), bindings) }));
91
+ return (0, create_mitosis_node_1.createMitosisNode)(__assign(__assign({}, node), {
92
+ // TODO: use 'button' tag for no link, and add `all: unset` to CSS string only then
93
+ name: hasLink ? 'a' : node.properties.$tagName || 'span', properties: __assign(__assign({}, (0, lodash_1.omit)(node.properties, omitFields)), properties), bindings: __assign(__assign({}, (0, lodash_1.omit)(node.bindings, omitFields)), bindings) }));
92
94
  },
93
95
  Embed: function (node, context, components) {
94
96
  return wrapOutput(node, (0, create_mitosis_node_1.createMitosisNode)({
@@ -24,5 +24,5 @@ export declare const targets: {
24
24
  vue3: (vueOptions?: Omit<import("./generators/vue").ToVueOptions, "vueVersion"> | undefined) => import(".").Transpiler;
25
25
  stencil: (options?: import("./generators/stencil").ToStencilOptions) => import(".").Transpiler;
26
26
  qwik: (userOptions?: import("./generators/qwik/component-generator").ToQwikOptions) => import(".").Transpiler;
27
- marko: (options?: import("./generators/marko").ToMarkoOptions) => import(".").Transpiler;
27
+ marko: (userOptions?: import("./generators/marko").ToMarkoOptions) => import(".").Transpiler;
28
28
  };