@builder.io/mitosis 0.0.76 → 0.0.77
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/generators/alpine/generate.d.ts +20 -0
- package/dist/src/generators/alpine/generate.js +207 -0
- package/dist/src/generators/alpine/index.d.ts +1 -0
- package/dist/src/generators/alpine/index.js +17 -0
- package/dist/src/generators/alpine/render-mount-hook.d.ts +2 -0
- package/dist/src/generators/alpine/render-mount-hook.js +14 -0
- package/dist/src/generators/alpine/render-update-hooks.d.ts +5 -0
- package/dist/src/generators/alpine/render-update-hooks.js +44 -0
- package/dist/src/generators/angular.d.ts +0 -3
- package/dist/src/generators/angular.js +79 -95
- package/dist/src/generators/helpers/functions.js +2 -1
- package/dist/src/generators/html.js +1 -1
- package/dist/src/generators/qwik/component-generator.js +8 -1
- package/dist/src/generators/svelte/blocks.js +19 -21
- package/dist/src/generators/svelte/helpers.d.ts +5 -1
- package/dist/src/generators/svelte/helpers.js +8 -5
- package/dist/src/generators/svelte/svelte.js +34 -31
- package/dist/src/generators/swift-ui.js +1 -1
- package/dist/src/generators/vue/blocks.js +4 -2
- package/dist/src/generators/vue/compositionApi.js +3 -1
- package/dist/src/generators/vue/helpers.d.ts +2 -2
- package/dist/src/generators/vue/helpers.js +13 -5
- package/dist/src/generators/vue/optionsApi.js +0 -5
- package/dist/src/generators/vue/vue.js +4 -26
- package/dist/src/helpers/babel-transform.d.ts +2 -1
- package/dist/src/helpers/babel-transform.js +16 -19
- package/dist/src/helpers/get-state-object-string.d.ts +1 -1
- package/dist/src/helpers/get-state-object-string.js +5 -5
- package/dist/src/helpers/has-bindings-text.js +1 -1
- package/dist/src/helpers/is-children.d.ts +4 -1
- package/dist/src/helpers/is-children.js +5 -4
- package/dist/src/helpers/patterns.d.ts +1 -0
- package/dist/src/helpers/patterns.js +3 -1
- package/dist/src/helpers/plugins/process-code.js +1 -1
- package/dist/src/helpers/styles/helpers.d.ts +0 -1
- package/dist/src/helpers/traverse-nodes.d.ts +1 -1
- package/dist/src/helpers/traverse-nodes.js +3 -3
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +2 -3
- package/dist/src/parsers/builder.d.ts +4 -4
- package/dist/src/parsers/jsx/function-parser.js +8 -0
- package/dist/src/parsers/jsx/metadata.js +17 -1
- package/dist/src/parsers/jsx/state.js +6 -1
- package/dist/src/plugins/map-styles.js +1 -1
- package/dist/src/targets.d.ts +1 -0
- package/dist/src/targets.js +2 -0
- package/dist/src/types/mitosis-component.d.ts +4 -8
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/jsx-runtime.d.ts +2 -0
- package/package.json +7 -9
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { MitosisNode } from '../../types/mitosis-node';
|
|
2
|
+
import { BaseTranspilerOptions, TranspilerGenerator } from '../../types/transpiler';
|
|
3
|
+
export interface ToAlpineOptions extends BaseTranspilerOptions {
|
|
4
|
+
/**
|
|
5
|
+
* use @on and : instead of `x-on` and `x-bind`
|
|
6
|
+
*/
|
|
7
|
+
useShorthandSyntax?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* If true, the javascript won't be extracted into a separate script block.
|
|
10
|
+
*/
|
|
11
|
+
inlineState?: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare const checkIsComponentNode: (node: MitosisNode) => boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Test if the binding expression would be likely to generate
|
|
16
|
+
* valid or invalid liquid. If we generate invalid liquid tags
|
|
17
|
+
* Shopify will reject our PUT to update the template
|
|
18
|
+
*/
|
|
19
|
+
export declare const isValidAlpineBinding: (str?: string) => boolean;
|
|
20
|
+
export declare const componentToAlpine: TranspilerGenerator<ToAlpineOptions>;
|
|
@@ -0,0 +1,207 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.componentToAlpine = exports.isValidAlpineBinding = exports.checkIsComponentNode = void 0;
|
|
15
|
+
var standalone_1 = require("prettier/standalone");
|
|
16
|
+
var collect_css_1 = require("../../helpers/styles/collect-css");
|
|
17
|
+
var fast_clone_1 = require("../../helpers/fast-clone");
|
|
18
|
+
var strip_state_and_props_refs_1 = require("../../helpers/strip-state-and-props-refs");
|
|
19
|
+
var jsx_1 = require("../../parsers/jsx");
|
|
20
|
+
var mitosis_node_1 = require("../../types/mitosis-node");
|
|
21
|
+
var plugins_1 = require("../../modules/plugins");
|
|
22
|
+
var strip_meta_properties_1 = require("../../helpers/strip-meta-properties");
|
|
23
|
+
var get_state_object_string_1 = require("../../helpers/get-state-object-string");
|
|
24
|
+
var dash_case_1 = require("../../helpers/dash-case");
|
|
25
|
+
var remove_surrounding_block_1 = require("../../helpers/remove-surrounding-block");
|
|
26
|
+
var lodash_1 = require("lodash");
|
|
27
|
+
var get_refs_1 = require("../../helpers/get-refs");
|
|
28
|
+
var render_update_hooks_1 = require("./render-update-hooks");
|
|
29
|
+
var render_mount_hook_1 = require("./render-mount-hook");
|
|
30
|
+
var babel_transform_1 = require("../../helpers/babel-transform");
|
|
31
|
+
var checkIsComponentNode = function (node) {
|
|
32
|
+
return node.name === '@builder.io/mitosis/component';
|
|
33
|
+
};
|
|
34
|
+
exports.checkIsComponentNode = checkIsComponentNode;
|
|
35
|
+
/**
|
|
36
|
+
* Test if the binding expression would be likely to generate
|
|
37
|
+
* valid or invalid liquid. If we generate invalid liquid tags
|
|
38
|
+
* Shopify will reject our PUT to update the template
|
|
39
|
+
*/
|
|
40
|
+
var isValidAlpineBinding = function (str) {
|
|
41
|
+
if (str === void 0) { str = ''; }
|
|
42
|
+
return true;
|
|
43
|
+
/*
|
|
44
|
+
const strictMatches = Boolean(
|
|
45
|
+
// Test for our `context.shopify.liquid.*(expression), which
|
|
46
|
+
// we regex out later to transform back into valid liquid expressions
|
|
47
|
+
str.match(/(context|ctx)\s*(\.shopify\s*)?\.liquid\s*\./),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
strictMatches ||
|
|
52
|
+
// Test is the expression is simple and would map to Shopify bindings // Test for our `context.shopify.liquid.*(expression), which
|
|
53
|
+
// e.g. `state.product.price` -> `{{product.price}} // we regex out later to transform back into valid liquid expressions
|
|
54
|
+
Boolean(str.match(/^[a-z0-9_\.\s]+$/i))
|
|
55
|
+
);
|
|
56
|
+
*/
|
|
57
|
+
};
|
|
58
|
+
exports.isValidAlpineBinding = isValidAlpineBinding;
|
|
59
|
+
var removeOnFromEventName = function (str) { return str.replace(/^on/, ''); };
|
|
60
|
+
var prefixEvent = function (str) { return str.replace(/(?<=[\s]|^)event/gm, '$event'); };
|
|
61
|
+
var removeTrailingSemicolon = function (str) { return str.replace(/;$/, ''); };
|
|
62
|
+
var trim = function (str) { return str.trim(); };
|
|
63
|
+
var replaceInputRefs = (0, lodash_1.curry)(function (json, str) {
|
|
64
|
+
(0, get_refs_1.getRefs)(json).forEach(function (value) {
|
|
65
|
+
str = str.replaceAll(value, "this.$refs.".concat(value));
|
|
66
|
+
});
|
|
67
|
+
return str;
|
|
68
|
+
});
|
|
69
|
+
var replaceStateWithThis = function (str) { return str.replaceAll('state.', 'this.'); };
|
|
70
|
+
var getStateObjectString = function (json) {
|
|
71
|
+
return (0, lodash_1.flow)(get_state_object_string_1.getStateObjectStringFromComponent, trim, replaceInputRefs(json), (0, render_mount_hook_1.renderMountHook)(json), (0, render_update_hooks_1.renderUpdateHooks)(json), replaceStateWithThis)(json);
|
|
72
|
+
};
|
|
73
|
+
var bindEventHandlerKey = (0, lodash_1.flowRight)(dash_case_1.dashCase, removeOnFromEventName);
|
|
74
|
+
var bindEventHandlerValue = (0, lodash_1.flowRight)(prefixEvent, removeTrailingSemicolon, trim, remove_surrounding_block_1.removeSurroundingBlock, strip_state_and_props_refs_1.stripStateAndPropsRefs);
|
|
75
|
+
var bindEventHandler = function (_a) {
|
|
76
|
+
var useShorthandSyntax = _a.useShorthandSyntax;
|
|
77
|
+
return function (eventName, code) {
|
|
78
|
+
var bind = useShorthandSyntax ? '@' : 'x-on:';
|
|
79
|
+
return " ".concat(bind).concat(bindEventHandlerKey(eventName), "=\"").concat(bindEventHandlerValue(code).trim(), "\"");
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
var mappers = {
|
|
83
|
+
For: function (json, options) {
|
|
84
|
+
var _a, _b, _c;
|
|
85
|
+
return !((0, mitosis_node_1.checkIsForNode)(json) &&
|
|
86
|
+
(0, exports.isValidAlpineBinding)((_a = json.bindings.each) === null || _a === void 0 ? void 0 : _a.code) &&
|
|
87
|
+
(0, exports.isValidAlpineBinding)(json.scope.forName))
|
|
88
|
+
? ''
|
|
89
|
+
: "<template x-for=\"".concat(json.scope.forName, " in ").concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)((_b = json.bindings.each) === null || _b === void 0 ? void 0 : _b.code), "\">\n ").concat(((_c = json.children) !== null && _c !== void 0 ? _c : []).map(function (item) { return blockToAlpine(item, options); }).join('\n'), "\n </template>");
|
|
90
|
+
},
|
|
91
|
+
Fragment: function (json, options) { return blockToAlpine(__assign(__assign({}, json), { name: 'div' }), options); },
|
|
92
|
+
Show: function (json, options) {
|
|
93
|
+
var _a, _b, _c;
|
|
94
|
+
return !(0, exports.isValidAlpineBinding)((_a = json.bindings.when) === null || _a === void 0 ? void 0 : _a.code)
|
|
95
|
+
? ''
|
|
96
|
+
: "<template x-if=\"".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)((_b = json.bindings.when) === null || _b === void 0 ? void 0 : _b.code), "\">\n ").concat(((_c = json.children) !== null && _c !== void 0 ? _c : []).map(function (item) { return blockToAlpine(item, options); }).join('\n'), "\n </template>");
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
// TODO: spread support
|
|
100
|
+
var blockToAlpine = function (json, options) {
|
|
101
|
+
var _a, _b;
|
|
102
|
+
if (options === void 0) { options = {}; }
|
|
103
|
+
if (mappers[json.name]) {
|
|
104
|
+
return mappers[json.name](json, options);
|
|
105
|
+
}
|
|
106
|
+
// TODO: Add support for `{props.children}` bindings
|
|
107
|
+
if (json.properties._text) {
|
|
108
|
+
return json.properties._text;
|
|
109
|
+
}
|
|
110
|
+
if ((_a = json.bindings._text) === null || _a === void 0 ? void 0 : _a.code) {
|
|
111
|
+
return (0, exports.isValidAlpineBinding)(json.bindings._text.code)
|
|
112
|
+
? "<span x-html=\"".concat((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(json.bindings._text.code), "\"></span>")
|
|
113
|
+
: '';
|
|
114
|
+
}
|
|
115
|
+
var str = "<".concat(json.name, " ");
|
|
116
|
+
/*
|
|
117
|
+
// Copied from the liquid generator. Not sure what it does.
|
|
118
|
+
if (
|
|
119
|
+
json.bindings._spread?.code === '_spread' &&
|
|
120
|
+
isValidAlpineBinding(json.bindings._spread.code)
|
|
121
|
+
) {
|
|
122
|
+
str += `
|
|
123
|
+
<template x-for="_attr in ${json.bindings._spread.code}">
|
|
124
|
+
{{ _attr[0] }}="{{ _attr[1] }}"
|
|
125
|
+
</template>
|
|
126
|
+
`;
|
|
127
|
+
}
|
|
128
|
+
*/
|
|
129
|
+
for (var key in json.properties) {
|
|
130
|
+
var value = json.properties[key];
|
|
131
|
+
str += " ".concat(key, "=\"").concat(value, "\" ");
|
|
132
|
+
}
|
|
133
|
+
for (var key in json.bindings) {
|
|
134
|
+
if (key === '_spread' || key === 'css') {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
var _c = json.bindings[key], value = _c.code, bindingType = _c.type;
|
|
138
|
+
// TODO: proper babel transform to replace. Util for this
|
|
139
|
+
var useValue = (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(value);
|
|
140
|
+
if (key.startsWith('on')) {
|
|
141
|
+
str += bindEventHandler(options)(key, value);
|
|
142
|
+
}
|
|
143
|
+
else if (key === 'ref') {
|
|
144
|
+
str += " x-ref=\"".concat(useValue, "\"");
|
|
145
|
+
}
|
|
146
|
+
else if ((0, exports.isValidAlpineBinding)(useValue)) {
|
|
147
|
+
var bind = options.useShorthandSyntax && bindingType !== 'spread' ? ':' : 'x-bind:';
|
|
148
|
+
str += " ".concat(bind).concat(bindingType === 'spread' ? '' : key, "=\"").concat(useValue, "\" ").replace(':=', '=');
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return jsx_1.selfClosingTags.has(json.name)
|
|
152
|
+
? "".concat(str, " />")
|
|
153
|
+
: "".concat(str, ">").concat(((_b = json.children) !== null && _b !== void 0 ? _b : []).map(function (item) { return blockToAlpine(item, options); }).join('\n'), "</").concat(json.name, ">");
|
|
154
|
+
};
|
|
155
|
+
var componentToAlpine = function (options) {
|
|
156
|
+
if (options === void 0) { options = {}; }
|
|
157
|
+
return function (_a) {
|
|
158
|
+
var component = _a.component;
|
|
159
|
+
var json = (0, fast_clone_1.fastClone)(component);
|
|
160
|
+
if (options.plugins) {
|
|
161
|
+
json = (0, plugins_1.runPreJsonPlugins)(json, options.plugins);
|
|
162
|
+
}
|
|
163
|
+
var css = (0, collect_css_1.collectCss)(json);
|
|
164
|
+
(0, strip_meta_properties_1.stripMetaProperties)(json);
|
|
165
|
+
if (options.plugins) {
|
|
166
|
+
json = (0, plugins_1.runPostJsonPlugins)(json, options.plugins);
|
|
167
|
+
}
|
|
168
|
+
var stateObjectString = getStateObjectString(json);
|
|
169
|
+
// Set x-data on root element
|
|
170
|
+
json.children[0].properties['x-data'] = options.inlineState
|
|
171
|
+
? stateObjectString
|
|
172
|
+
: "".concat((0, lodash_1.camelCase)(json.name), "()");
|
|
173
|
+
if ((0, render_update_hooks_1.hasRootUpdateHook)(json)) {
|
|
174
|
+
json.children[0].properties['x-effect'] = 'onUpdate';
|
|
175
|
+
}
|
|
176
|
+
var str = css.trim().length ? "<style>".concat(css, "</style>") : '';
|
|
177
|
+
str += json.children.map(function (item) { return blockToAlpine(item, options); }).join('\n');
|
|
178
|
+
if (!options.inlineState) {
|
|
179
|
+
str += "<script>\n ".concat((0, babel_transform_1.babelTransformCode)("document.addEventListener('alpine:init', () => {\n Alpine.data('".concat((0, lodash_1.camelCase)(json.name), "', () => (").concat(stateObjectString, "))\n })")), "\n </script>");
|
|
180
|
+
}
|
|
181
|
+
if (options.plugins) {
|
|
182
|
+
str = (0, plugins_1.runPreCodePlugins)(str, options.plugins);
|
|
183
|
+
}
|
|
184
|
+
if (options.prettier !== false) {
|
|
185
|
+
try {
|
|
186
|
+
str = (0, standalone_1.format)(str, {
|
|
187
|
+
parser: 'html',
|
|
188
|
+
htmlWhitespaceSensitivity: 'ignore',
|
|
189
|
+
plugins: [
|
|
190
|
+
// To support running in browsers
|
|
191
|
+
require('prettier/parser-html'),
|
|
192
|
+
require('prettier/parser-postcss'),
|
|
193
|
+
require('prettier/parser-babel'),
|
|
194
|
+
],
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
catch (err) {
|
|
198
|
+
console.warn('Could not prettify', { string: str }, err);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (options.plugins) {
|
|
202
|
+
str = (0, plugins_1.runPostCodePlugins)(str, options.plugins);
|
|
203
|
+
}
|
|
204
|
+
return str;
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
exports.componentToAlpine = componentToAlpine;
|
|
@@ -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);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.renderMountHook = void 0;
|
|
4
|
+
var lodash_1 = require("lodash");
|
|
5
|
+
var render_update_hooks_1 = require("./render-update-hooks");
|
|
6
|
+
function shouldRenderMountHook(json) {
|
|
7
|
+
return json.hooks.onMount !== undefined || (0, render_update_hooks_1.hasWatchHooks)(json);
|
|
8
|
+
}
|
|
9
|
+
exports.renderMountHook = (0, lodash_1.curry)(function (json, objectString) {
|
|
10
|
+
var _a, _b;
|
|
11
|
+
return shouldRenderMountHook(json)
|
|
12
|
+
? objectString.replace(/(?:,)?(\s*)(}\s*)$/, ", init() {\n ".concat((0, render_update_hooks_1.renderWatchHooks)(json), "\n ").concat((_b = (_a = json.hooks.onMount) === null || _a === void 0 ? void 0 : _a.code) !== null && _b !== void 0 ? _b : '', "\n }$1$2"))
|
|
13
|
+
: objectString;
|
|
14
|
+
});
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { MitosisComponent } from '../../types/mitosis-component';
|
|
2
|
+
export declare function hasRootUpdateHook(json: MitosisComponent): boolean;
|
|
3
|
+
export declare const renderUpdateHooks: import("lodash").CurriedFunction2<MitosisComponent, string, string>;
|
|
4
|
+
export declare const hasWatchHooks: (json: MitosisComponent) => boolean;
|
|
5
|
+
export declare const renderWatchHooks: (json: MitosisComponent) => string;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.renderWatchHooks = exports.hasWatchHooks = exports.renderUpdateHooks = exports.hasRootUpdateHook = void 0;
|
|
4
|
+
var lodash_1 = require("lodash");
|
|
5
|
+
var extractCode = function (hook) { return hook.code; };
|
|
6
|
+
function renderRootUpdateHook(hooks, output) {
|
|
7
|
+
if (hooks.length === 0) {
|
|
8
|
+
return output;
|
|
9
|
+
}
|
|
10
|
+
var str = "onUpdate() {\n ".concat(hooks.map(extractCode).join('\n'), "\n }");
|
|
11
|
+
return output.replace(/,?(\s*})$/, ",\n".concat(str, "$1"));
|
|
12
|
+
}
|
|
13
|
+
function getRootUpdateHooks(json) {
|
|
14
|
+
var _a;
|
|
15
|
+
return ((_a = json.hooks.onUpdate) !== null && _a !== void 0 ? _a : []).filter(function (hook) { return hook.deps == ''; });
|
|
16
|
+
}
|
|
17
|
+
function hasRootUpdateHook(json) {
|
|
18
|
+
return getRootUpdateHooks(json).length > 0;
|
|
19
|
+
}
|
|
20
|
+
exports.hasRootUpdateHook = hasRootUpdateHook;
|
|
21
|
+
exports.renderUpdateHooks = (0, lodash_1.curry)(function (json, output) {
|
|
22
|
+
return renderRootUpdateHook(getRootUpdateHooks(json), output);
|
|
23
|
+
});
|
|
24
|
+
function getWatchHooks(json) {
|
|
25
|
+
var _a;
|
|
26
|
+
return ((_a = json.hooks.onUpdate) !== null && _a !== void 0 ? _a : []).filter(function (hook) { var _a; return (_a = hook.deps) === null || _a === void 0 ? void 0 : _a.match(/state|this/); });
|
|
27
|
+
}
|
|
28
|
+
var hasWatchHooks = function (json) {
|
|
29
|
+
return getWatchHooks(json).length > 0;
|
|
30
|
+
};
|
|
31
|
+
exports.hasWatchHooks = hasWatchHooks;
|
|
32
|
+
function renderWatchHook(hook) {
|
|
33
|
+
var _a, _b;
|
|
34
|
+
var deps = (_b = ((_a = hook.deps) !== null && _a !== void 0 ? _a : '')) === null || _b === void 0 ? void 0 : _b.slice(1).slice(0, -1).split(', ').filter(function (dep) { return dep.match(/state|this/); });
|
|
35
|
+
return deps
|
|
36
|
+
.map(function (dep) {
|
|
37
|
+
return "this.$watch('".concat(dep.replace(/(state|this)\./, ''), "', (value, oldValue) => { ").concat(hook.code, " });");
|
|
38
|
+
})
|
|
39
|
+
.join('\n');
|
|
40
|
+
}
|
|
41
|
+
var renderWatchHooks = function (json) {
|
|
42
|
+
return (0, exports.hasWatchHooks)(json) ? getWatchHooks(json).map(renderWatchHook).join('\n') : '';
|
|
43
|
+
};
|
|
44
|
+
exports.renderWatchHooks = renderWatchHooks;
|
|
@@ -8,10 +8,7 @@ export interface ToAngularOptions extends BaseTranspilerOptions {
|
|
|
8
8
|
bootstrapMapper?: Function;
|
|
9
9
|
}
|
|
10
10
|
interface AngularBlockOptions {
|
|
11
|
-
contextVars?: string[];
|
|
12
|
-
outputVars?: string[];
|
|
13
11
|
childComponents?: string[];
|
|
14
|
-
domRefs?: string[];
|
|
15
12
|
}
|
|
16
13
|
export declare const blockToAngular: (json: MitosisNode, options?: ToAngularOptions, blockOptions?: AngularBlockOptions) => string;
|
|
17
14
|
export declare const componentToAngular: TranspilerGenerator<ToAngularOptions>;
|
|
@@ -54,27 +54,30 @@ var get_custom_imports_1 = require("../helpers/get-custom-imports");
|
|
|
54
54
|
var get_components_used_1 = require("../helpers/get-components-used");
|
|
55
55
|
var is_upper_case_1 = require("../helpers/is-upper-case");
|
|
56
56
|
var html_tags_1 = require("../constants/html_tags");
|
|
57
|
+
var function_1 = require("fp-ts/lib/function");
|
|
58
|
+
var merge_options_1 = require("../helpers/merge-options");
|
|
59
|
+
var process_code_1 = require("../helpers/plugins/process-code");
|
|
57
60
|
var BUILT_IN_COMPONENTS = new Set(['Show', 'For', 'Fragment']);
|
|
58
61
|
var mappers = {
|
|
59
|
-
Fragment: function (json, options
|
|
60
|
-
return "<
|
|
61
|
-
.map(function (item) { return (0, exports.blockToAngular)(item, options
|
|
62
|
-
.join('\n'), "</
|
|
62
|
+
Fragment: function (json, options) {
|
|
63
|
+
return "<ng-container>".concat(json.children
|
|
64
|
+
.map(function (item) { return (0, exports.blockToAngular)(item, options); })
|
|
65
|
+
.join('\n'), "</ng-container>");
|
|
63
66
|
},
|
|
64
|
-
Slot: function (json, options
|
|
65
|
-
return "<ng-content ".concat(Object.
|
|
66
|
-
.map(function (
|
|
67
|
-
var _a,
|
|
68
|
-
if (binding === 'name') {
|
|
69
|
-
var selector = (0,
|
|
67
|
+
Slot: function (json, options) {
|
|
68
|
+
return "<ng-content ".concat(Object.entries(json.bindings)
|
|
69
|
+
.map(function (_a) {
|
|
70
|
+
var binding = _a[0], value = _a[1];
|
|
71
|
+
if (value && binding === 'name') {
|
|
72
|
+
var selector = (0, function_1.pipe)(value.code, slots_1.stripSlotPrefix, lodash_1.kebabCase);
|
|
70
73
|
return "select=\"[".concat(selector, "]\"");
|
|
71
74
|
}
|
|
72
75
|
})
|
|
73
|
-
.join('\n'), ">").concat(Object.
|
|
74
|
-
.map(function (
|
|
75
|
-
var _a;
|
|
76
|
-
if (binding !== 'name') {
|
|
77
|
-
return
|
|
76
|
+
.join('\n'), ">").concat(Object.entries(json.bindings)
|
|
77
|
+
.map(function (_a) {
|
|
78
|
+
var binding = _a[0], value = _a[1];
|
|
79
|
+
if (value && binding !== 'name') {
|
|
80
|
+
return value.code;
|
|
78
81
|
}
|
|
79
82
|
})
|
|
80
83
|
.join('\n'), "</ng-content>");
|
|
@@ -89,53 +92,38 @@ var BINDINGS_MAPPER = {
|
|
|
89
92
|
style: 'ngStyle',
|
|
90
93
|
};
|
|
91
94
|
var blockToAngular = function (json, options, blockOptions) {
|
|
92
|
-
var _a, _b, _c, _d
|
|
95
|
+
var _a, _b, _c, _d;
|
|
93
96
|
if (options === void 0) { options = {}; }
|
|
94
97
|
if (blockOptions === void 0) { blockOptions = {}; }
|
|
95
|
-
var contextVars = (blockOptions === null || blockOptions === void 0 ? void 0 : blockOptions.contextVars) || [];
|
|
96
|
-
var outputVars = (blockOptions === null || blockOptions === void 0 ? void 0 : blockOptions.outputVars) || [];
|
|
97
98
|
var childComponents = (blockOptions === null || blockOptions === void 0 ? void 0 : blockOptions.childComponents) || [];
|
|
98
|
-
var domRefs = (blockOptions === null || blockOptions === void 0 ? void 0 : blockOptions.domRefs) || [];
|
|
99
99
|
var isValidHtmlTag = html_tags_1.VALID_HTML_TAGS.includes(json.name.trim());
|
|
100
100
|
if (mappers[json.name]) {
|
|
101
|
-
return mappers[json.name](json, options
|
|
101
|
+
return mappers[json.name](json, options);
|
|
102
102
|
}
|
|
103
|
-
if ((0, is_children_1.default)(json)) {
|
|
103
|
+
if ((0, is_children_1.default)({ node: json })) {
|
|
104
104
|
return "<ng-content></ng-content>";
|
|
105
105
|
}
|
|
106
106
|
if (json.properties._text) {
|
|
107
107
|
return json.properties._text;
|
|
108
108
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
contextVars: [],
|
|
117
|
-
outputVars: outputVars,
|
|
118
|
-
domRefs: domRefs,
|
|
119
|
-
}), "}}");
|
|
109
|
+
var textCode = (_a = json.bindings._text) === null || _a === void 0 ? void 0 : _a.code;
|
|
110
|
+
if (textCode) {
|
|
111
|
+
if ((0, slots_1.isSlotProperty)(textCode)) {
|
|
112
|
+
var selector = (0, function_1.pipe)(textCode, slots_1.stripSlotPrefix, lodash_1.kebabCase);
|
|
113
|
+
return "<ng-content select=\"[".concat(selector, "]\"></ng-content>");
|
|
114
|
+
}
|
|
115
|
+
return "{{".concat(textCode, "}}");
|
|
120
116
|
}
|
|
121
117
|
var str = '';
|
|
122
118
|
var needsToRenderSlots = [];
|
|
123
119
|
if ((0, mitosis_node_1.checkIsForNode)(json)) {
|
|
124
120
|
var indexName = json.scope.indexName;
|
|
125
|
-
str += "<ng-container *ngFor=\"let ".concat(json.scope.forName, " of ").concat((
|
|
126
|
-
contextVars: contextVars,
|
|
127
|
-
outputVars: outputVars,
|
|
128
|
-
domRefs: domRefs,
|
|
129
|
-
})).concat(indexName ? "; let ".concat(indexName, " = index") : '', "\">");
|
|
121
|
+
str += "<ng-container *ngFor=\"let ".concat(json.scope.forName, " of ").concat((_b = json.bindings.each) === null || _b === void 0 ? void 0 : _b.code).concat(indexName ? "; let ".concat(indexName, " = index") : '', "\">");
|
|
130
122
|
str += json.children.map(function (item) { return (0, exports.blockToAngular)(item, options, blockOptions); }).join('\n');
|
|
131
123
|
str += "</ng-container>";
|
|
132
124
|
}
|
|
133
125
|
else if (json.name === 'Show') {
|
|
134
|
-
str += "<ng-container *ngIf=\"".concat((
|
|
135
|
-
contextVars: contextVars,
|
|
136
|
-
outputVars: outputVars,
|
|
137
|
-
domRefs: domRefs,
|
|
138
|
-
}), "\">");
|
|
126
|
+
str += "<ng-container *ngIf=\"".concat((_c = json.bindings.when) === null || _c === void 0 ? void 0 : _c.code, "\">");
|
|
139
127
|
str += json.children.map(function (item) { return (0, exports.blockToAngular)(item, options, blockOptions); }).join('\n');
|
|
140
128
|
str += "</ng-container>";
|
|
141
129
|
}
|
|
@@ -158,19 +146,14 @@ var blockToAngular = function (json, options, blockOptions) {
|
|
|
158
146
|
str += " ".concat(key, "=\"").concat(value, "\" ");
|
|
159
147
|
}
|
|
160
148
|
for (var key in json.bindings) {
|
|
161
|
-
if (((
|
|
149
|
+
if (((_d = json.bindings[key]) === null || _d === void 0 ? void 0 : _d.type) === 'spread') {
|
|
162
150
|
continue;
|
|
163
151
|
}
|
|
164
152
|
if (key.startsWith('$')) {
|
|
165
153
|
continue;
|
|
166
154
|
}
|
|
167
|
-
var
|
|
155
|
+
var _e = json.bindings[key], code = _e.code, _f = _e.arguments, cusArgs = _f === void 0 ? ['event'] : _f;
|
|
168
156
|
// TODO: proper babel transform to replace. Util for this
|
|
169
|
-
var useValue = (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
|
|
170
|
-
contextVars: contextVars,
|
|
171
|
-
outputVars: outputVars,
|
|
172
|
-
domRefs: domRefs,
|
|
173
|
-
}).replace(/"/g, '"');
|
|
174
157
|
if (key.startsWith('on')) {
|
|
175
158
|
var event_1 = key.replace('on', '').toLowerCase();
|
|
176
159
|
if (event_1 === 'change' && json.name === 'input' /* todo: other tags */) {
|
|
@@ -180,28 +163,28 @@ var blockToAngular = function (json, options, blockOptions) {
|
|
|
180
163
|
var eventName = cusArgs[0];
|
|
181
164
|
var regexp = new RegExp('(^|\\n|\\r| |;|\\(|\\[|!)' + eventName + '(\\?\\.|\\.|\\(| |;|\\)|$)', 'g');
|
|
182
165
|
var replacer = '$1$event$2';
|
|
183
|
-
var finalValue = (0, remove_surrounding_block_1.removeSurroundingBlock)(
|
|
166
|
+
var finalValue = (0, remove_surrounding_block_1.removeSurroundingBlock)(code.replace(regexp, replacer));
|
|
184
167
|
str += " (".concat(event_1, ")=\"").concat(finalValue, "\" ");
|
|
185
168
|
}
|
|
186
169
|
else if (key === 'class') {
|
|
187
|
-
str += " [class]=\"".concat(
|
|
170
|
+
str += " [class]=\"".concat(code, "\" ");
|
|
188
171
|
}
|
|
189
172
|
else if (key === 'ref') {
|
|
190
|
-
str += " #".concat(
|
|
173
|
+
str += " #".concat(code, " ");
|
|
191
174
|
}
|
|
192
175
|
else if ((0, slots_1.isSlotProperty)(key)) {
|
|
193
|
-
var lowercaseKey =
|
|
194
|
-
needsToRenderSlots.push("".concat(
|
|
176
|
+
var lowercaseKey = (0, function_1.pipe)(key, slots_1.stripSlotPrefix, function (x) { return x.toLowerCase(); });
|
|
177
|
+
needsToRenderSlots.push("".concat(code.replace(/(\/\>)|\>/, " ".concat(lowercaseKey, ">"))));
|
|
195
178
|
}
|
|
196
179
|
else if (BINDINGS_MAPPER[key]) {
|
|
197
|
-
str += " [".concat(BINDINGS_MAPPER[key], "]=\"").concat(
|
|
180
|
+
str += " [".concat(BINDINGS_MAPPER[key], "]=\"").concat(code, "\" ");
|
|
198
181
|
}
|
|
199
182
|
else if (isValidHtmlTag || key.includes('-')) {
|
|
200
183
|
// standard html elements need the attr to satisfy the compiler in many cases: eg: svg elements and [fill]
|
|
201
|
-
str += " [attr.".concat(key, "]=\"").concat(
|
|
184
|
+
str += " [attr.".concat(key, "]=\"").concat(code, "\" ");
|
|
202
185
|
}
|
|
203
186
|
else {
|
|
204
|
-
str += "[".concat(key, "]=\"").concat(
|
|
187
|
+
str += "[".concat(key, "]=\"").concat(code, "\" ");
|
|
205
188
|
}
|
|
206
189
|
}
|
|
207
190
|
if (jsx_1.selfClosingTags.has(json.name)) {
|
|
@@ -228,9 +211,42 @@ var componentToAngular = function (userOptions) {
|
|
|
228
211
|
preserveImports: false,
|
|
229
212
|
preserveFileExtensions: false,
|
|
230
213
|
};
|
|
231
|
-
var options = __assign(__assign({}, DEFAULT_OPTIONS), userOptions);
|
|
232
214
|
// Make a copy we can safely mutate, similar to babel's toolchain
|
|
233
215
|
var json = (0, fast_clone_1.fastClone)(_component);
|
|
216
|
+
var contextVars = Object.keys(((_b = json === null || json === void 0 ? void 0 : json.context) === null || _b === void 0 ? void 0 : _b.get) || {});
|
|
217
|
+
var metaOutputVars = ((_d = (_c = json.meta) === null || _c === void 0 ? void 0 : _c.useMetadata) === null || _d === void 0 ? void 0 : _d.outputs) || [];
|
|
218
|
+
var outputVars = (0, lodash_1.uniq)(__spreadArray(__spreadArray([], metaOutputVars, true), (0, get_prop_functions_1.getPropFunctions)(json), true));
|
|
219
|
+
var stateVars = Object.keys((json === null || json === void 0 ? void 0 : json.state) || {});
|
|
220
|
+
var options = (0, merge_options_1.mergeOptions)(__assign(__assign({}, DEFAULT_OPTIONS), userOptions));
|
|
221
|
+
options.plugins = __spreadArray(__spreadArray([], (options.plugins || []), true), [
|
|
222
|
+
(0, process_code_1.CODE_PROCESSOR_PLUGIN)(function (codeType) {
|
|
223
|
+
var domRefs = (0, get_refs_1.getRefs)(json);
|
|
224
|
+
switch (codeType) {
|
|
225
|
+
case 'hooks':
|
|
226
|
+
return function (code) {
|
|
227
|
+
return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
|
|
228
|
+
replaceWith: 'this.',
|
|
229
|
+
contextVars: contextVars,
|
|
230
|
+
outputVars: outputVars,
|
|
231
|
+
domRefs: Array.from(domRefs),
|
|
232
|
+
stateVars: stateVars,
|
|
233
|
+
});
|
|
234
|
+
};
|
|
235
|
+
case 'bindings':
|
|
236
|
+
return function (code) {
|
|
237
|
+
return (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
|
|
238
|
+
contextVars: [],
|
|
239
|
+
outputVars: outputVars,
|
|
240
|
+
domRefs: [], // the template doesn't need the this keyword.
|
|
241
|
+
}).replace(/"/g, '"');
|
|
242
|
+
};
|
|
243
|
+
case 'hooks-deps':
|
|
244
|
+
case 'state':
|
|
245
|
+
case 'properties':
|
|
246
|
+
return function (x) { return x; };
|
|
247
|
+
}
|
|
248
|
+
}),
|
|
249
|
+
], false);
|
|
234
250
|
if (options.plugins) {
|
|
235
251
|
json = (0, plugins_1.runPreJsonPlugins)(json, options.plugins);
|
|
236
252
|
}
|
|
@@ -250,8 +266,6 @@ var componentToAngular = function (userOptions) {
|
|
|
250
266
|
var localExportVars = Object.keys(localExports)
|
|
251
267
|
.filter(function (key) { return localExports[key].usedInLocal; })
|
|
252
268
|
.map(function (key) { return "".concat(key, " = ").concat(key, ";"); });
|
|
253
|
-
var metaOutputVars = ((_c = (_b = json.meta) === null || _b === void 0 ? void 0 : _b.useMetadata) === null || _c === void 0 ? void 0 : _c.outputs) || [];
|
|
254
|
-
var contextVars = Object.keys(((_d = json === null || json === void 0 ? void 0 : json.context) === null || _d === void 0 ? void 0 : _d.get) || {});
|
|
255
269
|
var injectables = contextVars.map(function (variableName) {
|
|
256
270
|
var _a, _b, _c, _d;
|
|
257
271
|
var variableType = (_a = json === null || json === void 0 ? void 0 : json.context) === null || _a === void 0 ? void 0 : _a.get[variableName].name;
|
|
@@ -270,7 +284,6 @@ var componentToAngular = function (userOptions) {
|
|
|
270
284
|
props.delete(forwardProp);
|
|
271
285
|
}
|
|
272
286
|
props.delete('children');
|
|
273
|
-
var outputVars = (0, lodash_1.uniq)(__spreadArray(__spreadArray([], metaOutputVars, true), (0, get_prop_functions_1.getPropFunctions)(json), true));
|
|
274
287
|
// remove props for outputs
|
|
275
288
|
outputVars.forEach(function (variableName) {
|
|
276
289
|
props.delete(variableName);
|
|
@@ -285,7 +298,6 @@ var componentToAngular = function (userOptions) {
|
|
|
285
298
|
var hasOnMount = Boolean((_f = json.hooks) === null || _f === void 0 ? void 0 : _f.onMount);
|
|
286
299
|
var domRefs = (0, get_refs_1.getRefs)(json);
|
|
287
300
|
var jsRefs = Object.keys(json.refs).filter(function (ref) { return !domRefs.has(ref); });
|
|
288
|
-
var stateVars = Object.keys((json === null || json === void 0 ? void 0 : json.state) || {});
|
|
289
301
|
var componentsUsed = Array.from((0, get_components_used_1.getComponentsUsed)(json)).filter(function (item) {
|
|
290
302
|
return item.length && (0, is_upper_case_1.isUpperCase)(item[0]) && !BUILT_IN_COMPONENTS.has(item);
|
|
291
303
|
});
|
|
@@ -300,14 +312,8 @@ var componentToAngular = function (userOptions) {
|
|
|
300
312
|
if (options.prettier !== false) {
|
|
301
313
|
css = tryFormat(css, 'css');
|
|
302
314
|
}
|
|
303
|
-
var blockOptions = {
|
|
304
|
-
contextVars: contextVars,
|
|
305
|
-
outputVars: outputVars,
|
|
306
|
-
domRefs: [],
|
|
307
|
-
childComponents: childComponents,
|
|
308
|
-
};
|
|
309
315
|
var template = json.children
|
|
310
|
-
.map(function (item) { return (0, exports.blockToAngular)(item, options,
|
|
316
|
+
.map(function (item) { return (0, exports.blockToAngular)(item, options, { childComponents: childComponents }); })
|
|
311
317
|
.join('\n');
|
|
312
318
|
if (options.prettier !== false) {
|
|
313
319
|
template = tryFormat(template, 'html');
|
|
@@ -384,40 +390,18 @@ var componentToAngular = function (userOptions) {
|
|
|
384
390
|
? ''
|
|
385
391
|
: "constructor(\n".concat(injectables.join(',\n'), ") {\n ").concat(!((_h = json.hooks) === null || _h === void 0 ? void 0 : _h.onInit)
|
|
386
392
|
? ''
|
|
387
|
-
: "\n ".concat((
|
|
388
|
-
replaceWith: 'this.',
|
|
389
|
-
contextVars: contextVars,
|
|
390
|
-
outputVars: outputVars,
|
|
391
|
-
}), "\n "), "\n }\n "), !hasOnMount
|
|
393
|
+
: "\n ".concat((_j = json.hooks.onInit) === null || _j === void 0 ? void 0 : _j.code, "\n "), "\n }\n "), !hasOnMount
|
|
392
394
|
? ''
|
|
393
395
|
: "ngOnInit() {\n\n ".concat(!((_k = json.hooks) === null || _k === void 0 ? void 0 : _k.onMount)
|
|
394
396
|
? ''
|
|
395
|
-
: "\n ".concat((
|
|
396
|
-
replaceWith: 'this.',
|
|
397
|
-
contextVars: contextVars,
|
|
398
|
-
outputVars: outputVars,
|
|
399
|
-
domRefs: Array.from(domRefs),
|
|
400
|
-
stateVars: stateVars,
|
|
401
|
-
}), "\n "), "\n }"), !((_m = json.hooks.onUpdate) === null || _m === void 0 ? void 0 : _m.length)
|
|
397
|
+
: "\n ".concat((_l = json.hooks.onMount) === null || _l === void 0 ? void 0 : _l.code, "\n "), "\n }"), !((_m = json.hooks.onUpdate) === null || _m === void 0 ? void 0 : _m.length)
|
|
402
398
|
? ''
|
|
403
399
|
: "ngAfterContentChecked() {\n ".concat(json.hooks.onUpdate.reduce(function (code, hook) {
|
|
404
|
-
code +=
|
|
405
|
-
replaceWith: 'this.',
|
|
406
|
-
contextVars: contextVars,
|
|
407
|
-
outputVars: outputVars,
|
|
408
|
-
domRefs: Array.from(domRefs),
|
|
409
|
-
stateVars: stateVars,
|
|
410
|
-
});
|
|
400
|
+
code += hook.code;
|
|
411
401
|
return code + '\n';
|
|
412
402
|
}, ''), "\n }"), !json.hooks.onUnMount
|
|
413
403
|
? ''
|
|
414
|
-
: "ngOnDestroy() {\n ".concat(
|
|
415
|
-
replaceWith: 'this.',
|
|
416
|
-
contextVars: contextVars,
|
|
417
|
-
outputVars: outputVars,
|
|
418
|
-
domRefs: Array.from(domRefs),
|
|
419
|
-
stateVars: stateVars,
|
|
420
|
-
}), "\n }"));
|
|
404
|
+
: "ngOnDestroy() {\n ".concat(json.hooks.onUnMount.code, "\n }"));
|
|
421
405
|
str = generateNgModule(str, json.name, componentsUsed, json, options.bootstrapMapper);
|
|
422
406
|
if (options.plugins) {
|
|
423
407
|
str = (0, plugins_1.runPreCodePlugins)(str, options.plugins);
|