@builder.io/mitosis 0.0.122 → 0.0.124
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/alpine/generate.js +3 -1
- package/dist/src/generators/alpine/render-mount-hook.js +3 -3
- package/dist/src/generators/angular.js +8 -10
- package/dist/src/generators/builder.js +7 -6
- package/dist/src/generators/helpers/on-mount.d.ts +5 -0
- package/dist/src/generators/helpers/on-mount.js +21 -0
- package/dist/src/generators/html.js +16 -15
- package/dist/src/generators/lit/generate.js +5 -4
- package/dist/src/generators/marko/generate.js +5 -4
- package/dist/src/generators/mitosis.js +2 -2
- package/dist/src/generators/qwik/component-generator.js +20 -6
- package/dist/src/generators/qwik/component.js +2 -2
- package/dist/src/generators/react/generator.js +44 -19
- package/dist/src/generators/rsc.d.ts +2 -0
- package/dist/src/generators/rsc.js +27 -16
- package/dist/src/generators/solid/index.js +7 -6
- package/dist/src/generators/stencil/generate.js +5 -4
- package/dist/src/generators/svelte/svelte.js +9 -7
- package/dist/src/generators/vue/compositionApi.d.ts +2 -2
- package/dist/src/generators/vue/compositionApi.js +2 -2
- package/dist/src/generators/vue/optionsApi.d.ts +2 -2
- package/dist/src/generators/vue/optionsApi.js +4 -3
- package/dist/src/generators/vue/vue.js +68 -62
- package/dist/src/helpers/create-mitosis-component.d.ts +6 -1
- package/dist/src/helpers/create-mitosis-component.js +16 -1
- package/dist/src/helpers/event-handlers.d.ts +1 -0
- package/dist/src/helpers/event-handlers.js +5 -0
- package/dist/src/helpers/on-event.d.ts +13 -0
- package/dist/src/helpers/on-event.js +53 -0
- package/dist/src/helpers/output.js +2 -4
- package/dist/src/helpers/process-http-requests.js +4 -4
- package/dist/src/helpers/typescript.d.ts +4 -1
- package/dist/src/index.d.ts +7 -1
- package/dist/src/parsers/builder/builder.js +10 -6
- package/dist/src/parsers/jsx/function-parser.js +52 -2
- package/dist/src/parsers/svelte/helpers/post-process.js +5 -5
- package/dist/src/parsers/svelte/html/actions.js +4 -6
- package/dist/src/parsers/svelte/index.js +4 -1
- package/dist/src/parsers/svelte/instance/hooks.js +2 -2
- package/dist/src/parsers/svelte/instance/index.js +3 -5
- package/dist/src/types/metadata.d.ts +1 -0
- package/dist/src/types/mitosis-component.d.ts +19 -8
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -14,6 +14,15 @@ var __assign = (this && this.__assign) || function () {
|
|
|
14
14
|
};
|
|
15
15
|
return __assign.apply(this, arguments);
|
|
16
16
|
};
|
|
17
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
18
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
19
|
+
if (ar || !(i in from)) {
|
|
20
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
21
|
+
ar[i] = from[i];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
25
|
+
};
|
|
17
26
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
18
27
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
19
28
|
};
|
|
@@ -31,6 +40,7 @@ var get_props_1 = require("../../helpers/get-props");
|
|
|
31
40
|
var is_mitosis_node_1 = require("../../helpers/is-mitosis-node");
|
|
32
41
|
var map_refs_1 = require("../../helpers/map-refs");
|
|
33
42
|
var merge_options_1 = require("../../helpers/merge-options");
|
|
43
|
+
var on_event_1 = require("../../helpers/on-event");
|
|
34
44
|
var process_code_1 = require("../../helpers/plugins/process-code");
|
|
35
45
|
var process_http_requests_1 = require("../../helpers/process-http-requests");
|
|
36
46
|
var render_imports_1 = require("../../helpers/render-imports");
|
|
@@ -96,14 +106,14 @@ var onUpdatePlugin = function (options) { return ({
|
|
|
96
106
|
},
|
|
97
107
|
}); };
|
|
98
108
|
var BASE_OPTIONS = {
|
|
99
|
-
plugins: [],
|
|
100
109
|
vueVersion: 2,
|
|
101
110
|
api: 'options',
|
|
102
111
|
defineComponent: true,
|
|
103
112
|
};
|
|
104
113
|
var componentToVue = function (userOptions) {
|
|
105
114
|
return function (_a) {
|
|
106
|
-
var _b
|
|
115
|
+
var _b;
|
|
116
|
+
var _c, _d, _e, _f, _g, _h;
|
|
107
117
|
var _component = _a.component, path = _a.path;
|
|
108
118
|
// Make a copy we can safely mutate, similar to babel's toolchain can be used
|
|
109
119
|
var component = (0, fast_clone_1.fastClone)(_component);
|
|
@@ -113,64 +123,60 @@ var componentToVue = function (userOptions) {
|
|
|
113
123
|
defaults: BASE_OPTIONS,
|
|
114
124
|
userOptions: userOptions,
|
|
115
125
|
});
|
|
116
|
-
options.
|
|
117
|
-
if (options.api === 'composition') {
|
|
118
|
-
switch (codeType) {
|
|
119
|
-
case 'hooks':
|
|
120
|
-
return function (code) { return (0, helpers_1.processBinding)({ code: code, options: options, json: component }); };
|
|
121
|
-
case 'state':
|
|
122
|
-
return function (code) { return (0, helpers_1.processBinding)({ code: code, options: options, json: component }); };
|
|
123
|
-
case 'bindings':
|
|
124
|
-
return (0, function_1.flow)(
|
|
125
|
-
// Strip types from any JS code that ends up in the template, because Vue does not support TS code in templates.
|
|
126
|
-
babel_transform_1.convertTypeScriptToJS, function (code) { return (0, helpers_1.processBinding)({ code: code, options: options, json: component, codeType: codeType }); });
|
|
127
|
-
case 'context-set':
|
|
128
|
-
return function (code) {
|
|
129
|
-
return (0, helpers_1.processBinding)({ code: code, options: options, json: component, preserveGetter: true });
|
|
130
|
-
};
|
|
131
|
-
case 'hooks-deps':
|
|
132
|
-
return (0, replace_identifiers_1.replaceStateIdentifier)(null);
|
|
133
|
-
case 'properties':
|
|
134
|
-
case 'dynamic-jsx-elements':
|
|
135
|
-
case 'types':
|
|
136
|
-
return function (c) { return c; };
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
else {
|
|
140
|
-
switch (codeType) {
|
|
141
|
-
case 'hooks':
|
|
142
|
-
return function (code) { return (0, helpers_1.processBinding)({ code: code, options: options, json: component }); };
|
|
143
|
-
case 'bindings':
|
|
144
|
-
return (0, function_1.flow)(
|
|
145
|
-
// Strip types from any JS code that ends up in the template, because Vue does not support TS code in templates.
|
|
146
|
-
babel_transform_1.convertTypeScriptToJS, function (code) { return (0, helpers_1.processBinding)({ code: code, options: options, json: component, codeType: codeType }); });
|
|
147
|
-
case 'properties':
|
|
148
|
-
case 'dynamic-jsx-elements':
|
|
149
|
-
case 'hooks-deps':
|
|
150
|
-
case 'types':
|
|
151
|
-
return function (c) { return c; };
|
|
152
|
-
case 'state':
|
|
153
|
-
return function (c) { return (0, helpers_1.processBinding)({ code: c, options: options, json: component }); };
|
|
154
|
-
case 'context-set':
|
|
155
|
-
return function (code) {
|
|
156
|
-
return (0, helpers_1.processBinding)({
|
|
157
|
-
code: code,
|
|
158
|
-
options: options,
|
|
159
|
-
json: component,
|
|
160
|
-
thisPrefix: '_this',
|
|
161
|
-
preserveGetter: true,
|
|
162
|
-
});
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}));
|
|
167
|
-
if (options.api === 'options') {
|
|
168
|
-
options.plugins.unshift(onUpdatePlugin);
|
|
169
|
-
}
|
|
170
|
-
else if (options.api === 'composition') {
|
|
171
|
-
options.plugins.unshift(functions_1.FUNCTION_HACK_PLUGIN);
|
|
126
|
+
if (options.api === 'composition') {
|
|
172
127
|
options.asyncComponentImports = false;
|
|
173
128
|
}
|
|
129
|
+
(_b = options.plugins).unshift.apply(_b, __spreadArray(__spreadArray(__spreadArray([(0, on_event_1.processOnEventHooksPlugin)()], (options.api === 'options' ? [onUpdatePlugin] : []), false), (options.api === 'composition' ? [functions_1.FUNCTION_HACK_PLUGIN] : []), false), [(0, process_code_1.CODE_PROCESSOR_PLUGIN)(function (codeType) {
|
|
130
|
+
if (options.api === 'composition') {
|
|
131
|
+
switch (codeType) {
|
|
132
|
+
case 'hooks':
|
|
133
|
+
return function (code) { return (0, helpers_1.processBinding)({ code: code, options: options, json: component }); };
|
|
134
|
+
case 'state':
|
|
135
|
+
return function (code) { return (0, helpers_1.processBinding)({ code: code, options: options, json: component }); };
|
|
136
|
+
case 'bindings':
|
|
137
|
+
return (0, function_1.flow)(
|
|
138
|
+
// Strip types from any JS code that ends up in the template, because Vue does not support TS code in templates.
|
|
139
|
+
babel_transform_1.convertTypeScriptToJS, function (code) { return (0, helpers_1.processBinding)({ code: code, options: options, json: component, codeType: codeType }); });
|
|
140
|
+
case 'context-set':
|
|
141
|
+
return function (code) {
|
|
142
|
+
return (0, helpers_1.processBinding)({ code: code, options: options, json: component, preserveGetter: true });
|
|
143
|
+
};
|
|
144
|
+
case 'hooks-deps':
|
|
145
|
+
return (0, replace_identifiers_1.replaceStateIdentifier)(null);
|
|
146
|
+
case 'properties':
|
|
147
|
+
case 'dynamic-jsx-elements':
|
|
148
|
+
case 'types':
|
|
149
|
+
return function (c) { return c; };
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
switch (codeType) {
|
|
154
|
+
case 'hooks':
|
|
155
|
+
return function (code) { return (0, helpers_1.processBinding)({ code: code, options: options, json: component }); };
|
|
156
|
+
case 'bindings':
|
|
157
|
+
return (0, function_1.flow)(
|
|
158
|
+
// Strip types from any JS code that ends up in the template, because Vue does not support TS code in templates.
|
|
159
|
+
babel_transform_1.convertTypeScriptToJS, function (code) { return (0, helpers_1.processBinding)({ code: code, options: options, json: component, codeType: codeType }); });
|
|
160
|
+
case 'properties':
|
|
161
|
+
case 'dynamic-jsx-elements':
|
|
162
|
+
case 'hooks-deps':
|
|
163
|
+
case 'types':
|
|
164
|
+
return function (c) { return c; };
|
|
165
|
+
case 'state':
|
|
166
|
+
return function (c) { return (0, helpers_1.processBinding)({ code: c, options: options, json: component }); };
|
|
167
|
+
case 'context-set':
|
|
168
|
+
return function (code) {
|
|
169
|
+
return (0, helpers_1.processBinding)({
|
|
170
|
+
code: code,
|
|
171
|
+
options: options,
|
|
172
|
+
json: component,
|
|
173
|
+
thisPrefix: '_this',
|
|
174
|
+
preserveGetter: true,
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
})], false));
|
|
174
180
|
(0, process_http_requests_1.processHttpRequests)(component);
|
|
175
181
|
processDynamicComponents(component, options);
|
|
176
182
|
processForKeys(component, options);
|
|
@@ -184,12 +190,12 @@ var componentToVue = function (userOptions) {
|
|
|
184
190
|
var slotsProps = props.filter(function (prop) { return (0, slots_1.isSlotProperty)(prop); });
|
|
185
191
|
component = (0, plugins_1.runPostJsonPlugins)({ json: component, plugins: options.plugins });
|
|
186
192
|
var css = (0, collect_css_1.collectCss)(component, {
|
|
187
|
-
prefix: (
|
|
193
|
+
prefix: (_d = (_c = options.cssNamespace) === null || _c === void 0 ? void 0 : _c.call(options)) !== null && _d !== void 0 ? _d : undefined,
|
|
188
194
|
});
|
|
189
195
|
(0, strip_meta_properties_1.stripMetaProperties)(component);
|
|
190
196
|
var template = (0, function_1.pipe)(component.children.map(function (item) { return (0, blocks_1.blockToVue)(item, options, { isRootNode: true }); }).join('\n'), helpers_1.renameMitosisComponentsToKebabCase);
|
|
191
|
-
var onUpdateWithDeps = ((
|
|
192
|
-
var onUpdateWithoutDeps = ((
|
|
197
|
+
var onUpdateWithDeps = ((_e = component.hooks.onUpdate) === null || _e === void 0 ? void 0 : _e.filter(function (hook) { var _a; return (_a = hook.deps) === null || _a === void 0 ? void 0 : _a.length; })) || [];
|
|
198
|
+
var onUpdateWithoutDeps = ((_f = component.hooks.onUpdate) === null || _f === void 0 ? void 0 : _f.filter(function (hook) { var _a; return !((_a = hook.deps) === null || _a === void 0 ? void 0 : _a.length); })) || [];
|
|
193
199
|
var getterKeys = Object.keys((0, lodash_1.pickBy)(component.state, function (i) { return (i === null || i === void 0 ? void 0 : i.type) === 'getter'; }));
|
|
194
200
|
// import from vue
|
|
195
201
|
var vueImports = [];
|
|
@@ -201,7 +207,7 @@ var componentToVue = function (userOptions) {
|
|
|
201
207
|
}
|
|
202
208
|
if (options.api === 'composition') {
|
|
203
209
|
onUpdateWithDeps.length && vueImports.push('watch');
|
|
204
|
-
|
|
210
|
+
component.hooks.onMount.length && vueImports.push('onMounted');
|
|
205
211
|
((_g = component.hooks.onUnMount) === null || _g === void 0 ? void 0 : _g.code) && vueImports.push('onUnmounted');
|
|
206
212
|
onUpdateWithoutDeps.length && vueImports.push('onUpdated');
|
|
207
213
|
(0, lodash_1.size)(getterKeys) && vueImports.push('computed');
|
|
@@ -1,2 +1,7 @@
|
|
|
1
1
|
import { MitosisComponent } from '../types/mitosis-component';
|
|
2
|
-
|
|
2
|
+
import { Overwrite, Prettify } from './typescript';
|
|
3
|
+
declare type PartialMitosisComponent = Prettify<Overwrite<Partial<MitosisComponent>, {
|
|
4
|
+
hooks: Partial<MitosisComponent['hooks']>;
|
|
5
|
+
}>>;
|
|
6
|
+
export declare const createMitosisComponent: (options?: PartialMitosisComponent) => MitosisComponent;
|
|
7
|
+
export {};
|
|
@@ -10,7 +10,22 @@ var __assign = (this && this.__assign) || function () {
|
|
|
10
10
|
};
|
|
11
11
|
return __assign.apply(this, arguments);
|
|
12
12
|
};
|
|
13
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
14
|
+
var t = {};
|
|
15
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
16
|
+
t[p] = s[p];
|
|
17
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
18
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
19
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
20
|
+
t[p[i]] = s[p[i]];
|
|
21
|
+
}
|
|
22
|
+
return t;
|
|
23
|
+
};
|
|
13
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
25
|
exports.createMitosisComponent = void 0;
|
|
15
|
-
var createMitosisComponent = function (options) {
|
|
26
|
+
var createMitosisComponent = function (options) {
|
|
27
|
+
var _a = options || {}, name = _a.name, hooks = _a.hooks, remainingOpts = __rest(_a, ["name", "hooks"]);
|
|
28
|
+
var _b = hooks || {}, _c = _b.onEvent, onEvent = _c === void 0 ? [] : _c, _d = _b.onMount, onMount = _d === void 0 ? [] : _d, remainingHooks = __rest(_b, ["onEvent", "onMount"]);
|
|
29
|
+
return __assign({ '@type': '@builder.io/mitosis/component', imports: [], exports: {}, inputs: [], meta: {}, refs: {}, state: {}, children: [], context: { get: {}, set: {} }, subComponents: [], name: name || 'MyComponent', hooks: __assign({ onMount: onMount, onEvent: onEvent }, remainingHooks) }, remainingOpts);
|
|
30
|
+
};
|
|
16
31
|
exports.createMitosisComponent = createMitosisComponent;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const checkIsBindingEventHandler: (code: string) => boolean;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkIsBindingEventHandler = void 0;
|
|
4
|
+
var checkIsBindingEventHandler = function (code) { return code.startsWith('on'); };
|
|
5
|
+
exports.checkIsBindingEventHandler = checkIsBindingEventHandler;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { MitosisComponent, MitosisNode, OnEventHook, Plugin } from '..';
|
|
2
|
+
export declare const getOnEventHandlerName: (hook: OnEventHook) => string;
|
|
3
|
+
export declare const getOnEventHooksForNode: ({ node, component, }: {
|
|
4
|
+
node: MitosisNode;
|
|
5
|
+
component: MitosisComponent;
|
|
6
|
+
}) => OnEventHook[];
|
|
7
|
+
/**
|
|
8
|
+
* Adds event handlers from `onEvent` hooks to the appropriate node's bindings.
|
|
9
|
+
* Only works with frameworks that support custom events in their templates.
|
|
10
|
+
*/
|
|
11
|
+
export declare const processOnEventHooksPlugin: (args?: {
|
|
12
|
+
setBindings?: boolean;
|
|
13
|
+
}) => Plugin;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.processOnEventHooksPlugin = exports.getOnEventHooksForNode = exports.getOnEventHandlerName = void 0;
|
|
4
|
+
var capitalize_1 = require("./capitalize");
|
|
5
|
+
var traverse_nodes_1 = require("./traverse-nodes");
|
|
6
|
+
var checkIsEventHandlerNode = function (node, hook) {
|
|
7
|
+
var _a;
|
|
8
|
+
return hook.refName === ((_a = node.bindings.ref) === null || _a === void 0 ? void 0 : _a.code);
|
|
9
|
+
};
|
|
10
|
+
var getBindingName = function (hook) {
|
|
11
|
+
return "on".concat((0, capitalize_1.capitalize)(hook.eventName));
|
|
12
|
+
};
|
|
13
|
+
var getOnEventHandlerName = function (hook) {
|
|
14
|
+
return "".concat(hook.refName, "_").concat(getBindingName(hook));
|
|
15
|
+
};
|
|
16
|
+
exports.getOnEventHandlerName = getOnEventHandlerName;
|
|
17
|
+
var getOnEventHooksForNode = function (_a) {
|
|
18
|
+
var node = _a.node, component = _a.component;
|
|
19
|
+
return component.hooks.onEvent.filter(function (hook) { return checkIsEventHandlerNode(node, hook); });
|
|
20
|
+
};
|
|
21
|
+
exports.getOnEventHooksForNode = getOnEventHooksForNode;
|
|
22
|
+
/**
|
|
23
|
+
* Adds event handlers from `onEvent` hooks to the appropriate node's bindings.
|
|
24
|
+
* Only works with frameworks that support custom events in their templates.
|
|
25
|
+
*/
|
|
26
|
+
var processOnEventHooksPlugin = function (args) {
|
|
27
|
+
if (args === void 0) { args = {}; }
|
|
28
|
+
return function () { return ({
|
|
29
|
+
json: {
|
|
30
|
+
pre: function (component) {
|
|
31
|
+
var _a = args.setBindings, setBindings = _a === void 0 ? true : _a;
|
|
32
|
+
(0, traverse_nodes_1.traverseNodes)(component, function (node) {
|
|
33
|
+
(0, exports.getOnEventHooksForNode)({ node: node, component: component }).forEach(function (hook) {
|
|
34
|
+
var handlerName = getBindingName(hook);
|
|
35
|
+
var fnName = (0, exports.getOnEventHandlerName)(hook);
|
|
36
|
+
component.state[fnName] = {
|
|
37
|
+
code: "".concat(fnName, "(").concat(hook.eventArgName, ") { ").concat(hook.code, " }"),
|
|
38
|
+
type: 'method',
|
|
39
|
+
};
|
|
40
|
+
if (setBindings) {
|
|
41
|
+
node.bindings[handlerName] = {
|
|
42
|
+
code: "state.".concat(fnName, "(").concat(hook.eventArgName, ")"),
|
|
43
|
+
arguments: [hook.eventArgName],
|
|
44
|
+
type: 'single',
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
}); };
|
|
52
|
+
};
|
|
53
|
+
exports.processOnEventHooksPlugin = processOnEventHooksPlugin;
|
|
@@ -3,11 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.checkShouldOutputTypeScript = void 0;
|
|
4
4
|
var nullable_1 = require("./nullable");
|
|
5
5
|
var checkShouldOutputTypeScript = function (_a) {
|
|
6
|
-
var _b
|
|
6
|
+
var _b;
|
|
7
7
|
var target = _a.target, options = _a.options;
|
|
8
8
|
var targetTsConfig = (_b = options.options[target]) === null || _b === void 0 ? void 0 : _b.typescript;
|
|
9
|
-
return (0, nullable_1.checkIsDefined)(targetTsConfig)
|
|
10
|
-
? targetTsConfig
|
|
11
|
-
: ((_c = options.commonOptions) === null || _c === void 0 ? void 0 : _c.typescript) || false;
|
|
9
|
+
return (0, nullable_1.checkIsDefined)(targetTsConfig) ? targetTsConfig : false;
|
|
12
10
|
};
|
|
13
11
|
exports.checkShouldOutputTypeScript = checkShouldOutputTypeScript;
|
|
@@ -2,9 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.processHttpRequests = void 0;
|
|
4
4
|
function processHttpRequests(json) {
|
|
5
|
-
var _a, _b
|
|
5
|
+
var _a, _b;
|
|
6
6
|
var httpRequests = (_b = (_a = json === null || json === void 0 ? void 0 : json.meta) === null || _a === void 0 ? void 0 : _a.useMetadata) === null || _b === void 0 ? void 0 : _b.httpRequests;
|
|
7
|
-
var onMount = ((_c = json.hooks.onMount) === null || _c === void 0 ? void 0 : _c.code) ? json.hooks.onMount : { code: '' };
|
|
8
7
|
if (httpRequests) {
|
|
9
8
|
for (var key in httpRequests) {
|
|
10
9
|
if (!json.state[key]) {
|
|
@@ -13,9 +12,10 @@ function processHttpRequests(json) {
|
|
|
13
12
|
var value = httpRequests[key];
|
|
14
13
|
// TODO: unravel our proxy. aka parse out methods, header, etc
|
|
15
14
|
// and remove our proxy from being used anymore
|
|
16
|
-
|
|
15
|
+
json.hooks.onMount.push({
|
|
16
|
+
code: "\n fetch(\"".concat(value, "\").then(res => res.json()).then(result => {\n state.").concat(key, " = result;\n })\n "),
|
|
17
|
+
});
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
|
-
json.hooks.onMount = onMount;
|
|
20
20
|
}
|
|
21
21
|
exports.processHttpRequests = processHttpRequests;
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
export declare type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
|
|
1
|
+
export declare type Overwrite<T, U> = keyof U extends keyof T ? Pick<T, Exclude<keyof T, keyof U>> & U : never;
|
|
2
|
+
export declare type Prettify<T> = {
|
|
3
|
+
[K in keyof T]: T[K];
|
|
4
|
+
} & {};
|
|
2
5
|
export declare type OmitObj<T, U> = T extends U ? Omit<T, keyof U> : never;
|
|
3
6
|
export declare type Dictionary<T> = {
|
|
4
7
|
[key: string]: T;
|
package/dist/src/index.d.ts
CHANGED
|
@@ -77,7 +77,9 @@ export declare function createContext<T = Dictionary<any>>(value: T, args?: {
|
|
|
77
77
|
export declare function setContext<T = Dictionary<any>>(key: Context<T>, value: Partial<T>, options?: {
|
|
78
78
|
type?: ReactivityType;
|
|
79
79
|
}): void;
|
|
80
|
-
export declare function onMount(fn: () => any
|
|
80
|
+
export declare function onMount(fn: () => any, args?: {
|
|
81
|
+
onSSR?: boolean;
|
|
82
|
+
}): void;
|
|
81
83
|
export declare function onUpdate(fn: () => any, deps?: any[]): void;
|
|
82
84
|
export declare function onInit(fn: () => any): void;
|
|
83
85
|
export declare function onUnMount(fn: () => any): void;
|
|
@@ -86,4 +88,8 @@ export declare function onError(fn: () => any): void;
|
|
|
86
88
|
export declare function useMetadata(obj: ComponentMetadata): void;
|
|
87
89
|
export declare function useDefaultProps<T = Dictionary<any>>(value: T): T;
|
|
88
90
|
export declare function useStyle(value: string): void;
|
|
91
|
+
/**
|
|
92
|
+
* Adds an event listener to a given element ref.
|
|
93
|
+
*/
|
|
94
|
+
export declare function onEvent(eventName: string, fn: (event: any) => any, elementRef: Element, isRoot?: boolean): void;
|
|
89
95
|
export declare function useTarget<Return>(dict: TargetBlock<Return>): Return;
|
|
@@ -718,7 +718,7 @@ var isBuilderElement = function (el) {
|
|
|
718
718
|
};
|
|
719
719
|
exports.isBuilderElement = isBuilderElement;
|
|
720
720
|
var builderContentPartToMitosisComponent = function (builderContent, options) {
|
|
721
|
-
var _a, _b, _c, _d, _e, _f, _g, _h
|
|
721
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
722
722
|
if (options === void 0) { options = {}; }
|
|
723
723
|
builderContent = (0, fast_clone_1.fastClone)(builderContent);
|
|
724
724
|
(0, traverse_1.default)(builderContent).forEach(function (elem) {
|
|
@@ -747,7 +747,7 @@ var builderContentPartToMitosisComponent = function (builderContent, options) {
|
|
|
747
747
|
}
|
|
748
748
|
}
|
|
749
749
|
});
|
|
750
|
-
var
|
|
750
|
+
var _j = extractStateHook(((_a = builderContent === null || builderContent === void 0 ? void 0 : builderContent.data) === null || _a === void 0 ? void 0 : _a.tsCode) || ((_b = builderContent === null || builderContent === void 0 ? void 0 : builderContent.data) === null || _b === void 0 ? void 0 : _b.jsCode) || ''), state = _j.state, code = _j.code;
|
|
751
751
|
var customCode = convertExportDefaultToReturn(code);
|
|
752
752
|
var parsed = getHooks(builderContent);
|
|
753
753
|
var parsedState = (parsed === null || parsed === void 0 ? void 0 : parsed.state) || {};
|
|
@@ -763,10 +763,14 @@ var builderContentPartToMitosisComponent = function (builderContent, options) {
|
|
|
763
763
|
defaultValue: input.defaultValue,
|
|
764
764
|
}); }),
|
|
765
765
|
state: mitosisState,
|
|
766
|
-
hooks:
|
|
767
|
-
onMount: (parsed === null || parsed === void 0 ? void 0 : parsed.hooks.onMount)
|
|
768
|
-
|
|
769
|
-
|
|
766
|
+
hooks: {
|
|
767
|
+
onMount: __spreadArray([], ((parsed === null || parsed === void 0 ? void 0 : parsed.hooks.onMount.length)
|
|
768
|
+
? parsed === null || parsed === void 0 ? void 0 : parsed.hooks.onMount
|
|
769
|
+
: customCode
|
|
770
|
+
? [{ code: customCode }]
|
|
771
|
+
: []), true),
|
|
772
|
+
},
|
|
773
|
+
children: (((_h = builderContent.data) === null || _h === void 0 ? void 0 : _h.blocks) || [])
|
|
770
774
|
.filter(function (item) {
|
|
771
775
|
var _a, _b;
|
|
772
776
|
if ((_b = (_a = item.properties) === null || _a === void 0 ? void 0 : _a.src) === null || _b === void 0 ? void 0 : _b.includes('/api/v1/pixel')) {
|
|
@@ -65,7 +65,10 @@ var types = babel.types;
|
|
|
65
65
|
*/
|
|
66
66
|
var componentFunctionToJson = function (node, context) {
|
|
67
67
|
var _a;
|
|
68
|
-
var hooks = {
|
|
68
|
+
var hooks = {
|
|
69
|
+
onMount: [],
|
|
70
|
+
onEvent: [],
|
|
71
|
+
};
|
|
69
72
|
var state = {};
|
|
70
73
|
var accessedContext = {};
|
|
71
74
|
var setContext = {};
|
|
@@ -111,12 +114,58 @@ var componentFunctionToJson = function (node, context) {
|
|
|
111
114
|
}
|
|
112
115
|
case hooks_1.HOOKS.MOUNT: {
|
|
113
116
|
var firstArg = expression.arguments[0];
|
|
117
|
+
var hookOptions = expression.arguments[1];
|
|
114
118
|
if (types.isFunctionExpression(firstArg) || types.isArrowFunctionExpression(firstArg)) {
|
|
115
119
|
var code = (0, helpers_2.processHookCode)(firstArg);
|
|
116
|
-
|
|
120
|
+
var onSSR = false;
|
|
121
|
+
if (types.isObjectExpression(hookOptions)) {
|
|
122
|
+
var onSSRProp = hookOptions.properties.find(function (property) {
|
|
123
|
+
return types.isProperty(property) &&
|
|
124
|
+
types.isIdentifier(property.key) &&
|
|
125
|
+
property.key.name === 'onSSR';
|
|
126
|
+
});
|
|
127
|
+
if (types.isObjectProperty(onSSRProp) && types.isBooleanLiteral(onSSRProp.value)) {
|
|
128
|
+
onSSR = onSSRProp.value.value;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
hooks.onMount.push({
|
|
132
|
+
code: code,
|
|
133
|
+
onSSR: onSSR,
|
|
134
|
+
});
|
|
117
135
|
}
|
|
118
136
|
break;
|
|
119
137
|
}
|
|
138
|
+
case hooks_1.HOOKS.EVENT: {
|
|
139
|
+
var firstArg = expression.arguments[0];
|
|
140
|
+
var secondArg = expression.arguments[1];
|
|
141
|
+
var thirdArg = expression.arguments[2];
|
|
142
|
+
var fourthArg = expression.arguments[3];
|
|
143
|
+
if (!types.isStringLiteral(firstArg)) {
|
|
144
|
+
console.warn('`onEvent` hook skipped. Event name must be a string literal: ', (0, generator_1.default)(expression).code);
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
if (!types.isFunctionExpression(secondArg) &&
|
|
148
|
+
!types.isArrowFunctionExpression(secondArg)) {
|
|
149
|
+
console.warn('`onEvent` hook skipped. Event handler must be a function: ', (0, generator_1.default)(expression).code);
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
if (!types.isIdentifier(thirdArg)) {
|
|
153
|
+
console.warn('`onEvent` hook skipped. Element ref must be a value: ', (0, generator_1.default)(expression).code);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
var isRoot = types.isBooleanLiteral(fourthArg) ? fourthArg.value : false;
|
|
157
|
+
var eventArgName = types.isIdentifier(secondArg.params[0])
|
|
158
|
+
? secondArg.params[0].name
|
|
159
|
+
: 'event';
|
|
160
|
+
hooks.onEvent.push({
|
|
161
|
+
eventName: firstArg.value,
|
|
162
|
+
code: (0, helpers_2.processHookCode)(secondArg),
|
|
163
|
+
refName: thirdArg.name,
|
|
164
|
+
isRoot: isRoot,
|
|
165
|
+
eventArgName: eventArgName,
|
|
166
|
+
});
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
120
169
|
case hooks_1.HOOKS.UPDATE: {
|
|
121
170
|
var firstArg = expression.arguments[0];
|
|
122
171
|
var secondArg = expression.arguments[1];
|
|
@@ -161,6 +210,7 @@ var componentFunctionToJson = function (node, context) {
|
|
|
161
210
|
}
|
|
162
211
|
case hooks_1.HOOKS.METADATA: {
|
|
163
212
|
context.builder.component.meta[hooks_1.HOOKS.METADATA] = __assign(__assign({}, context.builder.component.meta[hooks_1.HOOKS.METADATA]), (0, helpers_1.parseCodeJson)(expression.arguments[0]));
|
|
213
|
+
break;
|
|
164
214
|
}
|
|
165
215
|
}
|
|
166
216
|
}
|
|
@@ -158,14 +158,14 @@ function postProcessHooks(json) {
|
|
|
158
158
|
if (!hook) {
|
|
159
159
|
return "continue";
|
|
160
160
|
}
|
|
161
|
-
if (
|
|
161
|
+
if (Array.isArray(hook)) {
|
|
162
162
|
hook.forEach(function (item, index) {
|
|
163
|
-
|
|
164
|
-
(_a = json.hooks[key]) === null || _a === void 0 ? void 0 : _a.splice(index, 1, addPropertiesAndStateToHook(json, item));
|
|
163
|
+
hook.splice(index, 1, addPropertiesAndStateToHook(json, item));
|
|
165
164
|
});
|
|
166
|
-
return "continue";
|
|
167
165
|
}
|
|
168
|
-
|
|
166
|
+
else {
|
|
167
|
+
hook = addPropertiesAndStateToHook(json, hook);
|
|
168
|
+
}
|
|
169
169
|
};
|
|
170
170
|
for (var _i = 0, hookKeys_1 = hookKeys; _i < hookKeys_1.length; _i++) {
|
|
171
171
|
var key = hookKeys_1[_i];
|
|
@@ -4,7 +4,7 @@ exports.parseAction = void 0;
|
|
|
4
4
|
var astring_1 = require("astring");
|
|
5
5
|
var string_1 = require("../helpers/string");
|
|
6
6
|
function parseAction(json, nodeReference, attribute) {
|
|
7
|
-
var _a
|
|
7
|
+
var _a;
|
|
8
8
|
var methodName = attribute.name;
|
|
9
9
|
var parameters = '';
|
|
10
10
|
if (['Identifier', 'ObjectExpression'].includes((_a = attribute.expression) === null || _a === void 0 ? void 0 : _a.type)) {
|
|
@@ -17,11 +17,9 @@ function parseAction(json, nodeReference, attribute) {
|
|
|
17
17
|
propertyType: 'normal',
|
|
18
18
|
};
|
|
19
19
|
var initHandler = "if (".concat(nodeReference, ") { ").concat(actionHandler, " = ").concat(methodName, "(").concat(nodeReference, ", ").concat(parameters, "); };\n");
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
code: "".concat(onMountCode, "\n").concat(initHandler, ";\n"),
|
|
24
|
-
};
|
|
20
|
+
json.hooks.onMount.push({
|
|
21
|
+
code: initHandler,
|
|
22
|
+
});
|
|
25
23
|
// Handle Destroy / Re-Mount
|
|
26
24
|
var onReferenceUpdate = "\n if (!".concat(nodeReference, " && ").concat(actionHandler, ") { \n ").concat(actionHandler, "?.destroy(); \n ").concat(actionHandler, " = null; \n } else if (").concat(nodeReference, " && !").concat(actionHandler, ") { \n ").concat(initHandler, " \n };\n\n ");
|
|
27
25
|
json.hooks.onUpdate = json.hooks.onUpdate || [];
|
|
@@ -12,9 +12,9 @@ function parseHookBody(node, stripCurlyBraces) {
|
|
|
12
12
|
return code;
|
|
13
13
|
}
|
|
14
14
|
function parseOnMount(json, node) {
|
|
15
|
-
json.hooks.onMount
|
|
15
|
+
json.hooks.onMount.push({
|
|
16
16
|
code: parseHookBody(node),
|
|
17
|
-
};
|
|
17
|
+
});
|
|
18
18
|
}
|
|
19
19
|
exports.parseOnMount = parseOnMount;
|
|
20
20
|
function parseOnDestroy(json, node) {
|
|
@@ -22,7 +22,6 @@ var handleMemberExpression = function (json, node, parent) {
|
|
|
22
22
|
(0, expressions_1.parseMemberExpression)(json, node, parent);
|
|
23
23
|
};
|
|
24
24
|
var handleExpressionStatement = function (json, node, parent) {
|
|
25
|
-
var _a;
|
|
26
25
|
if (node.expression.type === 'CallExpression') {
|
|
27
26
|
if (node.expression.callee.type === 'MemberExpression') {
|
|
28
27
|
handleMemberExpression(json, node, parent);
|
|
@@ -50,10 +49,9 @@ var handleExpressionStatement = function (json, node, parent) {
|
|
|
50
49
|
// No default
|
|
51
50
|
}
|
|
52
51
|
else if ((parent === null || parent === void 0 ? void 0 : parent.type) === 'Program') {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
};
|
|
52
|
+
json.hooks.onMount.push({
|
|
53
|
+
code: (0, astring_1.generate)(node),
|
|
54
|
+
});
|
|
57
55
|
}
|
|
58
56
|
};
|
|
59
57
|
var handleFunctionDeclaration = function (json, node) {
|
|
@@ -44,7 +44,7 @@ export interface ContextSetInfo extends ContextOptions {
|
|
|
44
44
|
value?: MitosisState;
|
|
45
45
|
ref?: string;
|
|
46
46
|
}
|
|
47
|
-
export declare type
|
|
47
|
+
export declare type BaseHook = {
|
|
48
48
|
code: string;
|
|
49
49
|
deps?: string;
|
|
50
50
|
};
|
|
@@ -79,6 +79,16 @@ export declare type TargetBlockDefinition = TargetBlockCode & {
|
|
|
79
79
|
requiresDefault: boolean;
|
|
80
80
|
};
|
|
81
81
|
};
|
|
82
|
+
export declare type OnEventHook = BaseHook & {
|
|
83
|
+
refName: string;
|
|
84
|
+
eventName: string;
|
|
85
|
+
isRoot: boolean;
|
|
86
|
+
deps?: never;
|
|
87
|
+
eventArgName: string;
|
|
88
|
+
};
|
|
89
|
+
export declare type OnMountHook = BaseHook & {
|
|
90
|
+
onSSR?: boolean;
|
|
91
|
+
};
|
|
82
92
|
export declare type MitosisComponent = {
|
|
83
93
|
'@type': '@builder.io/mitosis/component';
|
|
84
94
|
name: string;
|
|
@@ -109,13 +119,14 @@ export declare type MitosisComponent = {
|
|
|
109
119
|
};
|
|
110
120
|
};
|
|
111
121
|
hooks: {
|
|
112
|
-
init?:
|
|
113
|
-
onInit?:
|
|
114
|
-
onMount
|
|
115
|
-
onUnMount?:
|
|
116
|
-
preComponent?:
|
|
117
|
-
postComponent?:
|
|
118
|
-
onUpdate?:
|
|
122
|
+
init?: BaseHook;
|
|
123
|
+
onInit?: BaseHook;
|
|
124
|
+
onMount: OnMountHook[];
|
|
125
|
+
onUnMount?: BaseHook;
|
|
126
|
+
preComponent?: BaseHook;
|
|
127
|
+
postComponent?: BaseHook;
|
|
128
|
+
onUpdate?: BaseHook[];
|
|
129
|
+
onEvent: OnEventHook[];
|
|
119
130
|
};
|
|
120
131
|
targetBlocks?: Dictionary<TargetBlockDefinition>;
|
|
121
132
|
children: MitosisNode[];
|