@builder.io/mitosis 0.5.24 → 0.5.25
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.js +4 -3
- package/dist/src/generators/angular/index.js +32 -23
- package/dist/src/generators/angular/types.d.ts +12 -0
- package/dist/src/generators/helpers/rsc.js +2 -1
- package/dist/src/generators/html/generator.js +8 -7
- package/dist/src/generators/liquid/generator.js +2 -1
- package/dist/src/generators/lit/generate.js +2 -1
- package/dist/src/generators/marko/generate.js +2 -1
- package/dist/src/generators/mitosis/generator.js +6 -5
- package/dist/src/generators/qwik/helpers/add-prevent-default.js +3 -2
- package/dist/src/generators/qwik/helpers/handlers.js +2 -1
- package/dist/src/generators/qwik/src-generator.js +2 -1
- package/dist/src/generators/react/blocks.js +2 -1
- package/dist/src/generators/solid/blocks.js +2 -1
- package/dist/src/generators/stencil/helpers/index.js +2 -1
- package/dist/src/generators/svelte/blocks.js +3 -2
- package/dist/src/generators/swift/generator.js +2 -1
- package/dist/src/generators/template/generator.js +2 -1
- package/dist/src/generators/vue/blocks.js +3 -2
- package/dist/src/helpers/event-handlers.d.ts +2 -1
- package/dist/src/helpers/event-handlers.js +110 -3
- package/dist/src/parsers/jsx/helpers.d.ts +6 -0
- package/dist/src/parsers/jsx/helpers.js +28 -4
- package/dist/src/parsers/jsx/hooks/index.d.ts +4 -3
- package/dist/src/parsers/jsx/hooks/index.js +37 -26
- package/dist/src/parsers/jsx/hooks/use-metadata.d.ts +9 -0
- package/dist/src/parsers/jsx/hooks/use-metadata.js +184 -0
- package/dist/src/parsers/jsx/imports.d.ts +0 -2
- package/dist/src/parsers/jsx/imports.js +8 -26
- package/dist/src/parsers/jsx/jsx.js +87 -106
- package/dist/src/parsers/jsx/types.d.ts +7 -1
- package/package.json +1 -1
|
@@ -27,18 +27,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.parseJsx = void 0;
|
|
30
|
+
const hooks_1 = require("../../constants/hooks");
|
|
31
|
+
const create_mitosis_component_1 = require("../../helpers/create-mitosis-component");
|
|
30
32
|
const filter_empty_text_nodes_1 = require("../../helpers/filter-empty-text-nodes");
|
|
33
|
+
const json_1 = require("../../helpers/json");
|
|
34
|
+
const replace_new_lines_in_strings_1 = require("../../helpers/replace-new-lines-in-strings");
|
|
35
|
+
const signals_1 = require("../../helpers/signals");
|
|
31
36
|
const traverse_nodes_1 = require("../../helpers/traverse-nodes");
|
|
32
37
|
const babel = __importStar(require("@babel/core"));
|
|
33
38
|
const generator_1 = __importDefault(require("@babel/generator"));
|
|
34
|
-
const plugin_syntax_typescript_1 = __importDefault(require("@babel/plugin-syntax-typescript"));
|
|
35
39
|
const preset_typescript_1 = __importDefault(require("@babel/preset-typescript"));
|
|
36
40
|
const function_1 = require("fp-ts/lib/function");
|
|
37
|
-
const hooks_1 = require("../../constants/hooks");
|
|
38
|
-
const create_mitosis_component_1 = require("../../helpers/create-mitosis-component");
|
|
39
|
-
const json_1 = require("../../helpers/json");
|
|
40
|
-
const replace_new_lines_in_strings_1 = require("../../helpers/replace-new-lines-in-strings");
|
|
41
|
-
const signals_1 = require("../../helpers/signals");
|
|
42
41
|
const ast_1 = require("./ast");
|
|
43
42
|
const component_types_1 = require("./component-types");
|
|
44
43
|
const context_1 = require("./context");
|
|
@@ -70,112 +69,94 @@ const beforeParse = (path) => {
|
|
|
70
69
|
* @returns A JSON representation of the Mitosis component
|
|
71
70
|
*/
|
|
72
71
|
function parseJsx(jsx, _options = {}) {
|
|
73
|
-
var _a;
|
|
74
72
|
let subComponentFunctions = [];
|
|
75
73
|
const options = {
|
|
76
74
|
typescript: false,
|
|
77
75
|
..._options,
|
|
78
76
|
};
|
|
79
|
-
const jsxToUse = options.typescript
|
|
80
|
-
? jsx
|
|
81
|
-
: // strip typescript types by running through babel's TS preset.
|
|
82
|
-
(_a = babel.transform(jsx, {
|
|
83
|
-
configFile: false,
|
|
84
|
-
babelrc: false,
|
|
85
|
-
presets: [typescriptBabelPreset],
|
|
86
|
-
})) === null || _a === void 0 ? void 0 : _a.code;
|
|
87
77
|
const stateToScope = [];
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
TSTypeAliasDeclaration(path, context) {
|
|
171
|
-
(0, component_types_1.collectTypes)(path, context);
|
|
172
|
-
},
|
|
173
|
-
TSInterfaceDeclaration(path, context) {
|
|
174
|
-
(0, component_types_1.collectTypes)(path, context);
|
|
175
|
-
},
|
|
176
|
-
},
|
|
177
|
-
}),
|
|
178
|
-
],
|
|
78
|
+
const jsxToUse = (0, helpers_1.babelStripTypes)(jsx, !options.typescript);
|
|
79
|
+
const output = (0, helpers_1.babelDefaultTransform)(jsxToUse, {
|
|
80
|
+
JSXExpressionContainer(path, context) {
|
|
81
|
+
if (types.isJSXEmptyExpression(path.node.expression)) {
|
|
82
|
+
path.remove();
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
Program(path, context) {
|
|
86
|
+
if (context.builder) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
beforeParse(path);
|
|
90
|
+
context.builder = {
|
|
91
|
+
component: (0, create_mitosis_component_1.createMitosisComponent)(),
|
|
92
|
+
};
|
|
93
|
+
const keepStatements = path.node.body.filter((statement) => (0, helpers_1.isImportOrDefaultExport)(statement) || (0, component_types_1.isTypeOrInterface)(statement));
|
|
94
|
+
context.builder.component.exports = (0, exports_1.generateExports)(path);
|
|
95
|
+
subComponentFunctions = path.node.body
|
|
96
|
+
.filter((node) => !types.isExportDefaultDeclaration(node) && types.isFunctionDeclaration(node))
|
|
97
|
+
.map((node) => `export default ${(0, generator_1.default)(node).code}`);
|
|
98
|
+
const preComponentCode = (0, function_1.pipe)(path, (0, hooks_2.collectModuleScopeHooks)(context, options), types.program, generator_1.default, (generatorResult) => generatorResult.code);
|
|
99
|
+
// TODO: support multiple? e.g. for others to add imports?
|
|
100
|
+
context.builder.component.hooks.preComponent = { code: preComponentCode };
|
|
101
|
+
path.replaceWith(types.program(keepStatements));
|
|
102
|
+
},
|
|
103
|
+
FunctionDeclaration(path, context) {
|
|
104
|
+
const { node } = path;
|
|
105
|
+
if (types.isIdentifier(node.id)) {
|
|
106
|
+
const name = node.id.name;
|
|
107
|
+
if (name[0].toUpperCase() === name[0]) {
|
|
108
|
+
path.traverse({
|
|
109
|
+
/**
|
|
110
|
+
* Plugin to find all `useTarget()` assignment calls inside of the component function body
|
|
111
|
+
* and replace them with a magic string.
|
|
112
|
+
*/
|
|
113
|
+
CallExpression(path) {
|
|
114
|
+
if (!types.isCallExpression(path.node))
|
|
115
|
+
return;
|
|
116
|
+
if (!types.isIdentifier(path.node.callee))
|
|
117
|
+
return;
|
|
118
|
+
if (path.node.callee.name !== hooks_1.HOOKS.TARGET)
|
|
119
|
+
return;
|
|
120
|
+
const targetBlock = (0, use_target_1.getUseTargetStatements)(path);
|
|
121
|
+
if (!targetBlock)
|
|
122
|
+
return;
|
|
123
|
+
const blockId = (0, use_target_1.getTargetId)(context.builder.component);
|
|
124
|
+
// replace the useTarget() call with a magic string
|
|
125
|
+
path.replaceWith(types.stringLiteral((0, use_target_1.getMagicString)(blockId)));
|
|
126
|
+
// store the target block in the component
|
|
127
|
+
context.builder.component.targetBlocks = {
|
|
128
|
+
...context.builder.component.targetBlocks,
|
|
129
|
+
[blockId]: targetBlock,
|
|
130
|
+
};
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
path.replaceWith((0, ast_1.jsonToAst)((0, function_parser_1.componentFunctionToJson)(node, context, stateToScope)));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
ImportDeclaration(path, context) {
|
|
138
|
+
(0, imports_1.handleImportDeclaration)({ options, path, context });
|
|
139
|
+
},
|
|
140
|
+
ExportDefaultDeclaration(path) {
|
|
141
|
+
path.replaceWith(path.node.declaration);
|
|
142
|
+
},
|
|
143
|
+
JSXElement(path) {
|
|
144
|
+
const { node } = path;
|
|
145
|
+
path.replaceWith((0, ast_1.jsonToAst)((0, element_parser_1.jsxElementToJson)(node)));
|
|
146
|
+
},
|
|
147
|
+
ExportNamedDeclaration(path, context) {
|
|
148
|
+
const { node } = path;
|
|
149
|
+
if (babel.types.isTSInterfaceDeclaration(node.declaration) ||
|
|
150
|
+
babel.types.isTSTypeAliasDeclaration(node.declaration)) {
|
|
151
|
+
(0, component_types_1.collectTypes)(path, context);
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
TSTypeAliasDeclaration(path, context) {
|
|
155
|
+
(0, component_types_1.collectTypes)(path, context);
|
|
156
|
+
},
|
|
157
|
+
TSInterfaceDeclaration(path, context) {
|
|
158
|
+
(0, component_types_1.collectTypes)(path, context);
|
|
159
|
+
},
|
|
179
160
|
});
|
|
180
161
|
if (!output || !output.code) {
|
|
181
162
|
throw new Error('Could not parse JSX');
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Project } from 'ts-morph';
|
|
2
1
|
import { MitosisComponent } from '../../types/mitosis-component';
|
|
2
|
+
import { Project } from 'ts-morph';
|
|
3
3
|
export type ParseMitosisOptions = {
|
|
4
4
|
jsonHookNames?: string[];
|
|
5
5
|
compileAwayPackages?: string[];
|
|
@@ -9,8 +9,14 @@ export type ParseMitosisOptions = {
|
|
|
9
9
|
};
|
|
10
10
|
filePath?: string;
|
|
11
11
|
};
|
|
12
|
+
export type ResolvedImport = {
|
|
13
|
+
path: string;
|
|
14
|
+
value: string;
|
|
15
|
+
};
|
|
12
16
|
export type Context = {
|
|
17
|
+
cwd?: string;
|
|
13
18
|
builder: {
|
|
14
19
|
component: MitosisComponent;
|
|
20
|
+
resolvedImports?: ResolvedImport[];
|
|
15
21
|
};
|
|
16
22
|
};
|