@builder.io/mitosis 0.0.56-68 → 0.0.56-69

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.
@@ -161,6 +161,17 @@ var TESTS_FOR_TARGET = {
161
161
  ADVANCED_REF,
162
162
  ON_UPDATE_RETURN,
163
163
  ],
164
+ lit: [
165
+ BASIC_TESTS,
166
+ SLOTS_TESTS,
167
+ SHOW_TESTS,
168
+ FORWARD_REF_TESTS,
169
+ MULTI_ON_UPDATE_TESTS,
170
+ FORM_BLOCK_TESTS,
171
+ FOR_SHOW_TESTS,
172
+ ADVANCED_REF,
173
+ ON_UPDATE_RETURN,
174
+ ],
164
175
  marko: [
165
176
  BASIC_TESTS,
166
177
  SLOTS_TESTS,
@@ -4,4 +4,7 @@ var solid_1 = require("../generators/solid");
4
4
  var shared_1 = require("./shared");
5
5
  describe('Solid', function () {
6
6
  (0, shared_1.runTestsForTarget)('solid', (0, solid_1.componentToSolid)());
7
+ (0, shared_1.runTestsForTarget)('solid', (0, solid_1.componentToSolid)({
8
+ stylesType: 'style-tag',
9
+ }));
7
10
  });
@@ -0,0 +1,2 @@
1
+ import { MitosisNode } from '../../types/mitosis-node';
2
+ export declare function collectClassString(json: MitosisNode, bindingOpenChar?: string, bindingCloseChar?: string): string | null;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.collectClassString = void 0;
4
+ // This should really be a preprocessor mapping the `class` attribute binding based on what other values have
5
+ // to make this more pluggable
6
+ function collectClassString(json, bindingOpenChar, bindingCloseChar) {
7
+ var _a, _b;
8
+ if (bindingOpenChar === void 0) { bindingOpenChar = '{'; }
9
+ if (bindingCloseChar === void 0) { bindingCloseChar = '}'; }
10
+ var staticClasses = [];
11
+ var hasStaticClasses = Boolean(staticClasses.length);
12
+ if (json.properties.class) {
13
+ staticClasses.push(json.properties.class);
14
+ delete json.properties.class;
15
+ }
16
+ if (json.properties.className) {
17
+ staticClasses.push(json.properties.className);
18
+ delete json.properties.className;
19
+ }
20
+ var dynamicClasses = [];
21
+ if (typeof ((_a = json.bindings.class) === null || _a === void 0 ? void 0 : _a.code) === 'string') {
22
+ dynamicClasses.push(json.bindings.class.code);
23
+ delete json.bindings.class;
24
+ }
25
+ if (typeof ((_b = json.bindings.className) === null || _b === void 0 ? void 0 : _b.code) === 'string') {
26
+ dynamicClasses.push(json.bindings.className.code);
27
+ delete json.bindings.className;
28
+ }
29
+ var staticClassesString = staticClasses.join(' ');
30
+ var dynamicClassesString = dynamicClasses.join(" + ' ' + ");
31
+ var hasDynamicClasses = Boolean(dynamicClasses.length);
32
+ if (hasStaticClasses && !hasDynamicClasses) {
33
+ return "\"".concat(staticClassesString, "\"");
34
+ }
35
+ if (hasDynamicClasses && !hasStaticClasses) {
36
+ return "".concat(bindingOpenChar).concat(dynamicClassesString).concat(bindingCloseChar);
37
+ }
38
+ if (hasDynamicClasses && hasStaticClasses) {
39
+ return "".concat(bindingOpenChar, "\"").concat(staticClassesString, " \" + ").concat(dynamicClassesString).concat(bindingCloseChar);
40
+ }
41
+ return null;
42
+ }
43
+ exports.collectClassString = collectClassString;
@@ -0,0 +1,4 @@
1
+ import { BaseTranspilerOptions, Transpiler } from '../../types/transpiler';
2
+ export interface ToLitOptions extends BaseTranspilerOptions {
3
+ }
4
+ export declare const componentToLit: (options?: ToLitOptions) => Transpiler;
@@ -0,0 +1,195 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
7
+ return (mod && mod.__esModule) ? mod : { "default": mod };
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.componentToLit = void 0;
11
+ var dedent_1 = __importDefault(require("dedent"));
12
+ var standalone_1 = require("prettier/standalone");
13
+ var get_state_object_string_1 = require("../../helpers/get-state-object-string");
14
+ var render_imports_1 = require("../../helpers/render-imports");
15
+ var jsx_1 = require("../../parsers/jsx");
16
+ var plugins_1 = require("../../modules/plugins");
17
+ var fast_clone_1 = require("../../helpers/fast-clone");
18
+ var strip_meta_properties_1 = require("../../helpers/strip-meta-properties");
19
+ var collect_class_string_1 = require("./collect-class-string");
20
+ var get_props_1 = require("../../helpers/get-props");
21
+ var strip_state_and_props_refs_1 = require("../../helpers/strip-state-and-props-refs");
22
+ var filter_empty_text_nodes_1 = require("../../helpers/filter-empty-text-nodes");
23
+ var dash_case_1 = require("../../helpers/dash-case");
24
+ var collect_css_1 = require("../../helpers/styles/collect-css");
25
+ var indent_1 = require("../../helpers/indent");
26
+ var map_refs_1 = require("../../helpers/map-refs");
27
+ var get_refs_1 = require("../../helpers/get-refs");
28
+ var lodash_1 = require("lodash");
29
+ var is_upper_case_1 = require("../../helpers/is-upper-case");
30
+ var blockToLit = function (json, options) {
31
+ var _a, _b, _c, _d, _e;
32
+ if (options === void 0) { options = {}; }
33
+ if (json.properties._text) {
34
+ return json.properties._text;
35
+ }
36
+ if ((_a = json.bindings._text) === null || _a === void 0 ? void 0 : _a.code) {
37
+ return "${".concat(processBinding((_b = json.bindings) === null || _b === void 0 ? void 0 : _b._text.code), "}");
38
+ }
39
+ if (json.name === 'For') {
40
+ return "${".concat(processBinding((_c = json.bindings.each) === null || _c === void 0 ? void 0 : _c.code), "?.map((").concat(json.properties._forName, ", index) => (\n html`").concat(json.children
41
+ .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
42
+ .map(function (item) { return blockToLit(item, options); })
43
+ .join('\n'), "`\n ))}");
44
+ }
45
+ else if (json.name === 'Show') {
46
+ return "${".concat(processBinding((_d = json.bindings.when) === null || _d === void 0 ? void 0 : _d.code), " ?\n html`").concat(json.children
47
+ .filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
48
+ .map(function (item) { return blockToLit(item, options); })
49
+ .join('\n'), "`\n ) : ").concat(!json.meta.else ? 'null' : blockToLit(json.meta.else, options), "}");
50
+ }
51
+ var str = '';
52
+ var tagName = (0, is_upper_case_1.isUpperCase)(json.name[0]) ? (0, dash_case_1.dashCase)(json.name) : json.name;
53
+ str += "<".concat(tagName, " ");
54
+ var classString = (0, collect_class_string_1.collectClassString)(json);
55
+ if (classString) {
56
+ str += " class=".concat(classString, " ");
57
+ }
58
+ if ((_e = json.bindings._spread) === null || _e === void 0 ? void 0 : _e.code) {
59
+ // Lit currently does not support spread, it's been an open PR
60
+ // for a year https://github.com/lit/lit/pull/1960/files
61
+ // str += ` \${...(${json.bindings._spread.code})} `;
62
+ }
63
+ for (var key in json.properties) {
64
+ var value = json.properties[key];
65
+ str += " ".concat(key, "=\"").concat(value, "\" ");
66
+ }
67
+ for (var key in json.bindings) {
68
+ var _f = json.bindings[key], code = _f.code, _g = _f.arguments, cusArgs = _g === void 0 ? ['event'] : _g;
69
+ if (key === '_spread' || key === '_forName') {
70
+ continue;
71
+ }
72
+ if (key === 'ref') {
73
+ str += " ref=\"".concat(code, "\" ");
74
+ }
75
+ else if (key.startsWith('on')) {
76
+ var useKey = key === 'onChange' && json.name === 'input' ? 'onInput' : key;
77
+ useKey = '@' + useKey.substring(2).toLowerCase();
78
+ str += " ".concat(useKey, "=${").concat(cusArgs.join(','), " => ").concat(processBinding(code), "} ");
79
+ }
80
+ else {
81
+ str += " ".concat(key, "=${").concat(processBinding(code), "} ");
82
+ }
83
+ }
84
+ if (jsx_1.selfClosingTags.has(json.name)) {
85
+ return str + ' />';
86
+ }
87
+ str += '>';
88
+ if (json.children) {
89
+ str += json.children.map(function (item) { return blockToLit(item, options); }).join('\n');
90
+ }
91
+ str += "</".concat(tagName, ">");
92
+ return str;
93
+ };
94
+ function processBinding(code) {
95
+ return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, { replaceWith: 'this.' });
96
+ }
97
+ var componentToLit = function (options) {
98
+ if (options === void 0) { options = {}; }
99
+ return function (_a) {
100
+ var _b, _c, _d, _e;
101
+ var component = _a.component;
102
+ var json = (0, fast_clone_1.fastClone)(component);
103
+ if (options.plugins) {
104
+ json = (0, plugins_1.runPreJsonPlugins)(json, options.plugins);
105
+ }
106
+ var props = (0, get_props_1.getProps)(component);
107
+ var css = (0, collect_css_1.collectCss)(json);
108
+ var domRefs = (0, get_refs_1.getRefs)(json);
109
+ (0, map_refs_1.mapRefs)(component, function (refName) { return "this.".concat((0, lodash_1.camelCase)(refName)); });
110
+ if (options.plugins) {
111
+ json = (0, plugins_1.runPostJsonPlugins)(json, options.plugins);
112
+ }
113
+ (0, strip_meta_properties_1.stripMetaProperties)(json);
114
+ var dataString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
115
+ format: 'class',
116
+ data: true,
117
+ functions: false,
118
+ getters: false,
119
+ keyPrefix: '@state() ',
120
+ valueMapper: function (code) { return processBinding(code); },
121
+ });
122
+ var methodsString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
123
+ format: 'class',
124
+ data: false,
125
+ functions: true,
126
+ getters: true,
127
+ valueMapper: function (code) { return processBinding(code); },
128
+ });
129
+ if (options.prettier !== false) {
130
+ try {
131
+ css = (0, standalone_1.format)(css, {
132
+ parser: 'css',
133
+ plugins: [require('prettier/parser-postcss')],
134
+ });
135
+ }
136
+ catch (err) {
137
+ console.warn('Could not format css', err);
138
+ }
139
+ }
140
+ var html = json.children.map(function (item) { return blockToLit(item, options); }).join('\n');
141
+ if (options.prettier !== false) {
142
+ try {
143
+ css = (0, standalone_1.format)(css, {
144
+ parser: 'css',
145
+ plugins: [require('prettier/parser-postcss')],
146
+ });
147
+ }
148
+ catch (err) {
149
+ console.warn('Could not format css', err);
150
+ }
151
+ try {
152
+ html = (0, standalone_1.format)(html, {
153
+ parser: 'html',
154
+ plugins: [require('prettier/parser-html')],
155
+ });
156
+ }
157
+ catch (err) {
158
+ console.warn('Could not format html', err);
159
+ }
160
+ }
161
+ var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n import { LitElement, html } from 'lit';\n import { customElement, property, css, state } from 'lit/decorators.js';\n\n\n @customElement('", "')\n export default class ", " extends LitElement {\n ", "\n\n ", "\n \n \n ", "\n\n ", "\n ", "\n \n ", "\n ", "\n ", "\n \n render() {\n return html`\n ", "\n `\n }\n }\n "], ["\n ", "\n import { LitElement, html } from 'lit';\n import { customElement, property, css, state } from 'lit/decorators.js';\n\n\n @customElement('", "')\n export default class ", " extends LitElement {\n ", "\n\n ", "\n \n \n ", "\n\n ", "\n ", "\n \n ", "\n ", "\n ", "\n \n render() {\n return html\\`\n ", "\n \\`\n }\n }\n "])), (0, render_imports_1.renderPreComponent)({ component: json, target: 'lit' }), ((_b = json.meta.useMetadata) === null || _b === void 0 ? void 0 : _b.tagName) || (0, dash_case_1.dashCase)(json.name), json.name, css.length
162
+ ? "static styles = css`\n ".concat((0, indent_1.indent)(css, 8), "`;")
163
+ : '', Array.from(domRefs)
164
+ .map(function (refName) { return "\n @query('[ref=\"".concat(refName, "\"]')\n ").concat((0, lodash_1.camelCase)(refName), "!: HTMLElement;\n "); })
165
+ .join('\n'), Array.from(props)
166
+ .map(function (item) { return "@property() ".concat(item, ": any"); })
167
+ .join('\n'), dataString, methodsString, !((_c = json.hooks.onMount) === null || _c === void 0 ? void 0 : _c.code)
168
+ ? ''
169
+ : "connectedCallback() { ".concat(processBinding(json.hooks.onMount.code), " }"), !((_d = json.hooks.onUnMount) === null || _d === void 0 ? void 0 : _d.code)
170
+ ? ''
171
+ : "disconnectedCallback() { ".concat(processBinding(json.hooks.onUnMount.code), " }"), !((_e = json.hooks.onUpdate) === null || _e === void 0 ? void 0 : _e.length)
172
+ ? ''
173
+ : json.hooks.onUpdate.map(function (hook) { return "updated() { ".concat(processBinding(hook.code), " }"); }), (0, indent_1.indent)(html, 8));
174
+ if (options.plugins) {
175
+ str = (0, plugins_1.runPreCodePlugins)(str, options.plugins);
176
+ }
177
+ if (options.prettier !== false) {
178
+ try {
179
+ str = (0, standalone_1.format)(str, {
180
+ parser: 'typescript',
181
+ plugins: [require('prettier/parser-typescript')],
182
+ });
183
+ }
184
+ catch (err) {
185
+ console.warn('Could not format Lit typescript', err);
186
+ }
187
+ }
188
+ if (options.plugins) {
189
+ str = (0, plugins_1.runPostCodePlugins)(str, options.plugins);
190
+ }
191
+ return str;
192
+ };
193
+ };
194
+ exports.componentToLit = componentToLit;
195
+ var templateObject_1;
@@ -0,0 +1 @@
1
+ export * from './generate';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./generate"), exports);
@@ -49,6 +49,7 @@ var core_1 = require("@babel/core");
49
49
  var lodash_1 = require("lodash");
50
50
  var state_1 = require("../helpers/state");
51
51
  var collect_css_1 = require("../helpers/styles/collect-css");
52
+ var hash_sum_1 = __importDefault(require("hash-sum"));
52
53
  // Transform <foo.bar key="value" /> to <component :is="foo.bar" key="value" />
53
54
  function processDynamicComponents(json, options) {
54
55
  var found = false;
@@ -243,7 +244,10 @@ var componentToSolid = function (_options) {
243
244
  }
244
245
  (0, strip_meta_properties_1.stripMetaProperties)(json);
245
246
  var foundDynamicComponents = processDynamicComponents(json, options);
246
- var css = options.stylesType === 'style-tag' && (0, collect_css_1.collectCss)(json);
247
+ var css = options.stylesType === 'style-tag' &&
248
+ (0, collect_css_1.collectCss)(json, {
249
+ prefix: (0, hash_sum_1.default)(json),
250
+ });
247
251
  var stateString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json);
248
252
  var hasState = (0, state_1.checkHasState)(json);
249
253
  var componentsUsed = (0, get_components_used_1.getComponentsUsed)(json);
@@ -35,6 +35,8 @@ var getFileExtensionForTarget = function (target) {
35
35
  return '.vue';
36
36
  case 'marko':
37
37
  return '.marko';
38
+ case 'lit':
39
+ return '.js';
38
40
  case 'angular':
39
41
  return '';
40
42
  // these `.lite` extensions are handled in the `transpile` step of the CLI.
@@ -97,6 +99,11 @@ var renderImport = function (_a) {
97
99
  var importValue = getImportValue(importedValues);
98
100
  var isComponentImport = checkIsComponentImport(theImport);
99
101
  var shouldBeAsyncImport = asyncComponentImports && isComponentImport;
102
+ // For lit (components) we just want to do a plain import
103
+ // https://lit.dev/docs/components/rendering/#composing-templates
104
+ if (isComponentImport && target === 'lit') {
105
+ return "import '".concat(path, "';");
106
+ }
100
107
  if (shouldBeAsyncImport) {
101
108
  var isVueImport = target === 'vue';
102
109
  if (isVueImport && importedValues.namedImports) {
@@ -45,6 +45,7 @@ export * from './generators/marko';
45
45
  export * from './generators/mitosis';
46
46
  export * from './generators/template';
47
47
  export * from './generators/swift-ui';
48
+ export * from './generators/lit';
48
49
  export * from './generators/react-native';
49
50
  export * from './helpers/parse-reactive-script';
50
51
  export * from './helpers/is-mitosis-node';
package/dist/src/index.js CHANGED
@@ -84,6 +84,7 @@ __exportStar(require("./generators/marko"), exports);
84
84
  __exportStar(require("./generators/mitosis"), exports);
85
85
  __exportStar(require("./generators/template"), exports);
86
86
  __exportStar(require("./generators/swift-ui"), exports);
87
+ __exportStar(require("./generators/lit"), exports);
87
88
  __exportStar(require("./generators/react-native"), exports);
88
89
  __exportStar(require("./helpers/parse-reactive-script"), exports);
89
90
  __exportStar(require("./helpers/is-mitosis-node"), exports);
@@ -26,4 +26,5 @@ export declare const targets: {
26
26
  qwik: (userOptions?: import("./generators/qwik/component-generator").ToQwikOptions) => import(".").Transpiler;
27
27
  marko: (userOptions?: import("./generators/marko").ToMarkoOptions) => import(".").Transpiler;
28
28
  preact: (reactOptions?: import("./generators/react").ToReactOptions) => import(".").Transpiler;
29
+ lit: (options?: import("./generators/lit").ToLitOptions) => import(".").Transpiler;
29
30
  };
@@ -17,6 +17,7 @@ var vue_1 = require("./generators/vue");
17
17
  var stencil_1 = require("./generators/stencil");
18
18
  var qwik_1 = require("./generators/qwik");
19
19
  var marko_1 = require("./generators/marko");
20
+ var lit_1 = require("./generators/lit");
20
21
  exports.targets = {
21
22
  angular: angular_1.componentToAngular,
22
23
  builder: builder_1.componentToBuilder,
@@ -38,4 +39,5 @@ exports.targets = {
38
39
  qwik: qwik_1.componentToQwik,
39
40
  marko: marko_1.componentToMarko,
40
41
  preact: react_2.componentToPreact,
42
+ lit: lit_1.componentToLit,
41
43
  };