@builder.io/mitosis 0.0.50-6 → 0.0.50
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.
- package/dist/src/__tests__/angular.test.js +6 -0
- package/dist/src/__tests__/builder.test.js +5 -1
- package/dist/src/__tests__/data/blocks/multiple-onUpdate.raw.d.ts +1 -0
- package/dist/src/__tests__/data/blocks/multiple-onUpdate.raw.jsx +13 -0
- package/dist/src/__tests__/data/blocks/multiple-onUpdateWithDeps.raw.d.ts +1 -0
- package/dist/src/__tests__/data/blocks/multiple-onUpdateWithDeps.raw.jsx +19 -0
- package/dist/src/__tests__/data/blocks/onUpdateWithDeps.raw.jsx +6 -3
- package/dist/src/__tests__/data/blocks/self-referencing-component-with-children.raw.d.ts +1 -0
- package/dist/src/__tests__/data/blocks/self-referencing-component-with-children.raw.jsx +15 -0
- package/dist/src/__tests__/data/blocks/self-referencing-component.raw.d.ts +1 -0
- package/dist/src/__tests__/data/blocks/self-referencing-component.raw.jsx +12 -0
- package/dist/src/__tests__/data/blocks/shadow-dom.raw.d.ts +6 -0
- package/dist/src/__tests__/data/blocks/shadow-dom.raw.jsx +54 -0
- package/dist/src/__tests__/data/blocks/slot.raw.d.ts +5 -0
- package/dist/src/__tests__/data/blocks/slot.raw.jsx +6 -0
- package/dist/src/__tests__/data/blocks/stamped-io.raw.jsx +8 -0
- package/dist/src/__tests__/data/blocks/styles.raw.d.ts +1 -0
- package/dist/src/__tests__/data/blocks/styles.raw.jsx +10 -0
- package/dist/src/__tests__/data/context/simple.context.lite.d.ts +8 -1
- package/dist/src/__tests__/html.test.js +6 -0
- package/dist/src/__tests__/qwik.directive.test.d.ts +1 -0
- package/dist/src/__tests__/qwik.directive.test.js +107 -0
- package/dist/src/__tests__/qwik.test.js +256 -128
- package/dist/src/__tests__/react-native.test.js +12 -0
- package/dist/src/__tests__/react.test.js +12 -0
- package/dist/src/__tests__/svelte.test.js +24 -0
- package/dist/src/__tests__/vue.test.js +12 -0
- package/dist/src/__tests__/webcomponent.test.d.ts +1 -0
- package/dist/src/__tests__/webcomponent.test.js +12 -0
- package/dist/src/constants/html_tags.d.ts +1 -0
- package/dist/src/constants/html_tags.js +119 -0
- package/dist/src/generators/angular.js +7 -4
- package/dist/src/generators/context/react copy.d.ts +8 -0
- package/dist/src/generators/context/react copy.js +28 -0
- package/dist/src/generators/context/solid.d.ts +8 -0
- package/dist/src/generators/context/solid.js +28 -0
- package/dist/src/generators/context/svelte.d.ts +8 -0
- package/dist/src/generators/context/svelte.js +28 -0
- package/dist/src/generators/helpers/context.d.ts +2 -0
- package/dist/src/generators/helpers/context.js +8 -0
- package/dist/src/generators/html.js +20 -14
- package/dist/src/generators/mitosis.js +4 -3
- package/dist/src/generators/qwik/component.js +26 -5
- package/dist/src/generators/qwik/directives.d.ts +14 -5
- package/dist/src/generators/qwik/directives.js +32 -25
- package/dist/src/generators/qwik/handlers.js +3 -4
- package/dist/src/generators/qwik/jsx.js +17 -5
- package/dist/src/generators/qwik/src-generator.d.ts +0 -9
- package/dist/src/generators/qwik/src-generator.js +93 -101
- package/dist/src/generators/qwik/styles.js +8 -11
- package/dist/src/generators/react.js +9 -8
- package/dist/src/generators/solid.js +82 -8
- package/dist/src/generators/stencil/generate.js +5 -3
- package/dist/src/generators/stencil/index.js +5 -1
- package/dist/src/generators/svelte.d.ts +9 -1
- package/dist/src/generators/svelte.js +175 -76
- package/dist/src/generators/vue.d.ts +1 -1
- package/dist/src/generators/vue.js +86 -10
- package/dist/src/helpers/babel-transform copy.d.ts +8 -0
- package/dist/src/helpers/babel-transform copy.js +138 -0
- package/dist/src/helpers/babel-transform.d.ts +4 -7
- package/dist/src/helpers/babel-transform.js +18 -2
- package/dist/src/helpers/babel-transform.test.d.ts +1 -0
- package/dist/src/helpers/babel-transform.test.js +7 -0
- package/dist/src/helpers/create-mitosis-component.js +1 -1
- package/dist/src/helpers/get-state-object-string.js +2 -1
- package/dist/src/helpers/map-refs.js +22 -9
- package/dist/src/helpers/parsers.d.ts +8 -0
- package/dist/src/helpers/parsers.js +66 -0
- package/dist/src/helpers/render-imports.d.ts +2 -4
- package/dist/src/helpers/render-imports.js +32 -14
- package/dist/src/index.d.ts +10 -5
- package/dist/src/index.js +11 -5
- package/dist/src/parsers/builder.d.ts +16 -50
- package/dist/src/parsers/builder.js +22 -40
- package/dist/src/parsers/context.js +5 -1
- package/dist/src/parsers/jsx.d.ts +7 -0
- package/dist/src/parsers/jsx.js +27 -5
- package/dist/src/parsers/liquid.js +5 -1
- package/dist/src/plugins/compile-away-builder-components.js +5 -1
- package/dist/src/symbols/symbol-processor.js +3 -3
- package/dist/src/targets.d.ts +1 -1
- package/dist/src/types/mitosis-component.d.ts +8 -3
- package/dist/test/qwik/Accordion/high.jsx +0 -1
- package/dist/test/qwik/Accordion/low.jsx +171 -65
- package/dist/test/qwik/Accordion/med.jsx +9 -10
- package/dist/test/qwik/For/high.jsx +0 -1
- package/dist/test/qwik/For/low.jsx +25 -58
- package/dist/test/qwik/For/med.jsx +9 -10
- package/dist/test/qwik/Image/high.js +9 -1
- package/dist/test/qwik/Image/low.js +0 -42
- package/dist/test/qwik/Image/med.js +149 -12
- package/dist/test/qwik/Image.slow/high.js +9 -1
- package/dist/test/qwik/Image.slow/low.js +0 -42
- package/dist/test/qwik/Image.slow/med.js +149 -12
- package/dist/{src/types/generators.d.ts → test/qwik/bindings/high.cjs} +0 -0
- package/dist/test/qwik/bindings/low.cjs +33 -0
- package/dist/test/qwik/bindings/med.cjs +12 -0
- package/dist/test/qwik/button/high.js +1 -13
- package/dist/test/qwik/button/low.js +8 -40
- package/dist/test/qwik/button/med.js +36 -11
- package/dist/test/qwik/component/bindings/high.jsx +0 -1
- package/dist/test/qwik/component/bindings/low.jsx +26 -71
- package/dist/test/qwik/component/bindings/med.jsx +48 -39
- package/dist/test/qwik/component/component/inputs/high.cjsx +9 -0
- package/dist/test/qwik/component/component/inputs/low.cjsx +0 -0
- package/dist/test/qwik/component/component/inputs/med.cjsx +65 -0
- package/dist/test/qwik/hello_world/stylesheet/high.jsx +0 -1
- package/dist/test/qwik/hello_world/stylesheet/low.jsx +6 -27
- package/dist/test/qwik/hello_world/stylesheet/med.jsx +9 -10
- package/dist/test/qwik/page-with-symbol/high.js +0 -1
- package/dist/test/qwik/page-with-symbol/low.js +15 -52
- package/dist/test/qwik/page-with-symbol/med.js +9 -10
- package/dist/test/qwik/show-hide/high.jsx +9 -0
- package/dist/test/qwik/show-hide/low.jsx +0 -0
- package/dist/test/qwik/show-hide/med.jsx +174 -0
- package/dist/test/qwik/svg/high.js +0 -1
- package/dist/test/qwik/svg/low.js +9 -35
- package/dist/test/qwik/svg/med.js +9 -10
- package/dist/test/qwik/todo/Todo.cjs/high.cjs +4 -39
- package/dist/test/qwik/todo/Todo.cjs/low.cjs +0 -1
- package/dist/test/qwik/todo/Todo.cjs/med.cjs +38 -61
- package/dist/test/qwik/todo/Todo.js/high.js +22 -5
- package/dist/test/qwik/todo/Todo.js/med.js +45 -1
- package/dist/test/qwik/todo/Todo.tsx/high.tsx +4 -39
- package/dist/test/qwik/todo/Todo.tsx/low.tsx +0 -1
- package/dist/test/qwik/todo/Todo.tsx/med.tsx +31 -34
- package/dist/test/qwik/todos/Todo.tsx/high.tsx +0 -12
- package/dist/test/qwik/todos/Todo.tsx/low.tsx +18 -25
- package/dist/test/qwik/todos/Todo.tsx/med.tsx +8 -10
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +22 -22
- package/dist/src/__tests__/data/blocks/onMount-onUnMount.d.ts +0 -1
- package/dist/src/__tests__/data/blocks/onMount-onUnMount.jsx +0 -13
- package/dist/src/__tests__/data/blocks/onMount.d.ts +0 -1
- package/dist/src/__tests__/data/blocks/onMount.jsx +0 -13
- package/dist/src/types/generators.js +0 -1
|
@@ -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
|
};
|
|
@@ -21,6 +32,12 @@ var strip_meta_properties_1 = require("../helpers/strip-meta-properties");
|
|
|
21
32
|
var get_components_used_1 = require("../helpers/get-components-used");
|
|
22
33
|
var traverse_1 = __importDefault(require("traverse"));
|
|
23
34
|
var is_mitosis_node_1 = require("../helpers/is-mitosis-node");
|
|
35
|
+
var filter_empty_text_nodes_1 = require("../helpers/filter-empty-text-nodes");
|
|
36
|
+
var create_mitosis_node_1 = require("../helpers/create-mitosis-node");
|
|
37
|
+
var context_1 = require("./helpers/context");
|
|
38
|
+
var babel_transform_1 = require("../helpers/babel-transform");
|
|
39
|
+
var core_1 = require("@babel/core");
|
|
40
|
+
var lodash_1 = require("lodash");
|
|
24
41
|
// Transform <foo.bar key="value" /> to <component :is="foo.bar" key="value" />
|
|
25
42
|
function processDynamicComponents(json, options) {
|
|
26
43
|
var found = false;
|
|
@@ -35,6 +52,13 @@ function processDynamicComponents(json, options) {
|
|
|
35
52
|
});
|
|
36
53
|
return found;
|
|
37
54
|
}
|
|
55
|
+
function getContextString(component, options) {
|
|
56
|
+
var str = '';
|
|
57
|
+
for (var key in component.context.get) {
|
|
58
|
+
str += "\n const ".concat(key, " = useContext(").concat(component.context.get[key].name, ");\n ");
|
|
59
|
+
}
|
|
60
|
+
return str;
|
|
61
|
+
}
|
|
38
62
|
// This should really be a preprocessor mapping the `class` attribute binding based on what other values have
|
|
39
63
|
// to make this more pluggable
|
|
40
64
|
var collectClassString = function (json) {
|
|
@@ -90,7 +114,9 @@ var blockToSolid = function (json, options) {
|
|
|
90
114
|
}
|
|
91
115
|
if (json.name === 'For') {
|
|
92
116
|
var needsWrapper = json.children.length !== 1;
|
|
93
|
-
return "<For each={".concat(json.bindings.each, "}>\n {(").concat(json.properties._forName, ", index) =>\n ").concat(needsWrapper ? '<>' : '', "\n ").concat(json.children
|
|
117
|
+
return "<For each={".concat(json.bindings.each, "}>\n {(").concat(json.properties._forName, ", index) =>\n ").concat(needsWrapper ? '<>' : '', "\n ").concat(json.children
|
|
118
|
+
.filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
|
|
119
|
+
.map(function (child) { return blockToSolid(child, options); }), "}\n ").concat(needsWrapper ? '</>' : '', "\n </For>");
|
|
94
120
|
}
|
|
95
121
|
var str = '';
|
|
96
122
|
if (json.name === 'Fragment') {
|
|
@@ -120,7 +146,32 @@ var blockToSolid = function (json, options) {
|
|
|
120
146
|
str += " ".concat(useKey, "={event => ").concat(value, "} ");
|
|
121
147
|
}
|
|
122
148
|
else {
|
|
123
|
-
|
|
149
|
+
var useValue = value;
|
|
150
|
+
if (key === 'style') {
|
|
151
|
+
// Convert camelCase keys to kebab-case
|
|
152
|
+
// TODO: support more than top level objects, may need
|
|
153
|
+
// a runtime helper for expressions that are not a direct
|
|
154
|
+
// object literal, such as ternaries and other expression
|
|
155
|
+
// types
|
|
156
|
+
useValue = (0, babel_transform_1.babelTransformExpression)(value, {
|
|
157
|
+
ObjectExpression: function (path) {
|
|
158
|
+
// TODO: limit to top level objects only
|
|
159
|
+
for (var _i = 0, _a = path.node.properties; _i < _a.length; _i++) {
|
|
160
|
+
var property = _a[_i];
|
|
161
|
+
if (core_1.types.isObjectProperty(property)) {
|
|
162
|
+
if (core_1.types.isIdentifier(property.key) ||
|
|
163
|
+
core_1.types.isStringLiteral(property.key)) {
|
|
164
|
+
var key_1 = core_1.types.isIdentifier(property.key)
|
|
165
|
+
? property.key.name
|
|
166
|
+
: property.key.value;
|
|
167
|
+
property.key = core_1.types.stringLiteral((0, lodash_1.kebabCase)(key_1));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
str += " ".concat(key, "={").concat(useValue, "} ");
|
|
124
175
|
}
|
|
125
176
|
}
|
|
126
177
|
if (jsx_1.selfClosingTags.has(json.name)) {
|
|
@@ -128,7 +179,10 @@ var blockToSolid = function (json, options) {
|
|
|
128
179
|
}
|
|
129
180
|
str += '>';
|
|
130
181
|
if (json.children) {
|
|
131
|
-
str += json.children
|
|
182
|
+
str += json.children
|
|
183
|
+
.filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
|
|
184
|
+
.map(function (item) { return blockToSolid(item, options); })
|
|
185
|
+
.join('\n');
|
|
132
186
|
}
|
|
133
187
|
if (json.name === 'Fragment') {
|
|
134
188
|
str += '</>';
|
|
@@ -147,6 +201,18 @@ var getRefsString = function (json, refs) {
|
|
|
147
201
|
}
|
|
148
202
|
return str;
|
|
149
203
|
};
|
|
204
|
+
function addProviderComponents(json, options) {
|
|
205
|
+
for (var key in json.context.set) {
|
|
206
|
+
var _a = json.context.set[key], name_1 = _a.name, value = _a.value;
|
|
207
|
+
json.children = [
|
|
208
|
+
(0, create_mitosis_node_1.createMitosisNode)(__assign({ name: "".concat(name_1, ".Provider"), children: json.children }, (value && {
|
|
209
|
+
bindings: {
|
|
210
|
+
value: (0, get_state_object_string_1.getMemberObjectString)(value),
|
|
211
|
+
},
|
|
212
|
+
}))),
|
|
213
|
+
];
|
|
214
|
+
}
|
|
215
|
+
}
|
|
150
216
|
var componentToSolid = function (options) {
|
|
151
217
|
if (options === void 0) { options = {}; }
|
|
152
218
|
return function (_a) {
|
|
@@ -156,8 +222,9 @@ var componentToSolid = function (options) {
|
|
|
156
222
|
if (options.plugins) {
|
|
157
223
|
json = (0, plugins_1.runPreJsonPlugins)(json, options.plugins);
|
|
158
224
|
}
|
|
225
|
+
addProviderComponents(json, options);
|
|
159
226
|
var componentHasStyles = (0, collect_styles_1.hasStyles)(json);
|
|
160
|
-
var addWrapper = json.children.length
|
|
227
|
+
var addWrapper = json.children.filter(filter_empty_text_nodes_1.filterEmptyTextNodes).length !== 1;
|
|
161
228
|
if (options.plugins) {
|
|
162
229
|
json = (0, plugins_1.runPostJsonPlugins)(json, options.plugins);
|
|
163
230
|
}
|
|
@@ -166,15 +233,22 @@ var componentToSolid = function (options) {
|
|
|
166
233
|
var stateString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json);
|
|
167
234
|
var hasState = Boolean(Object.keys(component.state).length);
|
|
168
235
|
var componentsUsed = (0, get_components_used_1.getComponentsUsed)(json);
|
|
236
|
+
var componentHasContext = (0, context_1.hasContext)(json);
|
|
169
237
|
var hasShowComponent = componentsUsed.has('Show');
|
|
170
238
|
var hasForComponent = componentsUsed.has('For');
|
|
171
|
-
var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n ", "\n ", "\n ", "\n\n export default function ", "(props) {\n ", "\n \n ", "\n\n ", "\n\n return (", "\n ", "\n ", ")\n }\n\n "], ["\n ", "\n ", "\n ", "\n ", "\n ", "\n\n export default function ", "(props) {\n ", "\n \n ", "\n\n ", "\n\n return (", "\n ", "\n ", ")\n }\n\n "])), !(hasShowComponent || hasForComponent)
|
|
239
|
+
var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\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 export default function ", "(props) {\n ", "\n \n ", "\n ", "\n\n ", "\n\n return (", "\n ", "\n ", ")\n }\n\n "])), !(hasShowComponent || hasForComponent)
|
|
172
240
|
? ''
|
|
173
|
-
: "import { \n ".concat(!hasShowComponent ? '' : 'Show, ', "\n ").concat(!hasForComponent ? '' : 'For, ', "\n ").concat(!((_b = json.hooks.onMount) === null || _b === void 0 ? void 0 : _b.code) ? '' : 'onMount, ', "\n } from 'solid-js';"), !foundDynamicComponents ? '' : "import { Dynamic } from 'solid-js/web';", !hasState ? '' : "import { createMutable } from 'solid-js/store';", !componentHasStyles
|
|
241
|
+
: "import { \n ".concat(!componentHasContext ? '' : 'useContext, ', "\n ").concat(!hasShowComponent ? '' : 'Show, ', "\n ").concat(!hasForComponent ? '' : 'For, ', "\n ").concat(!((_b = json.hooks.onMount) === null || _b === void 0 ? void 0 : _b.code) ? '' : 'onMount, ', "\n } from 'solid-js';"), !foundDynamicComponents ? '' : "import { Dynamic } from 'solid-js/web';", !hasState ? '' : "import { createMutable } from 'solid-js/store';", !componentHasStyles
|
|
174
242
|
? ''
|
|
175
|
-
: "import { css } from \"solid-styled-components\";", (0, render_imports_1.renderPreComponent)(json), json.name, !hasState ? '' : "const state = createMutable(".concat(stateString, ");"), getRefsString(json), !((_c = json.hooks.onMount) === null || _c === void 0 ? void 0 : _c.code)
|
|
243
|
+
: "import { css } from \"solid-styled-components\";", (0, render_imports_1.renderPreComponent)(json), json.name, !hasState ? '' : "const state = createMutable(".concat(stateString, ");"), getRefsString(json), getContextString(json, options), !((_c = json.hooks.onMount) === null || _c === void 0 ? void 0 : _c.code)
|
|
176
244
|
? ''
|
|
177
|
-
: "onMount(() => { ".concat(json.hooks.onMount.code, " })"), addWrapper ? '<>' : '', json.children
|
|
245
|
+
: "onMount(() => { ".concat(json.hooks.onMount.code, " })"), addWrapper ? '<>' : '', json.children
|
|
246
|
+
.filter(filter_empty_text_nodes_1.filterEmptyTextNodes)
|
|
247
|
+
.map(function (item) { return blockToSolid(item, options); })
|
|
248
|
+
.join('\n'), addWrapper ? '</>' : '');
|
|
249
|
+
// HACK: for some reason we are generating `state.state.foo` instead of `state.foo`
|
|
250
|
+
// need a full fix, but this unblocks a lot in the short term
|
|
251
|
+
str = str.replace(/state\.state\./g, 'state.');
|
|
178
252
|
if (options.plugins) {
|
|
179
253
|
str = (0, plugins_1.runPreCodePlugins)(str, options.plugins);
|
|
180
254
|
}
|
|
@@ -163,7 +163,7 @@ var componentToStencil = function (options) {
|
|
|
163
163
|
*
|
|
164
164
|
* export default function ...
|
|
165
165
|
*/
|
|
166
|
-
((_b = json.meta.
|
|
166
|
+
((_b = json.meta.useMetadata) === null || _b === void 0 ? void 0 : _b.tagName) || (0, dash_case_1.dashCase)(json.name), css.length
|
|
167
167
|
? "styles: `\n ".concat((0, indent_1.indent)(css, 8), "`,")
|
|
168
168
|
: '', json.name, Array.from(props)
|
|
169
169
|
.map(function (item) { return "@Prop() ".concat(item, ": any"); })
|
|
@@ -171,9 +171,11 @@ var componentToStencil = function (options) {
|
|
|
171
171
|
? ''
|
|
172
172
|
: "componentDidLoad() { ".concat(processBinding(json.hooks.onMount.code), " }"), !((_d = json.hooks.onUnMount) === null || _d === void 0 ? void 0 : _d.code)
|
|
173
173
|
? ''
|
|
174
|
-
: "disconnectedCallback() { ".concat(processBinding(json.hooks.onUnMount.code), " }"), !((_e = json.hooks.onUpdate) === null || _e === void 0 ? void 0 : _e.
|
|
174
|
+
: "disconnectedCallback() { ".concat(processBinding(json.hooks.onUnMount.code), " }"), !((_e = json.hooks.onUpdate) === null || _e === void 0 ? void 0 : _e.length)
|
|
175
175
|
? ''
|
|
176
|
-
:
|
|
176
|
+
: json.hooks.onUpdate.map(function (hook) {
|
|
177
|
+
return "componentDidUpdate() { ".concat(processBinding(hook.code), " }");
|
|
178
|
+
}), wrap ? '<>' : '', json.children
|
|
177
179
|
.map(function (item) { return blockToStencil(item, options); })
|
|
178
180
|
.join('\n'), wrap ? '</>' : '');
|
|
179
181
|
if (options.plugins) {
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
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);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -1,7 +1,15 @@
|
|
|
1
|
+
import { MitosisComponent } from '../types/mitosis-component';
|
|
1
2
|
import { MitosisNode } from '../types/mitosis-node';
|
|
2
3
|
import { BaseTranspilerOptions, Transpiler } from '../types/config';
|
|
3
4
|
export interface ToSvelteOptions extends BaseTranspilerOptions {
|
|
4
5
|
stateType?: 'proxies' | 'variables';
|
|
5
6
|
}
|
|
6
|
-
|
|
7
|
+
interface BlockToSvelteProps {
|
|
8
|
+
json: MitosisNode;
|
|
9
|
+
options: ToSvelteOptions;
|
|
10
|
+
parentComponent: MitosisComponent;
|
|
11
|
+
}
|
|
12
|
+
declare type BlockToSvelte = (props: BlockToSvelteProps) => string;
|
|
13
|
+
export declare const blockToSvelte: BlockToSvelte;
|
|
7
14
|
export declare const componentToSvelte: (options?: ToSvelteOptions) => Transpiler;
|
|
15
|
+
export {};
|
|
@@ -35,17 +35,94 @@ var plugins_1 = require("../modules/plugins");
|
|
|
35
35
|
var is_children_1 = __importDefault(require("../helpers/is-children"));
|
|
36
36
|
var strip_meta_properties_1 = require("../helpers/strip-meta-properties");
|
|
37
37
|
var remove_surrounding_block_1 = require("../helpers/remove-surrounding-block");
|
|
38
|
+
var getters_to_functions_1 = require("../helpers/getters-to-functions");
|
|
39
|
+
var babel_transform_1 = require("../helpers/babel-transform");
|
|
40
|
+
var function_1 = require("fp-ts/lib/function");
|
|
41
|
+
var context_1 = require("./helpers/context");
|
|
42
|
+
var html_tags_1 = require("../constants/html_tags");
|
|
38
43
|
var mappers = {
|
|
39
|
-
Fragment: function (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
.
|
|
44
|
+
Fragment: function (_a) {
|
|
45
|
+
var json = _a.json, options = _a.options, parentComponent = _a.parentComponent;
|
|
46
|
+
if (json.bindings.innerHTML) {
|
|
47
|
+
return BINDINGS_MAPPER.innerHTML(json, options);
|
|
48
|
+
}
|
|
49
|
+
else if (json.children.length > 0) {
|
|
50
|
+
return "".concat(json.children
|
|
51
|
+
.map(function (item) { return (0, exports.blockToSvelte)({ json: item, options: options, parentComponent: parentComponent }); })
|
|
52
|
+
.join('\n'));
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
return '';
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
For: function (_a) {
|
|
59
|
+
var json = _a.json, options = _a.options, parentComponent = _a.parentComponent;
|
|
60
|
+
return "\n{#each ".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(json.bindings.each, {
|
|
61
|
+
includeState: options.stateType === 'variables',
|
|
62
|
+
}), " as ").concat(json.properties._forName, ", index }\n").concat(json.children
|
|
63
|
+
.map(function (item) { return (0, exports.blockToSvelte)({ json: item, options: options, parentComponent: parentComponent }); })
|
|
64
|
+
.join('\n'), "\n{/each}\n");
|
|
65
|
+
},
|
|
66
|
+
Show: function (_a) {
|
|
67
|
+
var json = _a.json, options = _a.options, parentComponent = _a.parentComponent;
|
|
68
|
+
return "\n{#if ".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(json.bindings.when, {
|
|
69
|
+
includeState: options.stateType === 'variables',
|
|
70
|
+
}), " }\n").concat(json.children
|
|
71
|
+
.map(function (item) { return (0, exports.blockToSvelte)({ json: item, options: options, parentComponent: parentComponent }); })
|
|
72
|
+
.join('\n'), "\n{/if}");
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
var getContextCode = function (json) {
|
|
76
|
+
var contextGetters = json.context.get;
|
|
77
|
+
return Object.keys(contextGetters)
|
|
78
|
+
.map(function (key) { return "let ".concat(key, " = getContext(").concat(contextGetters[key].name, ".key);"); })
|
|
79
|
+
.join('\n');
|
|
80
|
+
};
|
|
81
|
+
var setContextCode = function (json) {
|
|
82
|
+
var contextSetters = json.context.set;
|
|
83
|
+
return Object.keys(contextSetters)
|
|
84
|
+
.map(function (key) {
|
|
85
|
+
var _a = contextSetters[key], value = _a.value, name = _a.name;
|
|
86
|
+
return "setContext(".concat(name, ".key, ").concat(value ? (0, get_state_object_string_1.getMemberObjectString)(value) : 'undefined', ");");
|
|
87
|
+
})
|
|
88
|
+
.join('\n');
|
|
89
|
+
};
|
|
90
|
+
var BINDINGS_MAPPER = {
|
|
91
|
+
innerHTML: function (json, options) {
|
|
92
|
+
return "{@html ".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(json.bindings.innerHTML), "}");
|
|
43
93
|
},
|
|
44
94
|
};
|
|
45
|
-
var
|
|
95
|
+
var SVELTE_SPECIAL_TAGS = {
|
|
96
|
+
COMPONENT: 'svelte:component',
|
|
97
|
+
ELEMENT: 'svelte:element',
|
|
98
|
+
SELF: 'svelte:self',
|
|
99
|
+
};
|
|
100
|
+
var getTagName = function (_a) {
|
|
101
|
+
var json = _a.json, parentComponent = _a.parentComponent;
|
|
102
|
+
if (parentComponent && json.name === parentComponent.name) {
|
|
103
|
+
return SVELTE_SPECIAL_TAGS.SELF;
|
|
104
|
+
}
|
|
105
|
+
var isValidHtmlTag = html_tags_1.VALID_HTML_TAGS.includes(json.name);
|
|
106
|
+
var isSpecialSvelteTag = json.name.startsWith('svelte:');
|
|
107
|
+
// Check if any import matches `json.name`
|
|
108
|
+
var hasMatchingImport = parentComponent.imports.some(function (_a) {
|
|
109
|
+
var imports = _a.imports;
|
|
110
|
+
return Object.keys(imports).some(function (name) { return name === json.name; });
|
|
111
|
+
});
|
|
112
|
+
// TO-DO: no way to decide between <svelte:component> and <svelte:element>...need to do that through metadata
|
|
113
|
+
// overrides for now
|
|
114
|
+
if (!isValidHtmlTag && !isSpecialSvelteTag && !hasMatchingImport) {
|
|
115
|
+
json.bindings.this = json.name;
|
|
116
|
+
return SVELTE_SPECIAL_TAGS.COMPONENT;
|
|
117
|
+
}
|
|
118
|
+
return json.name;
|
|
119
|
+
};
|
|
120
|
+
var blockToSvelte = function (_a) {
|
|
121
|
+
var json = _a.json, options = _a.options, parentComponent = _a.parentComponent;
|
|
46
122
|
if (mappers[json.name]) {
|
|
47
|
-
return mappers[json.name](json, options);
|
|
123
|
+
return mappers[json.name]({ json: json, options: options, parentComponent: parentComponent });
|
|
48
124
|
}
|
|
125
|
+
var tagName = getTagName({ json: json, parentComponent: parentComponent });
|
|
49
126
|
if ((0, is_children_1.default)(json)) {
|
|
50
127
|
return "<slot></slot>";
|
|
51
128
|
}
|
|
@@ -58,63 +135,58 @@ var blockToSvelte = function (json, options) {
|
|
|
58
135
|
}), "}");
|
|
59
136
|
}
|
|
60
137
|
var str = '';
|
|
61
|
-
|
|
62
|
-
|
|
138
|
+
str += "<".concat(tagName, " ");
|
|
139
|
+
if (json.bindings._spread) {
|
|
140
|
+
str += "{...".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(json.bindings._spread, {
|
|
63
141
|
includeState: options.stateType === 'variables',
|
|
64
|
-
}), "
|
|
65
|
-
str += json.children.map(function (item) { return (0, exports.blockToSvelte)(item, options); }).join('\n');
|
|
66
|
-
str += "{/each}";
|
|
142
|
+
}), "}");
|
|
67
143
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}), " }");
|
|
72
|
-
str += json.children.map(function (item) { return (0, exports.blockToSvelte)(item, options); }).join('\n');
|
|
73
|
-
str += "{/if}";
|
|
144
|
+
for (var key in json.properties) {
|
|
145
|
+
var value = json.properties[key];
|
|
146
|
+
str += " ".concat(key, "=\"").concat(value, "\" ");
|
|
74
147
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
str += "{...".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(json.bindings._spread, {
|
|
79
|
-
includeState: options.stateType === 'variables',
|
|
80
|
-
}), "}");
|
|
148
|
+
for (var key in json.bindings) {
|
|
149
|
+
if (key === 'innerHTML') {
|
|
150
|
+
continue;
|
|
81
151
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
str += " ".concat(key, "=\"").concat(value, "\" ");
|
|
152
|
+
if (key === '_spread') {
|
|
153
|
+
continue;
|
|
85
154
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
var
|
|
93
|
-
|
|
94
|
-
});
|
|
95
|
-
if (key.startsWith('on')) {
|
|
96
|
-
var event_1 = key.replace('on', '').toLowerCase();
|
|
97
|
-
// TODO: handle quotes in event handler values
|
|
98
|
-
str += " on:".concat(event_1, "=\"{event => ").concat((0, remove_surrounding_block_1.removeSurroundingBlock)(useValue), "}\" ");
|
|
99
|
-
}
|
|
100
|
-
else if (key === 'ref') {
|
|
101
|
-
str += " bind:this={".concat(useValue, "} ");
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
str += " ".concat(key, "={").concat(useValue, "} ");
|
|
105
|
-
}
|
|
155
|
+
var value = json.bindings[key];
|
|
156
|
+
// TODO: proper babel transform to replace. Util for this
|
|
157
|
+
var useValue = (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(value, {
|
|
158
|
+
includeState: options.stateType === 'variables',
|
|
159
|
+
});
|
|
160
|
+
if (key.startsWith('on')) {
|
|
161
|
+
var event_1 = key.replace('on', '').toLowerCase();
|
|
162
|
+
// TODO: handle quotes in event handler values
|
|
163
|
+
str += " on:".concat(event_1, "=\"{event => ").concat((0, remove_surrounding_block_1.removeSurroundingBlock)(useValue), "}\" ");
|
|
106
164
|
}
|
|
107
|
-
if (
|
|
108
|
-
|
|
165
|
+
else if (key === 'ref') {
|
|
166
|
+
str += " bind:this={".concat(useValue, "} ");
|
|
109
167
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
str += json.children
|
|
113
|
-
.map(function (item) { return (0, exports.blockToSvelte)(item, options); })
|
|
114
|
-
.join('\n');
|
|
168
|
+
else {
|
|
169
|
+
str += " ".concat(key, "={").concat(useValue, "} ");
|
|
115
170
|
}
|
|
116
|
-
str += "</".concat(json.name, ">");
|
|
117
171
|
}
|
|
172
|
+
// if we have innerHTML, it doesn't matter whether we have closing tags or not, or children or not.
|
|
173
|
+
// we use the innerHTML content as children and don't render the self-closing tag.
|
|
174
|
+
if (json.bindings.innerHTML) {
|
|
175
|
+
str += '>';
|
|
176
|
+
str += BINDINGS_MAPPER.innerHTML(json, options);
|
|
177
|
+
str += "</".concat(tagName, ">");
|
|
178
|
+
return str;
|
|
179
|
+
}
|
|
180
|
+
if (jsx_1.selfClosingTags.has(tagName)) {
|
|
181
|
+
return str + ' />';
|
|
182
|
+
}
|
|
183
|
+
str += '>';
|
|
184
|
+
if (json.children) {
|
|
185
|
+
str += json.children
|
|
186
|
+
.map(function (item) { return (0, exports.blockToSvelte)({ json: item, options: options, parentComponent: parentComponent }); })
|
|
187
|
+
.join('\n');
|
|
188
|
+
}
|
|
189
|
+
str += "</".concat(tagName, ">");
|
|
118
190
|
return str;
|
|
119
191
|
};
|
|
120
192
|
exports.blockToSvelte = blockToSvelte;
|
|
@@ -140,6 +212,12 @@ var useBindValue = function (json, options) {
|
|
|
140
212
|
}
|
|
141
213
|
});
|
|
142
214
|
};
|
|
215
|
+
/**
|
|
216
|
+
* Removes all `this.` references.
|
|
217
|
+
*/
|
|
218
|
+
var stripThisRefs = function (str) {
|
|
219
|
+
return str.replace(/this\.([a-zA-Z_\$0-9]+)/g, '$1');
|
|
220
|
+
};
|
|
143
221
|
var componentToSvelte = function (options) {
|
|
144
222
|
if (options === void 0) { options = {}; }
|
|
145
223
|
return function (_a) {
|
|
@@ -153,12 +231,13 @@ var componentToSvelte = function (options) {
|
|
|
153
231
|
}
|
|
154
232
|
var refs = Array.from((0, get_refs_1.getRefs)(json));
|
|
155
233
|
useBindValue(json, useOptions);
|
|
234
|
+
(0, getters_to_functions_1.gettersToFunctions)(json);
|
|
156
235
|
if (useOptions.plugins) {
|
|
157
236
|
json = (0, plugins_1.runPostJsonPlugins)(json, useOptions.plugins);
|
|
158
237
|
}
|
|
159
238
|
var css = (0, collect_styles_1.collectCss)(json);
|
|
160
239
|
(0, strip_meta_properties_1.stripMetaProperties)(json);
|
|
161
|
-
var dataString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
240
|
+
var dataString = (0, function_1.pipe)((0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
162
241
|
data: true,
|
|
163
242
|
functions: false,
|
|
164
243
|
getters: false,
|
|
@@ -169,57 +248,77 @@ var componentToSvelte = function (options) {
|
|
|
169
248
|
includeState: useOptions.stateType === 'variables',
|
|
170
249
|
});
|
|
171
250
|
},
|
|
172
|
-
});
|
|
173
|
-
var getterString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
251
|
+
}), babel_transform_1.babelTransformCode);
|
|
252
|
+
var getterString = (0, function_1.pipe)((0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
174
253
|
data: false,
|
|
175
254
|
getters: true,
|
|
176
255
|
functions: false,
|
|
177
256
|
format: 'variables',
|
|
178
257
|
keyPrefix: '$: ',
|
|
179
258
|
valueMapper: function (code) {
|
|
180
|
-
return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code
|
|
259
|
+
return (0, function_1.pipe)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code
|
|
181
260
|
.replace(/^get ([a-zA-Z_\$0-9]+)/, '$1 = ')
|
|
182
261
|
.replace(/\)/, ') => '), {
|
|
183
262
|
includeState: useOptions.stateType === 'variables',
|
|
184
|
-
});
|
|
263
|
+
}), stripThisRefs);
|
|
185
264
|
},
|
|
186
|
-
});
|
|
187
|
-
var functionsString = (0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
265
|
+
}), babel_transform_1.babelTransformCode);
|
|
266
|
+
var functionsString = (0, function_1.pipe)((0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
188
267
|
data: false,
|
|
189
268
|
getters: false,
|
|
190
269
|
functions: true,
|
|
191
270
|
format: 'variables',
|
|
192
271
|
keyPrefix: 'function ',
|
|
193
272
|
valueMapper: function (code) {
|
|
194
|
-
return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
|
|
273
|
+
return (0, function_1.pipe)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
|
|
195
274
|
includeState: useOptions.stateType === 'variables',
|
|
196
|
-
});
|
|
275
|
+
}), stripThisRefs);
|
|
197
276
|
},
|
|
198
|
-
});
|
|
277
|
+
}), babel_transform_1.babelTransformCode);
|
|
199
278
|
var hasData = dataString.length > 4;
|
|
200
279
|
var props = Array.from((0, get_props_1.getProps)(json));
|
|
201
|
-
var
|
|
280
|
+
var transformHookCode = function (hookCode) {
|
|
281
|
+
return (0, function_1.pipe)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(hookCode, {
|
|
282
|
+
includeState: useOptions.stateType === 'variables',
|
|
283
|
+
}), babel_transform_1.babelTransformCode);
|
|
284
|
+
};
|
|
285
|
+
var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __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 </script>\n\n ", "\n\n ", "\n "], ["\n <script>\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 "])), !((_b = json.hooks.onMount) === null || _b === void 0 ? void 0 : _b.code) ? '' : "import { onMount } from 'svelte'", !((_c = json.hooks.onUpdate) === null || _c === void 0 ? void 0 : _c.length)
|
|
286
|
+
? ''
|
|
287
|
+
: "import { afterUpdate } from 'svelte'", !((_d = json.hooks.onUnMount) === null || _d === void 0 ? void 0 : _d.code) ? '' : "import { onDestroy } from 'svelte'", (0, render_imports_1.renderPreComponent)(json, 'svelte'), (0, context_1.hasContext)(component)
|
|
288
|
+
? 'import { getContext, setContext } from "svelte";'
|
|
289
|
+
: '', !hasData || useOptions.stateType === 'variables'
|
|
202
290
|
? ''
|
|
203
291
|
: "import onChange from 'on-change'", refs
|
|
204
292
|
.concat(props)
|
|
205
|
-
.map(function (name) {
|
|
206
|
-
|
|
293
|
+
.map(function (name) {
|
|
294
|
+
if (name === 'children') {
|
|
295
|
+
return '';
|
|
296
|
+
}
|
|
297
|
+
return "export let ".concat(name, ";");
|
|
298
|
+
})
|
|
299
|
+
.join('\n'), functionsString.length < 4 ? '' : functionsString, getterString.length < 4 ? '' : getterString, getContextCode(json), setContextCode(json), useOptions.stateType === 'proxies'
|
|
207
300
|
? dataString.length < 4
|
|
208
301
|
? ''
|
|
209
302
|
: "let state = onChange(".concat(dataString, ", () => state = state)")
|
|
210
303
|
: dataString, !((_e = json.hooks.onMount) === null || _e === void 0 ? void 0 : _e.code)
|
|
211
304
|
? ''
|
|
212
|
-
: "onMount(() => { ".concat((
|
|
213
|
-
includeState: useOptions.stateType === 'variables',
|
|
214
|
-
}), " });"), !((_f = json.hooks.onUpdate) === null || _f === void 0 ? void 0 : _f.code)
|
|
305
|
+
: "onMount(() => { ".concat(transformHookCode(json.hooks.onMount.code), " });"), !((_f = json.hooks.onUpdate) === null || _f === void 0 ? void 0 : _f.length)
|
|
215
306
|
? ''
|
|
216
|
-
:
|
|
217
|
-
|
|
218
|
-
|
|
307
|
+
: json.hooks.onUpdate
|
|
308
|
+
.map(function (hook) {
|
|
309
|
+
return "afterUpdate(() => { ".concat(transformHookCode(hook.code), " })");
|
|
310
|
+
})
|
|
311
|
+
.join(';'), !((_g = json.hooks.onUnMount) === null || _g === void 0 ? void 0 : _g.code)
|
|
219
312
|
? ''
|
|
220
|
-
: "onDestroy(() => { ".concat((
|
|
221
|
-
|
|
222
|
-
|
|
313
|
+
: "onDestroy(() => { ".concat(transformHookCode(json.hooks.onUnMount.code), " });"), json.children
|
|
314
|
+
.map(function (item) {
|
|
315
|
+
return (0, exports.blockToSvelte)({
|
|
316
|
+
json: item,
|
|
317
|
+
options: useOptions,
|
|
318
|
+
parentComponent: json,
|
|
319
|
+
});
|
|
320
|
+
})
|
|
321
|
+
.join('\n'), !css.trim().length
|
|
223
322
|
? ''
|
|
224
323
|
: "<style>\n ".concat(css, "\n </style>"));
|
|
225
324
|
if (useOptions.plugins) {
|
|
@@ -8,6 +8,6 @@ export interface ToVueOptions extends BaseTranspilerOptions {
|
|
|
8
8
|
registerComponentPrepend?: string;
|
|
9
9
|
}
|
|
10
10
|
export declare const blockToVue: (node: MitosisNode, options: ToVueOptions) => string;
|
|
11
|
-
export declare const componentToVue: (
|
|
11
|
+
export declare const componentToVue: (userOptions?: ToVueOptions) => ({ component, path }: TranspilerArgs & {
|
|
12
12
|
path: string;
|
|
13
13
|
}) => string;
|