@builder.io/mitosis 0.5.17 → 0.5.19
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/index.js +47 -7
- package/dist/src/generators/react/blocks.js +3 -3
- package/dist/src/generators/react/helpers.d.ts +2 -1
- package/dist/src/generators/react/helpers.js +16 -4
- package/dist/src/generators/solid/blocks.js +3 -2
- package/dist/src/parsers/jsx/function-parser.d.ts +1 -1
- package/dist/src/parsers/jsx/function-parser.js +3 -1
- package/dist/src/parsers/jsx/jsx.js +3 -2
- package/dist/src/parsers/jsx/state.d.ts +8 -1
- package/dist/src/parsers/jsx/state.js +10 -3
- package/package.json +1 -1
|
@@ -1,4 +1,27 @@
|
|
|
1
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
27
|
};
|
|
@@ -35,6 +58,7 @@ const traverse_nodes_1 = require("../../helpers/traverse-nodes");
|
|
|
35
58
|
const plugins_1 = require("../../modules/plugins");
|
|
36
59
|
const symbol_processor_1 = require("../../symbols/symbol-processor");
|
|
37
60
|
const mitosis_node_1 = require("../../types/mitosis-node");
|
|
61
|
+
const babel = __importStar(require("@babel/core"));
|
|
38
62
|
const function_1 = require("fp-ts/lib/function");
|
|
39
63
|
const lodash_1 = require("lodash");
|
|
40
64
|
const legacy_1 = __importDefault(require("neotraverse/legacy"));
|
|
@@ -43,6 +67,7 @@ const is_children_1 = __importDefault(require("../../helpers/is-children"));
|
|
|
43
67
|
const on_mount_1 = require("../helpers/on-mount");
|
|
44
68
|
const helpers_2 = require("./helpers");
|
|
45
69
|
const types_1 = require("./types");
|
|
70
|
+
const { types } = babel;
|
|
46
71
|
const mappers = {
|
|
47
72
|
Fragment: (root, json, options, blockOptions) => {
|
|
48
73
|
return `<ng-container>${json.children
|
|
@@ -438,12 +463,31 @@ const traverseToGetAllDynamicComponents = (json, options, blockOptions) => {
|
|
|
438
463
|
dynamicTemplate,
|
|
439
464
|
};
|
|
440
465
|
};
|
|
441
|
-
|
|
466
|
+
/**
|
|
467
|
+
* Prefixes state identifiers with this.
|
|
468
|
+
* e.g. state.foo --> this.foo
|
|
469
|
+
*/
|
|
470
|
+
const prefixState = (code) => {
|
|
471
|
+
return (0, replace_identifiers_1.replaceNodes)({
|
|
472
|
+
code,
|
|
473
|
+
nodeMaps: [
|
|
474
|
+
{
|
|
475
|
+
from: types.identifier('state'),
|
|
476
|
+
to: types.thisExpression(),
|
|
477
|
+
},
|
|
478
|
+
],
|
|
479
|
+
}).trim();
|
|
480
|
+
};
|
|
481
|
+
const processAngularCode = ({ contextVars, outputVars, domRefs, replaceWith, }) => (code) => (0, function_1.pipe)((0, strip_state_and_props_refs_1.DO_NOT_USE_VARS_TRANSFORMS)(code, {
|
|
442
482
|
contextVars,
|
|
443
483
|
domRefs,
|
|
444
484
|
outputVars,
|
|
445
|
-
|
|
446
|
-
|
|
485
|
+
}),
|
|
486
|
+
/**
|
|
487
|
+
* Only prefix state that is in the Angular class component.
|
|
488
|
+
* Do not prefix state referenced in the template
|
|
489
|
+
*/
|
|
490
|
+
replaceWith === 'this' ? prefixState : (x) => x, (newCode) => (0, strip_state_and_props_refs_1.stripStateAndPropsRefs)(newCode, { replaceWith }));
|
|
447
491
|
const isASimpleProperty = (code) => {
|
|
448
492
|
const expressions = ['==', '===', '!=', '!==', '<', '>', '<=', '>='];
|
|
449
493
|
const invalidChars = ['{', '}', '(', ')', 'typeof'];
|
|
@@ -578,7 +622,6 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
|
|
|
578
622
|
// TODO: Why is 'outputs' used here and shouldn't it be typed in packages/core/src/types/metadata.ts
|
|
579
623
|
const metaOutputVars = (useMetadata === null || useMetadata === void 0 ? void 0 : useMetadata.outputs) || [];
|
|
580
624
|
const outputVars = (0, lodash_1.uniq)([...metaOutputVars, ...(0, get_prop_functions_1.getPropFunctions)(json)]);
|
|
581
|
-
const stateVars = Object.keys((json === null || json === void 0 ? void 0 : json.state) || {});
|
|
582
625
|
const options = (0, merge_options_1.initializeOptions)({
|
|
583
626
|
target: 'angular',
|
|
584
627
|
component: _component,
|
|
@@ -595,7 +638,6 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
|
|
|
595
638
|
contextVars,
|
|
596
639
|
outputVars,
|
|
597
640
|
domRefs: Array.from((0, get_refs_1.getRefs)(json)),
|
|
598
|
-
stateVars,
|
|
599
641
|
}), (code) => {
|
|
600
642
|
const allMethodNames = Object.entries(json.state)
|
|
601
643
|
.filter(([_, value]) => (value === null || value === void 0 ? void 0 : value.type) === 'function' || (value === null || value === void 0 ? void 0 : value.type) === 'method')
|
|
@@ -731,7 +773,6 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
|
|
|
731
773
|
contextVars,
|
|
732
774
|
outputVars,
|
|
733
775
|
domRefs: Array.from(domRefs),
|
|
734
|
-
stateVars,
|
|
735
776
|
})(value);
|
|
736
777
|
},
|
|
737
778
|
});
|
|
@@ -851,7 +892,6 @@ const componentToAngular = (userOptions = {}) => ({ component: _component }) =>
|
|
|
851
892
|
contextVars,
|
|
852
893
|
outputVars,
|
|
853
894
|
domRefs: Array.from(domRefs),
|
|
854
|
-
stateVars,
|
|
855
895
|
})(argument)}`
|
|
856
896
|
: ''};`;
|
|
857
897
|
})
|
|
@@ -57,10 +57,10 @@ const NODE_MAPPERS = {
|
|
|
57
57
|
return `<>{${slotProp} ${hasChildren ? `|| (${renderChildren()})` : ''}}</>`;
|
|
58
58
|
},
|
|
59
59
|
Fragment(json, options, component) {
|
|
60
|
-
const wrap = (0, helpers_1.wrapInFragment)(json) || (0, is_root_text_node_1.isRootTextNode)(json);
|
|
61
|
-
return `${wrap ? (0, helpers_1.getFragment)('open', options) : ''}${json.children
|
|
60
|
+
const wrap = (0, helpers_1.isFragmentWithKey)(json) || (0, helpers_1.wrapInFragment)(json) || (0, is_root_text_node_1.isRootTextNode)(json);
|
|
61
|
+
return `${wrap ? (0, helpers_1.getFragment)('open', options, json) : ''}${json.children
|
|
62
62
|
.map((item) => (0, exports.blockToReact)(item, options, component, wrap))
|
|
63
|
-
.join('\n')}${wrap ? (0, helpers_1.getFragment)('close', options) : ''}`;
|
|
63
|
+
.join('\n')}${wrap ? (0, helpers_1.getFragment)('close', options, json) : ''}`;
|
|
64
64
|
},
|
|
65
65
|
For(_json, options, component, insideJsx) {
|
|
66
66
|
var _a;
|
|
@@ -4,6 +4,7 @@ import { ToReactOptions } from './types';
|
|
|
4
4
|
export declare const processBinding: (str: string, options: ToReactOptions) => string;
|
|
5
5
|
export declare const openFrag: (options: ToReactOptions) => string;
|
|
6
6
|
export declare const closeFrag: (options: ToReactOptions) => string;
|
|
7
|
-
export declare
|
|
7
|
+
export declare const isFragmentWithKey: (node?: MitosisNode) => boolean;
|
|
8
|
+
export declare function getFragment(type: 'open' | 'close', options: ToReactOptions, node?: MitosisNode): string;
|
|
8
9
|
export declare const wrapInFragment: (json: MitosisComponent | MitosisNode) => boolean;
|
|
9
10
|
export declare function processTagReferences(json: MitosisComponent, options: ToReactOptions): void;
|
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.processTagReferences = exports.wrapInFragment = exports.getFragment = exports.closeFrag = exports.openFrag = exports.processBinding = void 0;
|
|
6
|
+
exports.processTagReferences = exports.wrapInFragment = exports.getFragment = exports.isFragmentWithKey = exports.closeFrag = exports.openFrag = exports.processBinding = void 0;
|
|
7
7
|
const is_mitosis_node_1 = require("../../helpers/is-mitosis-node");
|
|
8
8
|
const strip_state_and_props_refs_1 = require("../../helpers/strip-state-and-props-refs");
|
|
9
9
|
const lodash_1 = require("lodash");
|
|
@@ -23,9 +23,21 @@ const openFrag = (options) => getFragment('open', options);
|
|
|
23
23
|
exports.openFrag = openFrag;
|
|
24
24
|
const closeFrag = (options) => getFragment('close', options);
|
|
25
25
|
exports.closeFrag = closeFrag;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
const isFragmentWithKey = (node) => (node === null || node === void 0 ? void 0 : node.name) === 'Fragment' && !!(node === null || node === void 0 ? void 0 : node.bindings['key']);
|
|
27
|
+
exports.isFragmentWithKey = isFragmentWithKey;
|
|
28
|
+
function getFragment(type, options, node) {
|
|
29
|
+
var _a;
|
|
30
|
+
let tag = '';
|
|
31
|
+
if (node && node.bindings && (0, exports.isFragmentWithKey)(node)) {
|
|
32
|
+
tag = options.preact ? 'Fragment' : 'React.Fragment';
|
|
33
|
+
if (type === 'open') {
|
|
34
|
+
tag += ` key={${(_a = node.bindings['key']) === null || _a === void 0 ? void 0 : _a.code}}`;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else if (options.preact) {
|
|
38
|
+
tag = 'Fragment';
|
|
39
|
+
}
|
|
40
|
+
return type === 'open' ? `<${tag}>` : `</${tag}>`;
|
|
29
41
|
}
|
|
30
42
|
exports.getFragment = getFragment;
|
|
31
43
|
const wrapInFragment = (json) => json.children.length !== 1;
|
|
@@ -50,7 +50,8 @@ const blockToSolid = (json, component, options, insideJsx) => {
|
|
|
50
50
|
</For>`;
|
|
51
51
|
}
|
|
52
52
|
let str = '';
|
|
53
|
-
|
|
53
|
+
const isFragmentWithoutKey = json.name === 'Fragment' && !json.bindings.key;
|
|
54
|
+
if (isFragmentWithoutKey) {
|
|
54
55
|
str += '<';
|
|
55
56
|
}
|
|
56
57
|
else {
|
|
@@ -121,7 +122,7 @@ const blockToSolid = (json, component, options, insideJsx) => {
|
|
|
121
122
|
.map((item) => (0, exports.blockToSolid)(item, component, options, true))
|
|
122
123
|
.join('\n');
|
|
123
124
|
}
|
|
124
|
-
if (
|
|
125
|
+
if (isFragmentWithoutKey) {
|
|
125
126
|
str += '</>';
|
|
126
127
|
}
|
|
127
128
|
else {
|
|
@@ -5,5 +5,5 @@ declare const types: typeof babel.types;
|
|
|
5
5
|
/**
|
|
6
6
|
* Parses function declarations within the Mitosis copmonent's body to JSON
|
|
7
7
|
*/
|
|
8
|
-
export declare const componentFunctionToJson: (node: babel.types.FunctionDeclaration, context: Context) => JSONOrNode;
|
|
8
|
+
export declare const componentFunctionToJson: (node: babel.types.FunctionDeclaration, context: Context, stateToScope: string[]) => JSONOrNode;
|
|
9
9
|
export {};
|
|
@@ -43,7 +43,7 @@ const { types } = babel;
|
|
|
43
43
|
/**
|
|
44
44
|
* Parses function declarations within the Mitosis copmonent's body to JSON
|
|
45
45
|
*/
|
|
46
|
-
const componentFunctionToJson = (node, context) => {
|
|
46
|
+
const componentFunctionToJson = (node, context, stateToScope) => {
|
|
47
47
|
var _a;
|
|
48
48
|
const hooks = {
|
|
49
49
|
onMount: [],
|
|
@@ -206,6 +206,7 @@ const componentFunctionToJson = (node, context) => {
|
|
|
206
206
|
code: (0, generator_1.default)(item).code,
|
|
207
207
|
type: 'function',
|
|
208
208
|
};
|
|
209
|
+
stateToScope.push(item.id.name);
|
|
209
210
|
}
|
|
210
211
|
}
|
|
211
212
|
if (types.isVariableDeclaration(item)) {
|
|
@@ -247,6 +248,7 @@ const componentFunctionToJson = (node, context) => {
|
|
|
247
248
|
propertyType,
|
|
248
249
|
};
|
|
249
250
|
}
|
|
251
|
+
stateToScope.push(varName);
|
|
250
252
|
// Typescript Parameter
|
|
251
253
|
if (types.isTSTypeParameterInstantiation(init.typeParameters)) {
|
|
252
254
|
state[varName].typeParameter = (0, generator_1.default)(init.typeParameters.params[0]).code;
|
|
@@ -84,6 +84,7 @@ function parseJsx(jsx, _options = {}) {
|
|
|
84
84
|
babelrc: false,
|
|
85
85
|
presets: [typescriptBabelPreset],
|
|
86
86
|
})) === null || _a === void 0 ? void 0 : _a.code;
|
|
87
|
+
const stateToScope = [];
|
|
87
88
|
const output = babel.transform(jsxToUse, {
|
|
88
89
|
configFile: false,
|
|
89
90
|
babelrc: false,
|
|
@@ -145,7 +146,7 @@ function parseJsx(jsx, _options = {}) {
|
|
|
145
146
|
};
|
|
146
147
|
},
|
|
147
148
|
});
|
|
148
|
-
path.replaceWith((0, ast_1.jsonToAst)((0, function_parser_1.componentFunctionToJson)(node, context)));
|
|
149
|
+
path.replaceWith((0, ast_1.jsonToAst)((0, function_parser_1.componentFunctionToJson)(node, context, stateToScope)));
|
|
149
150
|
}
|
|
150
151
|
}
|
|
151
152
|
},
|
|
@@ -189,7 +190,7 @@ function parseJsx(jsx, _options = {}) {
|
|
|
189
190
|
.replace(/^\({/, '{')
|
|
190
191
|
.replace(/}\);$/, '}'));
|
|
191
192
|
const mitosisComponent = (0, json_1.tryParseJson)(stringifiedMitosisComponent);
|
|
192
|
-
(0, state_1.mapStateIdentifiers)(mitosisComponent);
|
|
193
|
+
(0, state_1.mapStateIdentifiers)(mitosisComponent, stateToScope);
|
|
193
194
|
(0, context_1.extractContextComponents)(mitosisComponent);
|
|
194
195
|
mitosisComponent.subComponents = subComponentFunctions.map((item) => parseJsx(item, options));
|
|
195
196
|
const signalTypeImportName = (0, signals_1.getSignalImportName)(jsxToUse);
|
|
@@ -5,6 +5,13 @@ import { ObjectExpression } from '@babel/types';
|
|
|
5
5
|
* e.g.
|
|
6
6
|
* text -> state.text
|
|
7
7
|
* setText(...) -> state.text = ...
|
|
8
|
+
*
|
|
9
|
+
* This also applies to components that use both useState and useStore.
|
|
10
|
+
* e.g.
|
|
11
|
+
* const [foo, setFoo] = useState(1)
|
|
12
|
+
* const store = useStore({
|
|
13
|
+
* bar() { return foo } // becomes bar() { return state.foo }
|
|
14
|
+
* })`
|
|
8
15
|
*/
|
|
9
|
-
export declare function mapStateIdentifiers(json: MitosisComponent): void;
|
|
16
|
+
export declare function mapStateIdentifiers(json: MitosisComponent, stateProperties: string[]): void;
|
|
10
17
|
export declare const parseStateObjectToMitosisState: (object: ObjectExpression, isState?: boolean) => MitosisState;
|
|
@@ -97,9 +97,15 @@ const consolidateClassBindings = (item) => {
|
|
|
97
97
|
* e.g.
|
|
98
98
|
* text -> state.text
|
|
99
99
|
* setText(...) -> state.text = ...
|
|
100
|
+
*
|
|
101
|
+
* This also applies to components that use both useState and useStore.
|
|
102
|
+
* e.g.
|
|
103
|
+
* const [foo, setFoo] = useState(1)
|
|
104
|
+
* const store = useStore({
|
|
105
|
+
* bar() { return foo } // becomes bar() { return state.foo }
|
|
106
|
+
* })`
|
|
100
107
|
*/
|
|
101
|
-
function mapStateIdentifiers(json) {
|
|
102
|
-
const stateProperties = Object.keys(json.state);
|
|
108
|
+
function mapStateIdentifiers(json, stateProperties) {
|
|
103
109
|
const plugin = (0, process_code_1.createCodeProcessorPlugin)(() => (code) => mapStateIdentifiersInExpression(code, stateProperties));
|
|
104
110
|
plugin(json);
|
|
105
111
|
for (const key in json.targetBlocks) {
|
|
@@ -198,7 +204,8 @@ const processStateObjectSlice = (item) => {
|
|
|
198
204
|
type: 'function',
|
|
199
205
|
};
|
|
200
206
|
}
|
|
201
|
-
const
|
|
207
|
+
const method = (0, types_1.objectMethod)(item.kind, item.key, item.params, item.body, false, false, item.async);
|
|
208
|
+
const n = getCleanedStateCode((0, helpers_1.parseCode)({ ...method, returnType: null }));
|
|
202
209
|
const isGetter = item.kind === 'get';
|
|
203
210
|
return {
|
|
204
211
|
code: n,
|