@builder.io/mitosis 0.5.23 → 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 (49) hide show
  1. package/dist/src/generators/alpine/generate.js +4 -3
  2. package/dist/src/generators/angular/helpers.d.ts +6 -0
  3. package/dist/src/generators/angular/helpers.js +14 -1
  4. package/dist/src/generators/angular/index.js +53 -41
  5. package/dist/src/generators/angular/types.d.ts +12 -0
  6. package/dist/src/generators/helpers/functions.d.ts +2 -2
  7. package/dist/src/generators/helpers/rsc.js +2 -1
  8. package/dist/src/generators/html/generator.js +8 -7
  9. package/dist/src/generators/liquid/generator.js +2 -1
  10. package/dist/src/generators/lit/generate.js +2 -1
  11. package/dist/src/generators/marko/generate.js +2 -1
  12. package/dist/src/generators/mitosis/generator.js +6 -5
  13. package/dist/src/generators/qwik/helpers/add-prevent-default.js +3 -2
  14. package/dist/src/generators/qwik/helpers/handlers.js +2 -1
  15. package/dist/src/generators/qwik/src-generator.js +2 -1
  16. package/dist/src/generators/react/blocks.js +2 -1
  17. package/dist/src/generators/react/helpers.js +3 -2
  18. package/dist/src/generators/solid/blocks.js +2 -1
  19. package/dist/src/generators/stencil/helpers/index.js +2 -1
  20. package/dist/src/generators/stencil/plugins/get-code-processor-plugins.d.ts +1 -1
  21. package/dist/src/generators/svelte/blocks.js +3 -2
  22. package/dist/src/generators/swift/generator.js +2 -1
  23. package/dist/src/generators/template/generator.js +2 -1
  24. package/dist/src/generators/vue/blocks.js +3 -2
  25. package/dist/src/helpers/component-file-extensions.js +21 -5
  26. package/dist/src/helpers/event-handlers.d.ts +2 -1
  27. package/dist/src/helpers/event-handlers.js +110 -3
  28. package/dist/src/helpers/on-event.d.ts +2 -2
  29. package/dist/src/helpers/plugins/process-code/index.d.ts +2 -2
  30. package/dist/src/helpers/plugins/process-signals.d.ts +3 -3
  31. package/dist/src/helpers/plugins/process-target-blocks.d.ts +2 -2
  32. package/dist/src/modules/plugins.d.ts +6 -6
  33. package/dist/src/parsers/jsx/helpers.d.ts +6 -0
  34. package/dist/src/parsers/jsx/helpers.js +28 -4
  35. package/dist/src/parsers/jsx/hooks/index.d.ts +4 -3
  36. package/dist/src/parsers/jsx/hooks/index.js +37 -26
  37. package/dist/src/parsers/jsx/hooks/use-metadata.d.ts +9 -0
  38. package/dist/src/parsers/jsx/hooks/use-metadata.js +184 -0
  39. package/dist/src/parsers/jsx/imports.d.ts +0 -2
  40. package/dist/src/parsers/jsx/imports.js +8 -26
  41. package/dist/src/parsers/jsx/jsx.js +87 -106
  42. package/dist/src/parsers/jsx/types.d.ts +7 -1
  43. package/dist/src/parsers/svelte/helpers/post-process.d.ts +1 -1
  44. package/dist/src/plugins/compile-away-builder-components.d.ts +2 -2
  45. package/dist/src/types/config.d.ts +11 -2
  46. package/dist/src/types/mitosis-component.d.ts +9 -0
  47. package/dist/src/types/plugins.d.ts +17 -9
  48. package/dist/src/types/transpiler.d.ts +12 -2
  49. package/package.json +1 -1
@@ -0,0 +1,184 @@
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
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.resolveMetadata = void 0;
27
+ const mitosis_imports_1 = require("../../../helpers/mitosis-imports");
28
+ const helpers_1 = require("../../../parsers/jsx/helpers");
29
+ const babel = __importStar(require("@babel/core"));
30
+ const fs_extra_promise_1 = require("fs-extra-promise");
31
+ const path = __importStar(require("path"));
32
+ const getCodeFromImport = (importObject, currentFile) => {
33
+ if (currentFile) {
34
+ // resolve path of import
35
+ const originFile = path.basename(currentFile);
36
+ const typescript = (0, helpers_1.isTypescriptFile)(originFile);
37
+ const importFile = (0, helpers_1.isTypescriptFile)(importObject.path) || importObject.path.endsWith('.js')
38
+ ? importObject.path
39
+ : `${importObject.path}.${typescript ? 'ts' : 'js'}`;
40
+ const importFilePath = path.resolve(path.dirname(currentFile), importFile);
41
+ if ((0, fs_extra_promise_1.existsSync)(importFilePath)) {
42
+ return { code: (0, fs_extra_promise_1.readFileSync)(importFilePath).toString(), typescript, importFilePath };
43
+ }
44
+ return { typescript };
45
+ }
46
+ return {};
47
+ };
48
+ const fillDeclarations = ({ declaration, valueToResolve, currentFilePath, nodePath, }) => {
49
+ let result = {};
50
+ for (const variable of declaration.declarations) {
51
+ if (babel.types.isIdentifier(variable.id)) {
52
+ if (variable.id.name === valueToResolve && variable.init) {
53
+ const filled = resolveObjectsRecursive({
54
+ node: variable.init,
55
+ nodePath,
56
+ currentFilePath,
57
+ });
58
+ result = {
59
+ ...result,
60
+ ...filled,
61
+ };
62
+ }
63
+ }
64
+ }
65
+ return result;
66
+ };
67
+ const resolve = ({ nodePath, currentFilePath, valueToResolve, resolvedImports, }) => {
68
+ let result = {};
69
+ const programNodes = nodePath.node.body;
70
+ for (const statement of programNodes) {
71
+ if (babel.types.isImportDeclaration(statement)) {
72
+ const importObject = (0, mitosis_imports_1.mapImportDeclarationToMitosisImport)(statement);
73
+ if (Object.keys(importObject.imports).includes(valueToResolve)) {
74
+ if (resolvedImports) {
75
+ // We add this statement, to remove it from imports of generated file
76
+ resolvedImports.push({ path: importObject.path, value: valueToResolve });
77
+ }
78
+ // In this case the variable was imported
79
+ const { code, typescript, importFilePath } = getCodeFromImport(importObject, currentFilePath);
80
+ if (code) {
81
+ const jsxToUse = (0, helpers_1.babelStripTypes)(code, typescript);
82
+ (0, helpers_1.babelDefaultTransform)(jsxToUse, {
83
+ Program(path) {
84
+ const statements = path.node.body;
85
+ for (const pStatement of statements) {
86
+ if (babel.types.isExportNamedDeclaration(pStatement)) {
87
+ const declaration = pStatement.declaration;
88
+ if (babel.types.isVariableDeclaration(declaration)) {
89
+ const filledDeclaration = fillDeclarations({
90
+ declaration,
91
+ valueToResolve,
92
+ currentFilePath: importFilePath,
93
+ nodePath: path,
94
+ });
95
+ result = {
96
+ ...result,
97
+ ...filledDeclaration,
98
+ };
99
+ }
100
+ }
101
+ }
102
+ },
103
+ });
104
+ }
105
+ }
106
+ }
107
+ else if (babel.types.isVariableDeclaration(statement)) {
108
+ // In this case the variable is inside the same file
109
+ const filledDeclaration = fillDeclarations({
110
+ declaration: statement,
111
+ valueToResolve,
112
+ currentFilePath,
113
+ nodePath,
114
+ });
115
+ result = {
116
+ ...result,
117
+ ...filledDeclaration,
118
+ };
119
+ }
120
+ }
121
+ return result;
122
+ };
123
+ const resolveObjectsRecursive = ({ node, nodePath, currentFilePath, resolvedImports, }) => {
124
+ let result = {};
125
+ if (babel.types.isObjectExpression(node)) {
126
+ for (const prop of node.properties) {
127
+ if (babel.types.isObjectProperty(prop)) {
128
+ if (babel.types.isIdentifier(prop.key)) {
129
+ const objectKey = prop.key.name;
130
+ if (babel.types.isIdentifier(prop.value)) {
131
+ const valueToResolve = prop.value.name;
132
+ // In this case we have some variable defined in the same or another file
133
+ const resolved = resolve({
134
+ nodePath,
135
+ currentFilePath,
136
+ valueToResolve,
137
+ resolvedImports,
138
+ });
139
+ result = {
140
+ ...result,
141
+ [objectKey]: { ...resolved },
142
+ };
143
+ }
144
+ else {
145
+ // In this case we have a primitive value
146
+ const json = (0, helpers_1.parseCodeJson)(prop.value);
147
+ result = {
148
+ ...result,
149
+ [objectKey]: json,
150
+ };
151
+ }
152
+ }
153
+ }
154
+ else if (babel.types.isSpreadElement(prop)) {
155
+ if (babel.types.isIdentifier(prop.argument)) {
156
+ const valueToResolve = prop.argument.name;
157
+ result = {
158
+ ...result,
159
+ ...resolve({ nodePath, currentFilePath, valueToResolve }),
160
+ };
161
+ }
162
+ }
163
+ else {
164
+ // In this case we have a primitive value
165
+ result = {
166
+ ...result,
167
+ ...(0, helpers_1.parseCodeJson)(prop),
168
+ };
169
+ }
170
+ }
171
+ }
172
+ return result;
173
+ };
174
+ const resolveMetadata = ({ context, node, nodePath, options, }) => {
175
+ if (context.cwd && (options === null || options === void 0 ? void 0 : options.filePath)) {
176
+ const resolvedImports = [];
177
+ const currentFilePath = `${context.cwd}/${options.filePath}`;
178
+ const metadata = resolveObjectsRecursive({ node, nodePath, currentFilePath, resolvedImports });
179
+ context.builder.resolvedImports = resolvedImports;
180
+ return metadata;
181
+ }
182
+ return {};
183
+ };
184
+ exports.resolveMetadata = resolveMetadata;
@@ -1,9 +1,7 @@
1
1
  import * as babel from '@babel/core';
2
2
  import { Context, ParseMitosisOptions } from './types';
3
- declare const types: typeof babel.types;
4
3
  export declare const handleImportDeclaration: ({ options, path, context, }: {
5
4
  options: Partial<ParseMitosisOptions>;
6
5
  path: babel.NodePath<babel.types.ImportDeclaration>;
7
6
  context: Context;
8
7
  }) => void;
9
- export {};
@@ -1,33 +1,9 @@
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
- };
25
2
  Object.defineProperty(exports, "__esModule", { value: true });
26
3
  exports.handleImportDeclaration = void 0;
27
- const babel = __importStar(require("@babel/core"));
28
4
  const mitosis_imports_1 = require("../../helpers/mitosis-imports");
29
- const { types } = babel;
30
5
  const handleImportDeclaration = ({ options, path, context, }) => {
6
+ var _a;
31
7
  // @builder.io/mitosis or React imports compile away
32
8
  const customPackages = (options === null || options === void 0 ? void 0 : options.compileAwayPackages) || [];
33
9
  if (['react', '@builder.io/mitosis', '@emotion/react', ...customPackages].includes(path.node.source.value)) {
@@ -35,7 +11,13 @@ const handleImportDeclaration = ({ options, path, context, }) => {
35
11
  return;
36
12
  }
37
13
  const importObject = (0, mitosis_imports_1.mapImportDeclarationToMitosisImport)(path.node);
38
- context.builder.component.imports.push(importObject);
14
+ const resolvedImport = (_a = context.builder.resolvedImports) === null || _a === void 0 ? void 0 : _a.find((rImport) => rImport.path === importObject.path);
15
+ if (resolvedImport) {
16
+ delete importObject.imports[resolvedImport.value];
17
+ }
18
+ if (Object.keys(importObject.imports).length > 0) {
19
+ context.builder.component.imports.push(importObject);
20
+ }
39
21
  path.remove();
40
22
  };
41
23
  exports.handleImportDeclaration = handleImportDeclaration;
@@ -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
  };
@@ -7,7 +7,7 @@ type SveltosisStateValue = StateValue & {
7
7
  export declare function preventNameCollissions(json: SveltosisComponent, item: SveltosisStateValue): {
8
8
  code: string;
9
9
  typeParameter?: string | undefined;
10
- type: "function" | "getter" | "method" | "property";
10
+ type: "function" | "method" | "property" | "getter";
11
11
  propertyType?: import("../../../types/mitosis-component").ReactivityType | undefined;
12
12
  arguments?: string[] | undefined;
13
13
  };
@@ -1,4 +1,4 @@
1
- import { type Plugin } from '..';
1
+ import { type MitosisPlugin } from '..';
2
2
  import { TraverseContext } from 'neotraverse/legacy';
3
3
  import { MitosisComponent } from '../types/mitosis-component';
4
4
  import { MitosisNode } from '../types/mitosis-node';
@@ -11,5 +11,5 @@ type CompileAwayBuilderComponentsOptions = {
11
11
  omit?: string[];
12
12
  };
13
13
  export declare const compileAwayBuilderComponentsFromTree: (tree: MitosisNode | MitosisComponent, components: CompileAwayComponentsMap) => void;
14
- export declare const compileAwayBuilderComponents: (pluginOptions?: CompileAwayBuilderComponentsOptions) => Plugin;
14
+ export declare const compileAwayBuilderComponents: (pluginOptions?: CompileAwayBuilderComponentsOptions) => MitosisPlugin;
15
15
  export {};
@@ -1,7 +1,7 @@
1
- import type { ParseMitosisOptions } from '../parsers/jsx/types';
1
+ import type { ParseMitosisOptions } from '../parsers/jsx';
2
2
  import { targets } from '../targets';
3
3
  import type { MitosisComponent } from './mitosis-component';
4
- import { BaseTranspilerOptions } from './transpiler';
4
+ import { BaseTranspilerOptions, TranspilerGenerator } from './transpiler';
5
5
  export type Format = 'esm' | 'cjs';
6
6
  export type Language = 'js' | 'ts';
7
7
  interface TranspilerOptions {
@@ -17,6 +17,15 @@ export type GeneratorOptions = {
17
17
  export type generatorsOption = {
18
18
  [K in Target]: NonNullable<Targets[K]>;
19
19
  };
20
+ export interface TargetContext {
21
+ target: Target;
22
+ generator: TranspilerGenerator<NonNullable<MitosisConfig['options'][Target]>>;
23
+ outputPath: string;
24
+ }
25
+ export interface OutputFiles {
26
+ outputDir: string;
27
+ outputFilePath: string;
28
+ }
20
29
  export type MitosisConfig = {
21
30
  generators?: generatorsOption;
22
31
  /**
@@ -136,6 +136,15 @@ export type MitosisComponent = {
136
136
  propsTypeRef?: string;
137
137
  defaultProps?: MitosisState;
138
138
  style?: string;
139
+ /**
140
+ * This data is filled inside cli to provide more data for plugins
141
+ */
142
+ pluginData?: {
143
+ target?: Target;
144
+ path?: string;
145
+ outputDir?: string;
146
+ outputFilePath?: string;
147
+ };
139
148
  /**
140
149
  * Used to store context of a component for a specific framework
141
150
  * that we need access only during compilation (for internal use only) and gets removed after compilation.
@@ -1,11 +1,19 @@
1
+ import { OutputFiles, TargetContext } from '../types/config';
1
2
  import { MitosisComponent } from './mitosis-component';
2
- export type Plugin = (options?: any) => {
3
- json?: {
4
- pre?: (json: MitosisComponent) => MitosisComponent | void;
5
- post?: (json: MitosisComponent) => MitosisComponent | void;
6
- };
7
- code?: {
8
- pre?: (code: string, json: MitosisComponent) => string;
9
- post?: (code: string, json: MitosisComponent) => string;
10
- };
3
+ export type MitosisBuildPlugin = (targetContext: TargetContext, files?: {
4
+ componentFiles: OutputFiles[];
5
+ nonComponentFiles: OutputFiles[];
6
+ }) => void | Promise<void>;
7
+ export type MitosisJsonPlugin = (json: MitosisComponent) => MitosisComponent | void;
8
+ export type MitosisCodePlugin = (code: string, json: MitosisComponent) => string;
9
+ export type MitosisHook<T> = {
10
+ pre?: T;
11
+ post?: T;
12
+ };
13
+ export type MitosisPlugin = (options?: any) => {
14
+ name?: string;
15
+ order?: number;
16
+ build?: MitosisHook<MitosisBuildPlugin>;
17
+ json?: MitosisHook<MitosisJsonPlugin>;
18
+ code?: MitosisHook<MitosisCodePlugin>;
11
19
  };
@@ -1,5 +1,5 @@
1
1
  import { MitosisComponent } from './mitosis-component';
2
- import { Plugin } from './plugins';
2
+ import { MitosisPlugin } from './plugins';
3
3
  export interface TranspilerArgs {
4
4
  path?: string;
5
5
  component: MitosisComponent;
@@ -20,7 +20,7 @@ export interface BaseTranspilerOptions {
20
20
  /**
21
21
  * Mitosis Plugins to run during codegen.
22
22
  */
23
- plugins?: Plugin[];
23
+ plugins?: MitosisPlugin[];
24
24
  /**
25
25
  * Enable `typescript` output
26
26
  */
@@ -29,4 +29,14 @@ export interface BaseTranspilerOptions {
29
29
  * Preserves explicit filename extensions in import statements.
30
30
  */
31
31
  explicitImportFileExtension?: boolean;
32
+ /**
33
+ * Can be used for cli builds. Preserves explicit filename extensions when regex matches, e.g.:
34
+ * {
35
+ * explicitBuildFileExtension: {
36
+ * ".ts":/*.figma.lite.tsx/g,
37
+ * ".md":/*.docs.lite.tsx/g
38
+ * }
39
+ * }
40
+ */
41
+ explicitBuildFileExtensions?: Record<string, RegExp>;
32
42
  }
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.23",
25
+ "version": "0.5.25",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {