@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.
Files changed (32) hide show
  1. package/dist/src/generators/alpine/generate.js +4 -3
  2. package/dist/src/generators/angular/index.js +32 -23
  3. package/dist/src/generators/angular/types.d.ts +12 -0
  4. package/dist/src/generators/helpers/rsc.js +2 -1
  5. package/dist/src/generators/html/generator.js +8 -7
  6. package/dist/src/generators/liquid/generator.js +2 -1
  7. package/dist/src/generators/lit/generate.js +2 -1
  8. package/dist/src/generators/marko/generate.js +2 -1
  9. package/dist/src/generators/mitosis/generator.js +6 -5
  10. package/dist/src/generators/qwik/helpers/add-prevent-default.js +3 -2
  11. package/dist/src/generators/qwik/helpers/handlers.js +2 -1
  12. package/dist/src/generators/qwik/src-generator.js +2 -1
  13. package/dist/src/generators/react/blocks.js +2 -1
  14. package/dist/src/generators/solid/blocks.js +2 -1
  15. package/dist/src/generators/stencil/helpers/index.js +2 -1
  16. package/dist/src/generators/svelte/blocks.js +3 -2
  17. package/dist/src/generators/swift/generator.js +2 -1
  18. package/dist/src/generators/template/generator.js +2 -1
  19. package/dist/src/generators/vue/blocks.js +3 -2
  20. package/dist/src/helpers/event-handlers.d.ts +2 -1
  21. package/dist/src/helpers/event-handlers.js +110 -3
  22. package/dist/src/parsers/jsx/helpers.d.ts +6 -0
  23. package/dist/src/parsers/jsx/helpers.js +28 -4
  24. package/dist/src/parsers/jsx/hooks/index.d.ts +4 -3
  25. package/dist/src/parsers/jsx/hooks/index.js +37 -26
  26. package/dist/src/parsers/jsx/hooks/use-metadata.d.ts +9 -0
  27. package/dist/src/parsers/jsx/hooks/use-metadata.js +184 -0
  28. package/dist/src/parsers/jsx/imports.d.ts +0 -2
  29. package/dist/src/parsers/jsx/imports.js +8 -26
  30. package/dist/src/parsers/jsx/jsx.js +87 -106
  31. package/dist/src/parsers/jsx/types.d.ts +7 -1
  32. 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 output = babel.transform(jsxToUse, {
89
- configFile: false,
90
- babelrc: false,
91
- comments: false,
92
- plugins: [
93
- [plugin_syntax_typescript_1.default, { isTSX: true }],
94
- () => ({
95
- visitor: {
96
- JSXExpressionContainer(path, context) {
97
- if (types.isJSXEmptyExpression(path.node.expression)) {
98
- path.remove();
99
- }
100
- },
101
- Program(path, context) {
102
- if (context.builder) {
103
- return;
104
- }
105
- beforeParse(path);
106
- context.builder = {
107
- component: (0, create_mitosis_component_1.createMitosisComponent)(),
108
- };
109
- const keepStatements = path.node.body.filter((statement) => (0, helpers_1.isImportOrDefaultExport)(statement) || (0, component_types_1.isTypeOrInterface)(statement));
110
- context.builder.component.exports = (0, exports_1.generateExports)(path);
111
- subComponentFunctions = path.node.body
112
- .filter((node) => !types.isExportDefaultDeclaration(node) && types.isFunctionDeclaration(node))
113
- .map((node) => `export default ${(0, generator_1.default)(node).code}`);
114
- const preComponentCode = (0, function_1.pipe)(path.node.body.filter((statement) => !(0, helpers_1.isImportOrDefaultExport)(statement)), (0, hooks_2.collectModuleScopeHooks)(context.builder.component, options), types.program, generator_1.default, (generatorResult) => generatorResult.code);
115
- // TODO: support multiple? e.g. for others to add imports?
116
- context.builder.component.hooks.preComponent = { code: preComponentCode };
117
- path.replaceWith(types.program(keepStatements));
118
- },
119
- FunctionDeclaration(path, context) {
120
- const { node } = path;
121
- if (types.isIdentifier(node.id)) {
122
- const name = node.id.name;
123
- if (name[0].toUpperCase() === name[0]) {
124
- path.traverse({
125
- /**
126
- * Plugin to find all `useTarget()` assignment calls inside of the component function body
127
- * and replace them with a magic string.
128
- */
129
- CallExpression(path) {
130
- if (!types.isCallExpression(path.node))
131
- return;
132
- if (!types.isIdentifier(path.node.callee))
133
- return;
134
- if (path.node.callee.name !== hooks_1.HOOKS.TARGET)
135
- return;
136
- const targetBlock = (0, use_target_1.getUseTargetStatements)(path);
137
- if (!targetBlock)
138
- return;
139
- const blockId = (0, use_target_1.getTargetId)(context.builder.component);
140
- // replace the useTarget() call with a magic string
141
- path.replaceWith(types.stringLiteral((0, use_target_1.getMagicString)(blockId)));
142
- // store the target block in the component
143
- context.builder.component.targetBlocks = {
144
- ...context.builder.component.targetBlocks,
145
- [blockId]: targetBlock,
146
- };
147
- },
148
- });
149
- path.replaceWith((0, ast_1.jsonToAst)((0, function_parser_1.componentFunctionToJson)(node, context, stateToScope)));
150
- }
151
- }
152
- },
153
- ImportDeclaration(path, context) {
154
- (0, imports_1.handleImportDeclaration)({ options, path, context });
155
- },
156
- ExportDefaultDeclaration(path) {
157
- path.replaceWith(path.node.declaration);
158
- },
159
- JSXElement(path) {
160
- const { node } = path;
161
- path.replaceWith((0, ast_1.jsonToAst)((0, element_parser_1.jsxElementToJson)(node)));
162
- },
163
- ExportNamedDeclaration(path, context) {
164
- const { node } = path;
165
- if (babel.types.isTSInterfaceDeclaration(node.declaration) ||
166
- babel.types.isTSTypeAliasDeclaration(node.declaration)) {
167
- (0, component_types_1.collectTypes)(path, context);
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
  };
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "name": "Builder.io",
23
23
  "url": "https://www.builder.io"
24
24
  },
25
- "version": "0.5.24",
25
+ "version": "0.5.25",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {