@builder.io/mitosis 0.0.70 → 0.0.72-0
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/constants/hooks.d.ts +1 -0
- package/dist/src/constants/hooks.js +1 -0
- package/dist/src/generators/angular.d.ts +1 -0
- package/dist/src/generators/angular.js +7 -1
- package/dist/src/generators/react/generator.js +4 -2
- package/dist/src/generators/react/types.d.ts +1 -1
- package/dist/src/generators/rsc.d.ts +4 -0
- package/dist/src/generators/rsc.js +79 -0
- package/dist/src/generators/vue/blocks.d.ts +8 -0
- package/dist/src/generators/vue/blocks.js +294 -0
- package/dist/src/generators/vue/compositionApi.d.ts +3 -0
- package/dist/src/generators/vue/compositionApi.js +107 -0
- package/dist/src/generators/vue/helpers.d.ts +16 -0
- package/dist/src/generators/vue/helpers.js +62 -0
- package/dist/src/generators/vue/index.d.ts +2 -0
- package/dist/src/generators/vue/index.js +18 -0
- package/dist/src/generators/vue/optionsApi.d.ts +3 -0
- package/dist/src/generators/vue/optionsApi.js +186 -0
- package/dist/src/generators/vue/types.d.ts +12 -0
- package/dist/src/generators/vue/types.js +2 -0
- package/dist/src/generators/vue/vue.d.ts +6 -0
- package/dist/src/generators/vue/vue.js +227 -0
- package/dist/src/helpers/render-imports.d.ts +6 -3
- package/dist/src/helpers/render-imports.js +12 -8
- package/dist/src/helpers/styles/collect-css.js +4 -1
- package/dist/src/helpers/styles/helpers.js +5 -1
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +4 -1
- package/dist/src/parsers/builder.d.ts +2 -0
- package/dist/src/parsers/jsx/function-parser.js +3 -0
- package/dist/src/targets.d.ts +1 -0
- package/dist/src/targets.js +2 -0
- package/dist/src/types/mitosis-component.d.ts +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +3 -2
- package/dist/src/generators/vue.d.ts +0 -23
- package/dist/src/generators/vue.js +0 -778
|
@@ -3,6 +3,7 @@ import { BaseTranspilerOptions, TranspilerGenerator } from '../types/transpiler'
|
|
|
3
3
|
export interface ToAngularOptions extends BaseTranspilerOptions {
|
|
4
4
|
standalone?: boolean;
|
|
5
5
|
preserveImports?: boolean;
|
|
6
|
+
preserveFileExtensions?: boolean;
|
|
6
7
|
}
|
|
7
8
|
interface AngularBlockOptions {
|
|
8
9
|
contextVars?: string[];
|
|
@@ -220,6 +220,7 @@ var componentToAngular = function (userOptions) {
|
|
|
220
220
|
var _component = _a.component;
|
|
221
221
|
var DEFAULT_OPTIONS = {
|
|
222
222
|
preserveImports: false,
|
|
223
|
+
preserveFileExtensions: false,
|
|
223
224
|
};
|
|
224
225
|
var options = __assign(__assign({}, DEFAULT_OPTIONS), userOptions);
|
|
225
226
|
// Make a copy we can safely mutate, similar to babel's toolchain
|
|
@@ -317,7 +318,11 @@ var componentToAngular = function (userOptions) {
|
|
|
317
318
|
},
|
|
318
319
|
});
|
|
319
320
|
// Preparing built in component metadata parameters
|
|
320
|
-
var componentMetadata = __assign(__assign({ selector: "'".concat((0, lodash_1.kebabCase)(json.name || 'my-component'), ", ").concat(json.name, "'"), template: "`\n ".concat((0, indent_1.indent)(template, 8).replace(/`/g, '\\`').replace(/\$\{/g, '\\${'), "\n `") }, (css.length
|
|
321
|
+
var componentMetadata = __assign(__assign({ selector: "'".concat((0, lodash_1.kebabCase)(json.name || 'my-component'), ", ").concat(json.name, "'"), template: "`\n ".concat((0, indent_1.indent)(template, 8).replace(/`/g, '\\`').replace(/\$\{/g, '\\${'), "\n `") }, (css.length
|
|
322
|
+
? {
|
|
323
|
+
styles: "[`".concat((0, indent_1.indent)(css, 8), "`]"),
|
|
324
|
+
}
|
|
325
|
+
: {})), (options.standalone
|
|
321
326
|
? // TODO: also add child component imports here as well
|
|
322
327
|
{
|
|
323
328
|
standalone: 'true',
|
|
@@ -333,6 +338,7 @@ var componentToAngular = function (userOptions) {
|
|
|
333
338
|
component: json,
|
|
334
339
|
target: 'angular',
|
|
335
340
|
excludeMitosisComponents: !options.standalone && !options.preserveImports,
|
|
341
|
+
preserveFileExtensions: options.preserveFileExtensions,
|
|
336
342
|
}), Object.entries(componentMetadata)
|
|
337
343
|
.map(function (_a) {
|
|
338
344
|
var k = _a[0], v = _a[1];
|
|
@@ -430,8 +430,10 @@ var _componentToReact = function (json, options, isSubComponent) {
|
|
|
430
430
|
: stateType === 'solid'
|
|
431
431
|
? "const state = useMutable(".concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json), ");")
|
|
432
432
|
: stateType === 'builder'
|
|
433
|
-
? "
|
|
434
|
-
:
|
|
433
|
+
? "const state = useBuilderState(".concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json), ");")
|
|
434
|
+
: stateType === 'variables'
|
|
435
|
+
? "const state = ".concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json), ";")
|
|
436
|
+
: "const state = useLocalProxy(".concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json), ");")
|
|
435
437
|
: '', hasStateArgument ? refsString : '', getContextString(json, options), getInitCode(json, options), ((_g = json.hooks.onInit) === null || _g === void 0 ? void 0 : _g.code)
|
|
436
438
|
? "\n useEffect(() => {\n ".concat((0, state_2.processHookCode)({
|
|
437
439
|
str: json.hooks.onInit.code,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BaseTranspilerOptions } from '../../types/transpiler';
|
|
2
2
|
export interface ToReactOptions extends BaseTranspilerOptions {
|
|
3
3
|
stylesType?: 'emotion' | 'styled-components' | 'styled-jsx' | 'react-native' | 'style-tag';
|
|
4
|
-
stateType?: 'useState' | 'mobx' | 'valtio' | 'solid' | 'builder';
|
|
4
|
+
stateType?: 'useState' | 'mobx' | 'valtio' | 'solid' | 'builder' | 'variables';
|
|
5
5
|
format?: 'lite' | 'safe';
|
|
6
6
|
type?: 'dom' | 'native';
|
|
7
7
|
preact?: boolean;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
14
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
15
|
+
if (ar || !(i in from)) {
|
|
16
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
17
|
+
ar[i] = from[i];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
21
|
+
};
|
|
22
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.componentToRsc = void 0;
|
|
27
|
+
var traverse_1 = __importDefault(require("traverse"));
|
|
28
|
+
var fast_clone_1 = require("../helpers/fast-clone");
|
|
29
|
+
var is_mitosis_node_1 = require("../helpers/is-mitosis-node");
|
|
30
|
+
var react_1 = require("./react");
|
|
31
|
+
/**
|
|
32
|
+
* Transform react to be RSC compatible, such as
|
|
33
|
+
* - remove event listeners
|
|
34
|
+
* - remove lifecycle hooks
|
|
35
|
+
* - remove refs
|
|
36
|
+
*/
|
|
37
|
+
var RSC_TRANSFORM_PLUGIN = function () { return ({
|
|
38
|
+
json: {
|
|
39
|
+
pre: function (json) {
|
|
40
|
+
if (json.hooks.onMount) {
|
|
41
|
+
delete json.hooks.onMount;
|
|
42
|
+
}
|
|
43
|
+
if (json.hooks.onUnMount) {
|
|
44
|
+
delete json.hooks.onUnMount;
|
|
45
|
+
}
|
|
46
|
+
if (json.hooks.onUpdate) {
|
|
47
|
+
delete json.hooks.onUpdate;
|
|
48
|
+
}
|
|
49
|
+
if (json.refs) {
|
|
50
|
+
json.refs = {};
|
|
51
|
+
}
|
|
52
|
+
(0, traverse_1.default)(json).forEach(function (node) {
|
|
53
|
+
if ((0, is_mitosis_node_1.isMitosisNode)(node)) {
|
|
54
|
+
if (node.bindings.ref) {
|
|
55
|
+
delete node.bindings.ref;
|
|
56
|
+
}
|
|
57
|
+
for (var key in node.bindings) {
|
|
58
|
+
if (key.match(/^on[A-Z]/)) {
|
|
59
|
+
delete node.bindings[key];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
}); };
|
|
67
|
+
var DEFAULT_OPTIONS = {
|
|
68
|
+
plugins: [RSC_TRANSFORM_PLUGIN],
|
|
69
|
+
};
|
|
70
|
+
var componentToRsc = function (_options) {
|
|
71
|
+
if (_options === void 0) { _options = {}; }
|
|
72
|
+
return function (_a) {
|
|
73
|
+
var component = _a.component, path = _a.path;
|
|
74
|
+
var json = (0, fast_clone_1.fastClone)(component);
|
|
75
|
+
var options = __assign(__assign(__assign({}, DEFAULT_OPTIONS), _options), { plugins: __spreadArray(__spreadArray([], (DEFAULT_OPTIONS.plugins || []), true), (_options.plugins || []), true), stylesType: 'style-tag', stateType: 'variables' });
|
|
76
|
+
return (0, react_1.componentToReact)(options)({ component: json, path: path });
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
exports.componentToRsc = componentToRsc;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { MitosisNode } from '../../types/mitosis-node';
|
|
2
|
+
import { ToVueOptions } from './types';
|
|
3
|
+
declare type BlockRenderer = (json: MitosisNode, options: ToVueOptions, scope?: Scope) => string;
|
|
4
|
+
interface Scope {
|
|
5
|
+
isRootNode?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare const blockToVue: BlockRenderer;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.blockToVue = void 0;
|
|
18
|
+
var function_1 = require("fp-ts/lib/function");
|
|
19
|
+
var filter_empty_text_nodes_1 = require("../../helpers/filter-empty-text-nodes");
|
|
20
|
+
var is_children_1 = __importDefault(require("../../helpers/is-children"));
|
|
21
|
+
var is_mitosis_node_1 = require("../../helpers/is-mitosis-node");
|
|
22
|
+
var remove_surrounding_block_1 = require("../../helpers/remove-surrounding-block");
|
|
23
|
+
var replace_identifiers_1 = require("../../helpers/replace-identifiers");
|
|
24
|
+
var slots_1 = require("../../helpers/slots");
|
|
25
|
+
var strip_state_and_props_refs_1 = require("../../helpers/strip-state-and-props-refs");
|
|
26
|
+
var jsx_1 = require("../../parsers/jsx");
|
|
27
|
+
var helpers_1 = require("./helpers");
|
|
28
|
+
var SPECIAL_PROPERTIES = {
|
|
29
|
+
V_IF: 'v-if',
|
|
30
|
+
V_FOR: 'v-for',
|
|
31
|
+
V_ELSE: 'v-else',
|
|
32
|
+
V_ELSE_IF: 'v-else-if',
|
|
33
|
+
};
|
|
34
|
+
// TODO: Maybe in the future allow defining `string | function` as values
|
|
35
|
+
var BINDING_MAPPERS = {
|
|
36
|
+
innerHTML: 'v-html',
|
|
37
|
+
};
|
|
38
|
+
var NODE_MAPPERS = {
|
|
39
|
+
Fragment: function (json, options, scope) {
|
|
40
|
+
if (options.vueVersion === 2 && (scope === null || scope === void 0 ? void 0 : scope.isRootNode)) {
|
|
41
|
+
throw new Error('Vue 2 template should have a single root element');
|
|
42
|
+
}
|
|
43
|
+
return json.children.map(function (item) { return (0, exports.blockToVue)(item, options); }).join('\n');
|
|
44
|
+
},
|
|
45
|
+
For: function (_json, options) {
|
|
46
|
+
var _a, _b;
|
|
47
|
+
var _c;
|
|
48
|
+
var json = _json;
|
|
49
|
+
var keyValue = json.bindings.key || { code: 'index' };
|
|
50
|
+
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));
|
|
51
|
+
if (options.vueVersion >= 3) {
|
|
52
|
+
// TODO: tmk key goes on different element (parent vs child) based on Vue 2 vs Vue 3
|
|
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>");
|
|
54
|
+
}
|
|
55
|
+
// Vue 2 can only handle one root element
|
|
56
|
+
var firstChild = json.children.filter(filter_empty_text_nodes_1.filterEmptyTextNodes)[0];
|
|
57
|
+
// Edge-case for when the parent is a `Show`, we need to pass down the `v-if` prop.
|
|
58
|
+
var jsonIf = json.properties[SPECIAL_PROPERTIES.V_IF];
|
|
59
|
+
return firstChild
|
|
60
|
+
? (0, function_1.pipe)(firstChild, (0, helpers_1.addBindingsToJson)({ key: keyValue }), (0, helpers_1.addPropertiesToJson)(__assign((_a = {}, _a[SPECIAL_PROPERTIES.V_FOR] = forValue, _a), (jsonIf ? (_b = {}, _b[SPECIAL_PROPERTIES.V_IF] = jsonIf, _b) : {}))), function (block) { return (0, exports.blockToVue)(block, options); })
|
|
61
|
+
: '';
|
|
62
|
+
},
|
|
63
|
+
Show: function (json, options, scope) {
|
|
64
|
+
var _a, _b, _c, _d, _e, _f;
|
|
65
|
+
var _g, _h;
|
|
66
|
+
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); });
|
|
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)
|
|
68
|
+
? "\n <template ".concat(SPECIAL_PROPERTIES.V_ELSE, ">\n ").concat((0, exports.blockToVue)(json.meta.else, options), "\n </template>")
|
|
69
|
+
: '', "\n ");
|
|
70
|
+
switch (options.vueVersion) {
|
|
71
|
+
case 3:
|
|
72
|
+
return defaultShowTemplate;
|
|
73
|
+
case 2:
|
|
74
|
+
// if it is not the root node, the default show template can be used
|
|
75
|
+
// as Vue 2 only has this limitation at root level
|
|
76
|
+
if (!(scope === null || scope === void 0 ? void 0 : scope.isRootNode)) {
|
|
77
|
+
return defaultShowTemplate;
|
|
78
|
+
}
|
|
79
|
+
var children_1 = json.children.filter(filter_empty_text_nodes_1.filterEmptyTextNodes);
|
|
80
|
+
// Vue 2 can only handle one root element, so we just take the first one.
|
|
81
|
+
// TO-DO: warn user of multi-children Show.
|
|
82
|
+
var firstChild = children_1[0];
|
|
83
|
+
var elseBlock = json.meta.else;
|
|
84
|
+
var hasShowChild = (firstChild === null || firstChild === void 0 ? void 0 : firstChild.name) === 'Show';
|
|
85
|
+
var childElseBlock = firstChild === null || firstChild === void 0 ? void 0 : firstChild.meta.else;
|
|
86
|
+
var allShowChildrenWithoutElse = children_1.every(function (x) { return x.name === 'Show' && !x.meta.else; });
|
|
87
|
+
if (allShowChildrenWithoutElse && (0, is_mitosis_node_1.isMitosisNode)(elseBlock)) {
|
|
88
|
+
/**
|
|
89
|
+
* This is when we mimic an if-else chain by only providing `Show` elements as children, none of which have an `else` block
|
|
90
|
+
*
|
|
91
|
+
* <show when={foo} else={else-1}>
|
|
92
|
+
* <show when={bar}> <bar-code> </show>
|
|
93
|
+
* <show when={x}> <x-code> </show>
|
|
94
|
+
* <show when={y}> <y-code> </show>
|
|
95
|
+
* </show>
|
|
96
|
+
*
|
|
97
|
+
* What we want in this case is:
|
|
98
|
+
*
|
|
99
|
+
* <else-1 if={!foo} />
|
|
100
|
+
* <bar-code v-else-if={bar} />
|
|
101
|
+
* <x-code v-else-if={x} />
|
|
102
|
+
* <y-code v-else />
|
|
103
|
+
*/
|
|
104
|
+
var ifString = (0, function_1.pipe)(elseBlock, (0, helpers_1.addPropertiesToJson)((_a = {}, _a[SPECIAL_PROPERTIES.V_IF] = (0, helpers_1.invertBooleanExpression)(ifValue), _a)), function (block) { return (0, exports.blockToVue)(block, options); });
|
|
105
|
+
var childrenStrings = children_1.map(function (child, idx) {
|
|
106
|
+
var _a, _b;
|
|
107
|
+
var _c;
|
|
108
|
+
var isLast = idx === children_1.length - 1;
|
|
109
|
+
var innerBlock = child.children.filter(filter_empty_text_nodes_1.filterEmptyTextNodes)[0];
|
|
110
|
+
if (!isLast) {
|
|
111
|
+
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);
|
|
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); });
|
|
113
|
+
return elseIfString;
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
var elseString = (0, function_1.pipe)(innerBlock, (0, helpers_1.addPropertiesToJson)((_b = {}, _b[SPECIAL_PROPERTIES.V_ELSE] = '', _b)), function (block) { return (0, exports.blockToVue)(block, options); });
|
|
117
|
+
return elseString;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
return "\n ".concat(ifString, "\n ").concat(childrenStrings.join('\n'), "\n ");
|
|
121
|
+
}
|
|
122
|
+
else if (firstChild &&
|
|
123
|
+
(0, is_mitosis_node_1.isMitosisNode)(elseBlock) &&
|
|
124
|
+
hasShowChild &&
|
|
125
|
+
(0, is_mitosis_node_1.isMitosisNode)(childElseBlock)) {
|
|
126
|
+
/**
|
|
127
|
+
* This is special edge logic to handle 2 nested Show elements in Vue 2.
|
|
128
|
+
* We need to invert the logic to make it work, due to no-template-root-element limitations in Vue 2.
|
|
129
|
+
*
|
|
130
|
+
* <show when={foo} else={else-1}>
|
|
131
|
+
* <show when={bar}> <bar-code> </show>
|
|
132
|
+
*
|
|
133
|
+
* <show when={x}> <x-code> </show>
|
|
134
|
+
*
|
|
135
|
+
* <show when={y}> <y-code> </show>
|
|
136
|
+
* </show>
|
|
137
|
+
*
|
|
138
|
+
*
|
|
139
|
+
*
|
|
140
|
+
*
|
|
141
|
+
* foo: true && bar: true => if-code
|
|
142
|
+
* foo: true && bar: false => else-2
|
|
143
|
+
* foo: false && bar: true?? => else-1
|
|
144
|
+
*
|
|
145
|
+
*
|
|
146
|
+
* map to:
|
|
147
|
+
*
|
|
148
|
+
* <else-1 if={!foo} />
|
|
149
|
+
* <else-2 v-else-if={!bar} />
|
|
150
|
+
* <if-code v-else />
|
|
151
|
+
*
|
|
152
|
+
*/
|
|
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); });
|
|
154
|
+
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);
|
|
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); });
|
|
156
|
+
var firstChildOfFirstChild = firstChild.children.filter(filter_empty_text_nodes_1.filterEmptyTextNodes)[0];
|
|
157
|
+
var elseString = firstChildOfFirstChild
|
|
158
|
+
? (0, function_1.pipe)(firstChildOfFirstChild, (0, helpers_1.addPropertiesToJson)((_d = {}, _d[SPECIAL_PROPERTIES.V_ELSE] = '', _d)), function (block) { return (0, exports.blockToVue)(block, options); })
|
|
159
|
+
: '';
|
|
160
|
+
return "\n\n ".concat(ifString, "\n\n ").concat(elseIfString, "\n\n ").concat(elseString, "\n\n ");
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
var ifString = firstChild
|
|
164
|
+
? (0, function_1.pipe)(firstChild, (0, helpers_1.addPropertiesToJson)((_e = {}, _e[SPECIAL_PROPERTIES.V_IF] = ifValue, _e)), function (block) { return (0, exports.blockToVue)(block, options); })
|
|
165
|
+
: '';
|
|
166
|
+
var elseString = (0, is_mitosis_node_1.isMitosisNode)(elseBlock)
|
|
167
|
+
? (0, function_1.pipe)(elseBlock, (0, helpers_1.addPropertiesToJson)((_f = {}, _f[SPECIAL_PROPERTIES.V_ELSE] = '', _f)), function (block) {
|
|
168
|
+
return (0, exports.blockToVue)(block, options);
|
|
169
|
+
})
|
|
170
|
+
: '';
|
|
171
|
+
return "\n ".concat(ifString, "\n ").concat(elseString, "\n ");
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
Slot: function (json, options) {
|
|
176
|
+
var _a, _b;
|
|
177
|
+
if (!json.bindings.name) {
|
|
178
|
+
var key = Object.keys(json.bindings).find(Boolean);
|
|
179
|
+
if (!key)
|
|
180
|
+
return '<slot />';
|
|
181
|
+
return "\n <template #".concat(key, ">\n ").concat((_a = json.bindings[key]) === null || _a === void 0 ? void 0 : _a.code, "\n </template>\n ");
|
|
182
|
+
}
|
|
183
|
+
var strippedTextCode = (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(json.bindings.name.code);
|
|
184
|
+
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>");
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
var stringifyBinding = function (node) {
|
|
188
|
+
return function (_a) {
|
|
189
|
+
var key = _a[0], value = _a[1];
|
|
190
|
+
if (key === '_spread') {
|
|
191
|
+
return '';
|
|
192
|
+
}
|
|
193
|
+
else if (key === 'class') {
|
|
194
|
+
return " :class=\"_classStringToObject(".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(value === null || value === void 0 ? void 0 : value.code, {
|
|
195
|
+
replaceWith: '',
|
|
196
|
+
}), ")\" ");
|
|
197
|
+
// TODO: support dynamic classes as objects somehow like Vue requires
|
|
198
|
+
// https://vuejs.org/v2/guide/class-and-style.html
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
// TODO: proper babel transform to replace. Util for this
|
|
202
|
+
var useValue = (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(value === null || value === void 0 ? void 0 : value.code);
|
|
203
|
+
if (key.startsWith('on')) {
|
|
204
|
+
var _b = value.arguments, cusArgs = _b === void 0 ? ['event'] : _b;
|
|
205
|
+
var event_1 = key.replace('on', '').toLowerCase();
|
|
206
|
+
if (event_1 === 'change' && node.name === 'input') {
|
|
207
|
+
event_1 = 'input';
|
|
208
|
+
}
|
|
209
|
+
var isAssignmentExpression = useValue.includes('=');
|
|
210
|
+
var valueWRenamedEvent = (0, replace_identifiers_1.replaceIdentifiers)({
|
|
211
|
+
code: useValue,
|
|
212
|
+
from: cusArgs[0],
|
|
213
|
+
to: '$event',
|
|
214
|
+
});
|
|
215
|
+
// TODO: proper babel transform to replace. Util for this
|
|
216
|
+
if (isAssignmentExpression) {
|
|
217
|
+
return " @".concat(event_1, "=\"").concat((0, helpers_1.encodeQuotes)((0, remove_surrounding_block_1.removeSurroundingBlock)(valueWRenamedEvent)), "\" ");
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
return " @".concat(event_1, "=\"").concat((0, helpers_1.encodeQuotes)((0, remove_surrounding_block_1.removeSurroundingBlock)((0, remove_surrounding_block_1.removeSurroundingBlock)(valueWRenamedEvent))), "\" ");
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
else if (key === 'ref') {
|
|
224
|
+
return " ref=\"".concat((0, helpers_1.encodeQuotes)(useValue), "\" ");
|
|
225
|
+
}
|
|
226
|
+
else if (BINDING_MAPPERS[key]) {
|
|
227
|
+
return " ".concat(BINDING_MAPPERS[key], "=\"").concat((0, helpers_1.encodeQuotes)(useValue.replace(/"/g, "\\'")), "\" ");
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
return " :".concat(key, "=\"").concat((0, helpers_1.encodeQuotes)(useValue), "\" ");
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
};
|
|
235
|
+
var blockToVue = function (node, options, scope) {
|
|
236
|
+
var _a, _b;
|
|
237
|
+
var nodeMapper = NODE_MAPPERS[node.name];
|
|
238
|
+
if (nodeMapper) {
|
|
239
|
+
return nodeMapper(node, options, scope);
|
|
240
|
+
}
|
|
241
|
+
if ((0, is_children_1.default)(node)) {
|
|
242
|
+
return "<slot/>";
|
|
243
|
+
}
|
|
244
|
+
if (node.name === 'style') {
|
|
245
|
+
// Vue doesn't allow <style>...</style> in templates, but does support the synonymous
|
|
246
|
+
// <component is="'style'">...</component>
|
|
247
|
+
node.name = 'component';
|
|
248
|
+
node.bindings.is = { code: "'style'" };
|
|
249
|
+
}
|
|
250
|
+
if (node.properties._text) {
|
|
251
|
+
return "".concat(node.properties._text);
|
|
252
|
+
}
|
|
253
|
+
var textCode = (_a = node.bindings._text) === null || _a === void 0 ? void 0 : _a.code;
|
|
254
|
+
if (textCode) {
|
|
255
|
+
var strippedTextCode = (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(textCode);
|
|
256
|
+
if ((0, slots_1.isSlotProperty)(strippedTextCode)) {
|
|
257
|
+
return "<slot name=\"".concat((0, slots_1.stripSlotPrefix)(strippedTextCode).toLowerCase(), "\"/>");
|
|
258
|
+
}
|
|
259
|
+
return "{{".concat(strippedTextCode, "}}");
|
|
260
|
+
}
|
|
261
|
+
var str = '';
|
|
262
|
+
str += "<".concat(node.name, " ");
|
|
263
|
+
if ((_b = node.bindings._spread) === null || _b === void 0 ? void 0 : _b.code) {
|
|
264
|
+
str += "v-bind=\"".concat((0, helpers_1.encodeQuotes)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(node.bindings._spread.code)), "\"");
|
|
265
|
+
}
|
|
266
|
+
for (var key in node.properties) {
|
|
267
|
+
var value = node.properties[key];
|
|
268
|
+
if (key === 'className') {
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
else if (key === SPECIAL_PROPERTIES.V_ELSE) {
|
|
272
|
+
str += " ".concat(key, " ");
|
|
273
|
+
}
|
|
274
|
+
else if (typeof value === 'string') {
|
|
275
|
+
str += " ".concat(key, "=\"").concat((0, helpers_1.encodeQuotes)(value), "\" ");
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
var stringifiedBindings = Object.entries(node.bindings)
|
|
279
|
+
.map(function (_a) {
|
|
280
|
+
var k = _a[0], v = _a[1];
|
|
281
|
+
return stringifyBinding(node)([k, v]);
|
|
282
|
+
})
|
|
283
|
+
.join('');
|
|
284
|
+
str += stringifiedBindings;
|
|
285
|
+
if (jsx_1.selfClosingTags.has(node.name)) {
|
|
286
|
+
return str + ' />';
|
|
287
|
+
}
|
|
288
|
+
str += '>';
|
|
289
|
+
if (node.children) {
|
|
290
|
+
str += node.children.map(function (item) { return (0, exports.blockToVue)(item, options); }).join('');
|
|
291
|
+
}
|
|
292
|
+
return str + "</".concat(node.name, ">");
|
|
293
|
+
};
|
|
294
|
+
exports.blockToVue = blockToVue;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { MitosisComponent, extendedHook } from '../../types/mitosis-component';
|
|
2
|
+
import { ToVueOptions } from './types';
|
|
3
|
+
export declare function generateCompositionApiScript(component: MitosisComponent, options: ToVueOptions, template: string, props: Array<string>, onUpdateWithDeps: extendedHook[], onUpdateWithoutDeps: extendedHook[]): string;
|
|
@@ -0,0 +1,107 @@
|
|
|
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.generateCompositionApiScript = void 0;
|
|
11
|
+
var dedent_1 = __importDefault(require("dedent"));
|
|
12
|
+
var json5_1 = __importDefault(require("json5"));
|
|
13
|
+
var lodash_1 = require("lodash");
|
|
14
|
+
var babel_transform_1 = require("../../helpers/babel-transform");
|
|
15
|
+
var get_state_object_string_1 = require("../../helpers/get-state-object-string");
|
|
16
|
+
var strip_state_and_props_refs_1 = require("../../helpers/strip-state-and-props-refs");
|
|
17
|
+
var core_1 = require("@babel/core");
|
|
18
|
+
var helpers_1 = require("./helpers");
|
|
19
|
+
var getCompositionPropDefinition = function (_a) {
|
|
20
|
+
var options = _a.options, component = _a.component, props = _a.props;
|
|
21
|
+
var str = 'const props = ';
|
|
22
|
+
if (component.defaultProps) {
|
|
23
|
+
var generic = options.typescript ? "<".concat(component.propsTypeRef, ">") : '';
|
|
24
|
+
str += "withDefaults(defineProps".concat(generic, "(), ").concat(json5_1.default.stringify(component.defaultProps), ")");
|
|
25
|
+
}
|
|
26
|
+
else if (options.typescript && component.propsTypeRef && component.propsTypeRef !== 'any') {
|
|
27
|
+
str += "defineProps<".concat(component.propsTypeRef, ">()");
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
str += "defineProps(".concat(json5_1.default.stringify(props), ")");
|
|
31
|
+
}
|
|
32
|
+
return str;
|
|
33
|
+
};
|
|
34
|
+
function appendValueToRefs(input, component, options) {
|
|
35
|
+
var refKeys = Object.keys((0, lodash_1.pickBy)(component.state, function (i) { return (i === null || i === void 0 ? void 0 : i.type) === 'property'; }));
|
|
36
|
+
var output = (0, helpers_1.processBinding)({ code: input, options: options, json: component, includeProps: false });
|
|
37
|
+
return (0, babel_transform_1.babelTransformExpression)(output, {
|
|
38
|
+
Identifier: function (path) {
|
|
39
|
+
if (!(core_1.types.isFunctionDeclaration(path.parent) && path.parent.id === path.node) &&
|
|
40
|
+
!core_1.types.isCallExpression(path.parent) &&
|
|
41
|
+
(!core_1.types.isMemberExpression(path.parent) || core_1.types.isThisExpression(path.parent.object)) &&
|
|
42
|
+
path.parentPath.listKey !== 'arguments' &&
|
|
43
|
+
path.parentPath.listKey !== 'params' &&
|
|
44
|
+
refKeys.includes(path.node.name)) {
|
|
45
|
+
path.replaceWith(core_1.types.identifier("".concat(path.node.name, ".value")));
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
function generateCompositionApiScript(component, options, template, props, onUpdateWithDeps, onUpdateWithoutDeps) {
|
|
51
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
52
|
+
var refs = (0, get_state_object_string_1.getStateObjectStringFromComponent)(component, {
|
|
53
|
+
data: true,
|
|
54
|
+
functions: false,
|
|
55
|
+
getters: false,
|
|
56
|
+
format: 'variables',
|
|
57
|
+
valueMapper: function (code) {
|
|
58
|
+
return (0, helpers_1.processBinding)({ code: "ref(".concat(code, ")"), options: options, json: component });
|
|
59
|
+
},
|
|
60
|
+
keyPrefix: 'const',
|
|
61
|
+
});
|
|
62
|
+
var methods = (0, get_state_object_string_1.getStateObjectStringFromComponent)(component, {
|
|
63
|
+
data: false,
|
|
64
|
+
getters: false,
|
|
65
|
+
functions: true,
|
|
66
|
+
valueMapper: function (code) { return (0, helpers_1.processBinding)({ code: code, options: options, json: component, includeProps: false }); },
|
|
67
|
+
format: 'variables',
|
|
68
|
+
});
|
|
69
|
+
if (template.includes('_classStringToObject')) {
|
|
70
|
+
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 } ";
|
|
71
|
+
}
|
|
72
|
+
var getterKeys = Object.keys((0, lodash_1.pickBy)(component.state, function (i) { return (i === null || i === void 0 ? void 0 : i.type) === 'getter'; }));
|
|
73
|
+
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) {
|
|
74
|
+
if (options.typescript) {
|
|
75
|
+
return "const ".concat(key, " = ref<").concat(component.refs[key].typeParameter, ">()");
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
return "const ".concat(key, " = ref(null)");
|
|
79
|
+
}
|
|
80
|
+
}).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)
|
|
81
|
+
? ''
|
|
82
|
+
: "onMounted(() => { ".concat(appendValueToRefs(component.hooks.onMount.code, component, options), "})"), !((_g = component.hooks.onUnMount) === null || _g === void 0 ? void 0 : _g.code)
|
|
83
|
+
? ''
|
|
84
|
+
: "onMounted(() => { ".concat(appendValueToRefs(component.hooks.onUnMount.code, component, options), "})"), !getterKeys
|
|
85
|
+
? ''
|
|
86
|
+
: getterKeys
|
|
87
|
+
.map(function (key) {
|
|
88
|
+
var _a, _b;
|
|
89
|
+
var code = (_b = (_a = component.state[key]) === null || _a === void 0 ? void 0 : _a.code) === null || _b === void 0 ? void 0 : _b.toString();
|
|
90
|
+
return !code
|
|
91
|
+
? ''
|
|
92
|
+
: "const ".concat(key, " = computed(").concat(appendValueToRefs(code.replace(key, '').replace('get ()', '() =>'), component, options), ")");
|
|
93
|
+
})
|
|
94
|
+
.join('\n'), !(onUpdateWithoutDeps === null || onUpdateWithoutDeps === void 0 ? void 0 : onUpdateWithoutDeps.length)
|
|
95
|
+
? ''
|
|
96
|
+
: onUpdateWithoutDeps.map(function (hook) {
|
|
97
|
+
return "onUpdated(() => ".concat(appendValueToRefs(hook.code, component, options), ")");
|
|
98
|
+
}), !(onUpdateWithDeps === null || onUpdateWithDeps === void 0 ? void 0 : onUpdateWithDeps.length)
|
|
99
|
+
? ''
|
|
100
|
+
: onUpdateWithDeps.map(function (hook) {
|
|
101
|
+
return appendValueToRefs("watch(".concat(hook.deps, ", (").concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(hook.deps), ") => { ").concat(hook.code, "})\n"), component, options);
|
|
102
|
+
}), (methods === null || methods === void 0 ? void 0 : methods.length) ? appendValueToRefs(methods, component, options) : '');
|
|
103
|
+
str = str.replace(/this\./g, ''); // strip this elsewhere (e.g. functions)
|
|
104
|
+
return str;
|
|
105
|
+
}
|
|
106
|
+
exports.generateCompositionApiScript = generateCompositionApiScript;
|
|
107
|
+
var templateObject_1;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { MitosisComponent } from '../../types/mitosis-component';
|
|
2
|
+
import { MitosisNode } from '../../types/mitosis-node';
|
|
3
|
+
import { ToVueOptions } from './types';
|
|
4
|
+
export declare const addPropertiesToJson: (properties: MitosisNode['properties']) => (json: MitosisNode) => MitosisNode;
|
|
5
|
+
export declare const addBindingsToJson: (bindings: MitosisNode['bindings']) => (json: MitosisNode) => MitosisNode;
|
|
6
|
+
export declare const getOnUpdateHookName: (index: number) => string;
|
|
7
|
+
export declare const invertBooleanExpression: (expression: string) => string;
|
|
8
|
+
export declare function encodeQuotes(string: string): string;
|
|
9
|
+
export declare const renameMitosisComponentsToKebabCase: (str: string) => string;
|
|
10
|
+
export declare function getContextNames(json: MitosisComponent): string[];
|
|
11
|
+
export declare function processBinding({ code, options, json, includeProps, }: {
|
|
12
|
+
code: string;
|
|
13
|
+
options: ToVueOptions;
|
|
14
|
+
json: MitosisComponent;
|
|
15
|
+
includeProps?: boolean;
|
|
16
|
+
}): string;
|