@builder.io/mitosis 0.0.82 → 0.0.83
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/angular.js +2 -2
- package/dist/src/generators/context/helpers/context-with-symbol-key.d.ts +5 -0
- package/dist/src/generators/context/helpers/context-with-symbol-key.js +27 -0
- package/dist/src/generators/context/svelte.d.ts +2 -6
- package/dist/src/generators/context/svelte.js +2 -25
- package/dist/src/generators/context/vue.d.ts +3 -6
- package/dist/src/generators/context/vue.js +2 -6
- package/dist/src/generators/vue/compositionApi.js +7 -5
- package/dist/src/generators/vue/helpers.d.ts +10 -9
- package/dist/src/generators/vue/helpers.js +32 -17
- package/dist/src/generators/vue/optionsApi.js +13 -3
- package/dist/src/helpers/plugins/process-code.d.ts +3 -1
- package/dist/src/helpers/plugins/process-code.js +15 -14
- package/dist/src/helpers/replace-identifiers.js +16 -9
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -57,7 +57,7 @@ var html_tags_1 = require("../constants/html_tags");
|
|
|
57
57
|
var function_1 = require("fp-ts/lib/function");
|
|
58
58
|
var merge_options_1 = require("../helpers/merge-options");
|
|
59
59
|
var process_code_1 = require("../helpers/plugins/process-code");
|
|
60
|
-
var BUILT_IN_COMPONENTS = new Set(['Show', 'For', 'Fragment']);
|
|
60
|
+
var BUILT_IN_COMPONENTS = new Set(['Show', 'For', 'Fragment', 'Slot']);
|
|
61
61
|
var mappers = {
|
|
62
62
|
Fragment: function (json, options) {
|
|
63
63
|
return "<ng-container>".concat(json.children
|
|
@@ -85,7 +85,7 @@ var mappers = {
|
|
|
85
85
|
},
|
|
86
86
|
};
|
|
87
87
|
var generateNgModule = function (content, name, componentsUsed, component, bootstrapMapper) {
|
|
88
|
-
return "import { NgModule } from \"@angular/core\";\nimport {
|
|
88
|
+
return "import { NgModule } from \"@angular/core\";\nimport { CommonModule } from \"@angular/common\";\n\n".concat(content, "\n\n@NgModule({\n declarations: [").concat(name, "],\n imports: [CommonModule").concat(componentsUsed.length ? ', ' + componentsUsed.map(function (comp) { return "".concat(comp, "Module"); }).join(', ') : '', "],\n exports: [").concat(name, "],\n ").concat(bootstrapMapper ? bootstrapMapper(name, componentsUsed, component) : '', "\n})\nexport class ").concat(name, "Module {}");
|
|
89
89
|
};
|
|
90
90
|
// TODO: Maybe in the future allow defining `string | function` as values
|
|
91
91
|
var BINDINGS_MAPPER = {
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { MitosisContext } from '../../../types/mitosis-context';
|
|
2
|
+
import { BaseTranspilerOptions } from '../../../types/transpiler';
|
|
3
|
+
export declare const getContextWithSymbolKey: (options: Pick<BaseTranspilerOptions, 'prettier'>) => ({ context }: {
|
|
4
|
+
context: MitosisContext;
|
|
5
|
+
}) => string;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getContextWithSymbolKey = void 0;
|
|
4
|
+
var standalone_1 = require("prettier/standalone");
|
|
5
|
+
var get_state_object_string_1 = require("../../../helpers/get-state-object-string");
|
|
6
|
+
var getContextWithSymbolKey = function (options) {
|
|
7
|
+
return function (_a) {
|
|
8
|
+
var context = _a.context;
|
|
9
|
+
var str = "\n const key = Symbol(); \n\n export default {\n ".concat(context.name, ": ").concat((0, get_state_object_string_1.stringifyContextValue)(context.value), ", \n key \n }\n ");
|
|
10
|
+
if (options.prettier !== false) {
|
|
11
|
+
try {
|
|
12
|
+
str = (0, standalone_1.format)(str, {
|
|
13
|
+
parser: 'typescript',
|
|
14
|
+
plugins: [
|
|
15
|
+
require('prettier/parser-typescript'), // To support running in browsers
|
|
16
|
+
],
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
catch (err) {
|
|
20
|
+
console.error('Format error for file:', str);
|
|
21
|
+
throw err;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return str;
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
exports.getContextWithSymbolKey = getContextWithSymbolKey;
|
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
import { MitosisContext } from '../../types/mitosis-context';
|
|
2
1
|
import { BaseTranspilerOptions } from '../../types/transpiler';
|
|
3
|
-
interface ContextToSvelteOptions extends Pick<BaseTranspilerOptions, 'prettier'> {
|
|
4
|
-
}
|
|
5
2
|
/**
|
|
6
3
|
* TO-DO: support types
|
|
7
4
|
*/
|
|
8
|
-
export declare const contextToSvelte: (options
|
|
9
|
-
context: MitosisContext;
|
|
5
|
+
export declare const contextToSvelte: (options: Pick<BaseTranspilerOptions, "prettier">) => ({ context }: {
|
|
6
|
+
context: import("../../types/mitosis-context").MitosisContext;
|
|
10
7
|
}) => string;
|
|
11
|
-
export {};
|
|
@@ -1,31 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.contextToSvelte = void 0;
|
|
4
|
-
var
|
|
5
|
-
var get_state_object_string_1 = require("../../helpers/get-state-object-string");
|
|
4
|
+
var context_with_symbol_key_1 = require("./helpers/context-with-symbol-key");
|
|
6
5
|
/**
|
|
7
6
|
* TO-DO: support types
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
|
-
if (options === void 0) { options = {}; }
|
|
11
|
-
return function (_a) {
|
|
12
|
-
var context = _a.context;
|
|
13
|
-
var str = "\n const key = Symbol(); \n\n export default {\n ".concat(context.name, ": ").concat((0, get_state_object_string_1.stringifyContextValue)(context.value), ", \n key \n }\n ");
|
|
14
|
-
if (options.prettier !== false) {
|
|
15
|
-
try {
|
|
16
|
-
str = (0, standalone_1.format)(str, {
|
|
17
|
-
parser: 'typescript',
|
|
18
|
-
plugins: [
|
|
19
|
-
require('prettier/parser-typescript'), // To support running in browsers
|
|
20
|
-
],
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
catch (err) {
|
|
24
|
-
console.error('Format error for file:', str);
|
|
25
|
-
throw err;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
return str;
|
|
29
|
-
};
|
|
30
|
-
};
|
|
31
|
-
exports.contextToSvelte = contextToSvelte;
|
|
8
|
+
exports.contextToSvelte = context_with_symbol_key_1.getContextWithSymbolKey;
|
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
export declare function contextToVue(context: MitosisContext, options?: ContextToVueOptions): string;
|
|
6
|
-
export {};
|
|
1
|
+
export declare const contextToVue: (options: Pick<import("../..").BaseTranspilerOptions, "prettier">) => ({ context }: {
|
|
2
|
+
context: import("../../types/mitosis-context").MitosisContext;
|
|
3
|
+
}) => string;
|
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.contextToVue = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var str = "\n // Noop file\n export default {};\n ";
|
|
7
|
-
return str;
|
|
8
|
-
}
|
|
9
|
-
exports.contextToVue = contextToVue;
|
|
4
|
+
var context_with_symbol_key_1 = require("./helpers/context-with-symbol-key");
|
|
5
|
+
exports.contextToVue = context_with_symbol_key_1.getContextWithSymbolKey;
|
|
@@ -61,11 +61,13 @@ function generateCompositionApiScript(component, options, template, props, onUpd
|
|
|
61
61
|
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 } ";
|
|
62
62
|
}
|
|
63
63
|
var getterKeys = Object.keys((0, lodash_1.pickBy)(component.state, function (i) { return (i === null || i === void 0 ? void 0 : i.type) === 'getter'; }));
|
|
64
|
-
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 ", "\n ", "\n "])), props.length ? getCompositionPropDefinition({ component: component, props: props, options: options }) : '', refs, (_a = Object.
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
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 ", "\n ", "\n "])), props.length ? getCompositionPropDefinition({ component: component, props: props, options: options }) : '', refs, (_a = Object.entries(component.context.get)) === null || _a === void 0 ? void 0 : _a.map(function (_a) {
|
|
65
|
+
var key = _a[0], context = _a[1];
|
|
66
|
+
return "const ".concat(key, " = inject(").concat((0, helpers_1.getContextKey)(context), ")");
|
|
67
|
+
}).join('\n'), (_b = Object.values(component.context.set)) === null || _b === void 0 ? void 0 : _b.map(function (contextSet) {
|
|
68
|
+
var contextValue = (0, helpers_1.getContextValue)({ json: component, options: options })(contextSet);
|
|
69
|
+
var key = (0, helpers_1.getContextKey)(contextSet);
|
|
70
|
+
return "provide(".concat(key, ", ").concat(contextValue, ")");
|
|
69
71
|
}).join('\n'), (_c = Object.keys(component.refs)) === null || _c === void 0 ? void 0 : _c.map(function (key) {
|
|
70
72
|
if (options.typescript) {
|
|
71
73
|
return "const ".concat(key, " = ref<").concat(component.refs[key].typeParameter, ">()");
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Nullable } from '../../helpers/nullable';
|
|
2
|
-
import { ContextSetInfo, MitosisComponent } from '../../types/mitosis-component';
|
|
2
|
+
import { ContextGetInfo, ContextSetInfo, MitosisComponent } from '../../types/mitosis-component';
|
|
3
3
|
import { MitosisNode } from '../../types/mitosis-node';
|
|
4
4
|
import { ToVueOptions } from './types';
|
|
5
5
|
export declare const addPropertiesToJson: (properties: MitosisNode['properties']) => (json: MitosisNode) => MitosisNode;
|
|
@@ -9,14 +9,15 @@ export declare const invertBooleanExpression: (expression: string) => string;
|
|
|
9
9
|
export declare function encodeQuotes(string: string): string;
|
|
10
10
|
export declare const renameMitosisComponentsToKebabCase: (str: string) => string;
|
|
11
11
|
export declare function getContextNames(json: MitosisComponent): string[];
|
|
12
|
-
|
|
12
|
+
declare type ProcessBinding = {
|
|
13
13
|
code: string;
|
|
14
14
|
options: ToVueOptions;
|
|
15
15
|
json: MitosisComponent;
|
|
16
|
-
preserveGetter?: boolean
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export declare const
|
|
16
|
+
preserveGetter?: boolean;
|
|
17
|
+
thisPrefix?: 'this' | '_this';
|
|
18
|
+
};
|
|
19
|
+
export declare const processBinding: ({ code, options, json, preserveGetter, thisPrefix, }: ProcessBinding) => string;
|
|
20
|
+
export declare const getContextValue: (args: Pick<ProcessBinding, 'options' | 'json' | 'thisPrefix'>) => ({ name, ref, value }: ContextSetInfo) => Nullable<string>;
|
|
21
|
+
export declare const checkIfContextHasStrName: (context: ContextGetInfo | ContextSetInfo) => boolean;
|
|
22
|
+
export declare const getContextKey: (context: ContextGetInfo | ContextSetInfo) => string;
|
|
23
|
+
export {};
|
|
@@ -20,7 +20,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
|
20
20
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
21
21
|
};
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
exports.
|
|
23
|
+
exports.getContextKey = exports.checkIfContextHasStrName = exports.getContextValue = exports.processBinding = exports.getContextNames = exports.renameMitosisComponentsToKebabCase = exports.encodeQuotes = exports.invertBooleanExpression = exports.getOnUpdateHookName = exports.addBindingsToJson = exports.addPropertiesToJson = void 0;
|
|
24
24
|
var get_state_object_string_1 = require("../../helpers/get-state-object-string");
|
|
25
25
|
var strip_state_and_props_refs_1 = require("../../helpers/strip-state-and-props-refs");
|
|
26
26
|
var function_1 = require("fp-ts/lib/function");
|
|
@@ -29,6 +29,7 @@ var core_1 = require("@babel/core");
|
|
|
29
29
|
var lodash_1 = require("lodash");
|
|
30
30
|
var patterns_1 = require("../../helpers/patterns");
|
|
31
31
|
var replace_identifiers_1 = require("../../helpers/replace-identifiers");
|
|
32
|
+
var html_tags_1 = require("../../constants/html_tags");
|
|
32
33
|
var addPropertiesToJson = function (properties) {
|
|
33
34
|
return function (json) { return (__assign(__assign({}, json), { properties: __assign(__assign({}, json.properties), properties) })); };
|
|
34
35
|
};
|
|
@@ -48,7 +49,15 @@ function encodeQuotes(string) {
|
|
|
48
49
|
exports.encodeQuotes = encodeQuotes;
|
|
49
50
|
// Transform <FooBar> to <foo-bar> as Vue2 needs
|
|
50
51
|
var renameMitosisComponentsToKebabCase = function (str) {
|
|
51
|
-
return str.replace(/<\/?\w+/g, function (match) {
|
|
52
|
+
return str.replace(/<\/?\w+/g, function (match) {
|
|
53
|
+
var tagName = match.replaceAll('<', '').replaceAll('/', '');
|
|
54
|
+
if (html_tags_1.VALID_HTML_TAGS.includes(tagName)) {
|
|
55
|
+
return match;
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
return match.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
52
61
|
};
|
|
53
62
|
exports.renameMitosisComponentsToKebabCase = renameMitosisComponentsToKebabCase;
|
|
54
63
|
function getContextNames(json) {
|
|
@@ -90,13 +99,14 @@ var getAllRefs = function (component) {
|
|
|
90
99
|
var allKeys = __spreadArray(__spreadArray([], refKeys, true), stateKeys, true);
|
|
91
100
|
return allKeys;
|
|
92
101
|
};
|
|
93
|
-
function processRefs(
|
|
102
|
+
function processRefs(_a) {
|
|
103
|
+
var input = _a.input, component = _a.component, options = _a.options, thisPrefix = _a.thisPrefix;
|
|
94
104
|
var refs = options.api === 'options' ? getContextNames(component) : getAllRefs(component);
|
|
95
105
|
return (0, babel_transform_1.babelTransformExpression)(input, {
|
|
96
106
|
Identifier: function (path) {
|
|
97
107
|
var name = path.node.name;
|
|
98
108
|
if (refs.includes(name) && shouldAppendValueToRef(path)) {
|
|
99
|
-
var newValue = options.api === 'options' ? "
|
|
109
|
+
var newValue = options.api === 'options' ? "".concat(thisPrefix, ".").concat(name) : "".concat(name, ".value");
|
|
100
110
|
path.replaceWith(core_1.types.identifier(newValue));
|
|
101
111
|
}
|
|
102
112
|
},
|
|
@@ -124,7 +134,7 @@ function prefixMethodsWithThis(input, component, options) {
|
|
|
124
134
|
// TODO: migrate all stripStateAndPropsRefs to use this here
|
|
125
135
|
// to properly replace context refs
|
|
126
136
|
var processBinding = function (_a) {
|
|
127
|
-
var code = _a.code, options = _a.options, json = _a.json, _b = _a.preserveGetter, preserveGetter = _b === void 0 ? false : _b;
|
|
137
|
+
var code = _a.code, options = _a.options, json = _a.json, _b = _a.preserveGetter, preserveGetter = _b === void 0 ? false : _b, _c = _a.thisPrefix, thisPrefix = _c === void 0 ? 'this' : _c;
|
|
128
138
|
try {
|
|
129
139
|
return (0, function_1.pipe)((0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(code, {
|
|
130
140
|
includeState: true,
|
|
@@ -137,13 +147,13 @@ var processBinding = function (_a) {
|
|
|
137
147
|
return name;
|
|
138
148
|
case 'options':
|
|
139
149
|
if (name === 'children' || name.startsWith('children.')) {
|
|
140
|
-
return '
|
|
150
|
+
return '${thisPrefix}.$slots.default';
|
|
141
151
|
}
|
|
142
|
-
return "
|
|
152
|
+
return "".concat(thisPrefix, ".").concat(name);
|
|
143
153
|
}
|
|
144
154
|
},
|
|
145
155
|
}), function (x) {
|
|
146
|
-
return (0, function_1.pipe)(x, function (code) { return processRefs(code, json, options); }, function (code) { return prefixMethodsWithThis(code, json, options); }, function (code) { return (preserveGetter === false ? (0, patterns_1.stripGetter)(code) : code); });
|
|
156
|
+
return (0, function_1.pipe)(x, function (code) { return processRefs({ input: code, component: json, options: options, thisPrefix: thisPrefix }); }, function (code) { return prefixMethodsWithThis(code, json, options); }, function (code) { return (preserveGetter === false ? (0, patterns_1.stripGetter)(code) : code); });
|
|
147
157
|
});
|
|
148
158
|
}
|
|
149
159
|
catch (e) {
|
|
@@ -152,24 +162,29 @@ var processBinding = function (_a) {
|
|
|
152
162
|
}
|
|
153
163
|
};
|
|
154
164
|
exports.processBinding = processBinding;
|
|
155
|
-
var getContextValue = function (
|
|
156
|
-
var options = _a.options, json = _a.json;
|
|
165
|
+
var getContextValue = function (args) {
|
|
157
166
|
return function (_a) {
|
|
158
167
|
var name = _a.name, ref = _a.ref, value = _a.value;
|
|
159
168
|
var valueStr = value
|
|
160
169
|
? (0, get_state_object_string_1.stringifyContextValue)(value, {
|
|
161
|
-
valueMapper: function (code) { return (0, exports.processBinding)({ code: code,
|
|
170
|
+
valueMapper: function (code) { return (0, exports.processBinding)(__assign(__assign({ code: code }, args), { preserveGetter: true })); },
|
|
162
171
|
})
|
|
163
172
|
: ref
|
|
164
|
-
? (0, exports.processBinding)({ code: ref,
|
|
173
|
+
? (0, exports.processBinding)(__assign(__assign({ code: ref }, args), { preserveGetter: true }))
|
|
165
174
|
: null;
|
|
166
175
|
return valueStr;
|
|
167
176
|
};
|
|
168
177
|
};
|
|
169
178
|
exports.getContextValue = getContextValue;
|
|
170
|
-
var
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
179
|
+
var checkIfContextHasStrName = function (context) {
|
|
180
|
+
// check if the name is wrapped in single or double quotes
|
|
181
|
+
var isStrName = context.name.startsWith("'") || context.name.startsWith('"');
|
|
182
|
+
return isStrName;
|
|
183
|
+
};
|
|
184
|
+
exports.checkIfContextHasStrName = checkIfContextHasStrName;
|
|
185
|
+
var getContextKey = function (context) {
|
|
186
|
+
var isStrName = (0, exports.checkIfContextHasStrName)(context);
|
|
187
|
+
var key = isStrName ? context.name : "".concat(context.name, ".key");
|
|
188
|
+
return key;
|
|
174
189
|
};
|
|
175
|
-
exports.
|
|
190
|
+
exports.getContextKey = getContextKey;
|
|
@@ -21,10 +21,20 @@ var get_state_object_string_1 = require("../../helpers/get-state-object-string")
|
|
|
21
21
|
var nullable_1 = require("../../helpers/nullable");
|
|
22
22
|
var render_imports_1 = require("../../helpers/render-imports");
|
|
23
23
|
var helpers_1 = require("./helpers");
|
|
24
|
+
var getContextProvideString = function (json, options) {
|
|
25
|
+
return "{\n ".concat(Object.values(json.context.set)
|
|
26
|
+
.map(function (setVal) {
|
|
27
|
+
var key = (0, helpers_1.getContextKey)(setVal);
|
|
28
|
+
return "[".concat(key, "]: ").concat((0, helpers_1.getContextValue)({ options: options, json: json, thisPrefix: '_this' })(setVal));
|
|
29
|
+
})
|
|
30
|
+
.join(','), "\n }");
|
|
31
|
+
};
|
|
24
32
|
function getContextInjectString(component, options) {
|
|
25
33
|
var str = '{';
|
|
26
|
-
|
|
27
|
-
|
|
34
|
+
var contextGetters = component.context.get;
|
|
35
|
+
for (var key in contextGetters) {
|
|
36
|
+
var context = contextGetters[key];
|
|
37
|
+
str += "\n ".concat(key, ": ").concat((0, helpers_1.encodeQuotes)((0, helpers_1.getContextKey)(context)), ",\n ");
|
|
28
38
|
}
|
|
29
39
|
str += '}';
|
|
30
40
|
return str;
|
|
@@ -138,7 +148,7 @@ function generateOptionsApiScript(component, options, path, template, props, onU
|
|
|
138
148
|
: "name: '".concat(path && ((_a = options.namePrefix) === null || _a === void 0 ? void 0 : _a.call(options, path)) ? ((_b = options.namePrefix) === null || _b === void 0 ? void 0 : _b.call(options, path)) + '-' : '').concat((0, lodash_1.kebabCase)(component.name), "',"), "\n ").concat(generateComponents(componentsUsed, options), "\n ").concat(props.length ? getPropDefinition({ component: component, props: props }) : '', "\n ").concat(dataString.length < 4
|
|
139
149
|
? ''
|
|
140
150
|
: "\n data: () => (".concat(dataString, "),\n "), "\n\n ").concat((0, lodash_1.size)(component.context.set)
|
|
141
|
-
? "provide() {\n return ".concat(
|
|
151
|
+
? "provide() {\n const _this = this;\n return ".concat(getContextProvideString(component, options), "\n },")
|
|
142
152
|
: '', "\n ").concat((0, lodash_1.size)(component.context.get)
|
|
143
153
|
? "inject: ".concat(getContextInjectString(component, options), ",")
|
|
144
154
|
: '', "\n ").concat(((_c = component.hooks.onInit) === null || _c === void 0 ? void 0 : _c.code)
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Plugin } from '../../types/plugins';
|
|
2
|
+
import { MitosisComponent } from '../../types/mitosis-component';
|
|
2
3
|
declare type CodeType = 'hooks' | 'hooks-deps' | 'bindings' | 'properties' | 'state';
|
|
3
|
-
declare
|
|
4
|
+
declare function codeProcessor(codeType: CodeType): (code: string, hookType?: keyof MitosisComponent['hooks']) => string;
|
|
5
|
+
declare type CodeProcessor = typeof codeProcessor;
|
|
4
6
|
/**
|
|
5
7
|
* Given a `codeProcessor` function, processes all code expressions within a Mitosis component.
|
|
6
8
|
*/
|
|
@@ -7,14 +7,14 @@ var traverse_nodes_1 = require("../traverse-nodes");
|
|
|
7
7
|
* Process code in bindings and properties of a node
|
|
8
8
|
*/
|
|
9
9
|
var preProcessBlockCode = function (_a) {
|
|
10
|
-
|
|
11
|
-
var propertiesProcessor = codeProcessor('properties');
|
|
10
|
+
// const propertiesProcessor = codeProcessor('properties');
|
|
12
11
|
// for (const key in json.properties) {
|
|
13
12
|
// const value = json.properties[key];
|
|
14
13
|
// if (key !== '_text' && value) {
|
|
15
14
|
// json.properties[key] = propertiesProcessor(value);
|
|
16
15
|
// }
|
|
17
16
|
// }
|
|
17
|
+
var json = _a.json, codeProcessor = _a.codeProcessor;
|
|
18
18
|
var bindingsProcessor = codeProcessor('bindings');
|
|
19
19
|
for (var key in json.bindings) {
|
|
20
20
|
var value = json.bindings[key];
|
|
@@ -30,26 +30,27 @@ var CODE_PROCESSOR_PLUGIN = function (codeProcessor) {
|
|
|
30
30
|
return function () { return ({
|
|
31
31
|
json: {
|
|
32
32
|
post: function (json) {
|
|
33
|
-
|
|
33
|
+
function processHook(key, hook) {
|
|
34
|
+
hook.code = codeProcessor('hooks')(hook.code, key);
|
|
35
|
+
if (hook.deps) {
|
|
36
|
+
hook.deps = codeProcessor('hooks-deps')(hook.deps);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
34
39
|
/**
|
|
35
40
|
* process code in hooks
|
|
36
41
|
*/
|
|
37
42
|
for (var key in json.hooks) {
|
|
38
43
|
var typedKey = key;
|
|
39
44
|
var hooks = json.hooks[typedKey];
|
|
40
|
-
if ((0, nullable_1.checkIsDefined)(hooks)
|
|
41
|
-
|
|
42
|
-
var
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
hook.deps = codeProcessor('hooks-deps')(hook.deps);
|
|
45
|
+
if ((0, nullable_1.checkIsDefined)(hooks)) {
|
|
46
|
+
if (Array.isArray(hooks)) {
|
|
47
|
+
for (var _i = 0, hooks_1 = hooks; _i < hooks_1.length; _i++) {
|
|
48
|
+
var hook = hooks_1[_i];
|
|
49
|
+
processHook(typedKey, hook);
|
|
46
50
|
}
|
|
47
51
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
hooks.code = processHookCode(hooks.code);
|
|
51
|
-
if (hooks.deps) {
|
|
52
|
-
hooks.deps = codeProcessor('hooks-deps')(hooks.deps);
|
|
52
|
+
else {
|
|
53
|
+
processHook(typedKey, hooks);
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
56
|
}
|
|
@@ -14,10 +14,10 @@ var babel_transform_1 = require("./babel-transform");
|
|
|
14
14
|
*/
|
|
15
15
|
var getToParam = function (path) {
|
|
16
16
|
if (core_1.types.isMemberExpression(path.node) || core_1.types.isOptionalMemberExpression(path.node)) {
|
|
17
|
-
// if simple member expression e.g. `props.foo`, returns `foo`
|
|
18
17
|
if (core_1.types.isIdentifier(path.node.property)) {
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
// if simple member expression e.g. `props.foo`, returns `foo`
|
|
19
|
+
var propertyName = path.node.property.name;
|
|
20
|
+
return propertyName;
|
|
21
21
|
}
|
|
22
22
|
else {
|
|
23
23
|
// if nested member expression e.g. `props.foo.bar.baz`, returns `foo.bar.baz`
|
|
@@ -27,14 +27,17 @@ var getToParam = function (path) {
|
|
|
27
27
|
}
|
|
28
28
|
else {
|
|
29
29
|
// if naked identifier e.g. `foo`, returns `foo`
|
|
30
|
-
|
|
30
|
+
var nodeName = path.node.name;
|
|
31
|
+
return nodeName;
|
|
31
32
|
}
|
|
32
33
|
};
|
|
33
34
|
var _replaceIdentifiers = function (path, _a) {
|
|
35
|
+
var _b, _c;
|
|
34
36
|
var from = _a.from, to = _a.to;
|
|
35
37
|
var memberExpressionObject = core_1.types.isIdentifier(path.node) ? path.node : path.node.object;
|
|
36
38
|
var normalizedFrom = Array.isArray(from) ? from : [from];
|
|
37
|
-
if (!core_1.types.isIdentifier(memberExpressionObject)
|
|
39
|
+
if (!core_1.types.isIdentifier(memberExpressionObject) ||
|
|
40
|
+
((_c = (_b = path.node) === null || _b === void 0 ? void 0 : _b._builder_meta) === null || _c === void 0 ? void 0 : _c.newlyGenerated)) {
|
|
38
41
|
return;
|
|
39
42
|
}
|
|
40
43
|
var matchesFrom = normalizedFrom.includes(memberExpressionObject.name);
|
|
@@ -78,17 +81,18 @@ var _replaceIdentifiers = function (path, _a) {
|
|
|
78
81
|
if ((0, generator_1.default)(path.node).code === (0, generator_1.default)(newMemberExpression).code) {
|
|
79
82
|
return;
|
|
80
83
|
}
|
|
84
|
+
newMemberExpression._builder_meta = { newlyGenerated: true };
|
|
81
85
|
path.replaceWith(newMemberExpression);
|
|
82
86
|
}
|
|
83
87
|
catch (err) {
|
|
84
|
-
console.
|
|
88
|
+
console.debug('Could not replace', path.node, 'with', to.toString());
|
|
85
89
|
// throw err;
|
|
86
90
|
}
|
|
87
91
|
}
|
|
88
92
|
}
|
|
89
93
|
else {
|
|
90
94
|
if (core_1.types.isIdentifier(path.node)) {
|
|
91
|
-
console.
|
|
95
|
+
console.debug("Could not replace Identifier '".concat(from.toString(), "' with nothing."));
|
|
92
96
|
}
|
|
93
97
|
else {
|
|
94
98
|
// if we're looking at a member expression, e.g. `props.foo` and no `to` was provided, then we want to strip out
|
|
@@ -115,13 +119,16 @@ var replaceIdentifiers = function (_a) {
|
|
|
115
119
|
!core_1.types.isMemberExpression(path.parent) &&
|
|
116
120
|
!core_1.types.isOptionalMemberExpression(path.parent) &&
|
|
117
121
|
// function declaration identifiers shouldn't be transformed
|
|
118
|
-
!core_1.types.isFunctionDeclaration(path.parent)
|
|
122
|
+
!core_1.types.isFunctionDeclaration(path.parent)
|
|
123
|
+
// variable declaration identifiers shouldn't be transformed
|
|
124
|
+
// !(types.isVariableDeclarator(path.parent) && path.parent.id === path.node)
|
|
125
|
+
) {
|
|
119
126
|
_replaceIdentifiers(path, { from: from, to: to });
|
|
120
127
|
}
|
|
121
128
|
},
|
|
122
129
|
}),
|
|
123
130
|
// merely running `babel.transform` will add spaces around the code, even if we don't end up replacing anything.
|
|
124
|
-
//
|
|
131
|
+
// we have some other code downstream that cannot have untrimmed spaces, so we need to trim the output.
|
|
125
132
|
function (code) { return code.trim(); });
|
|
126
133
|
}
|
|
127
134
|
catch (err) {
|