| 46 | * @returns A JSON representation of the Mitosis component |
| 47 | */ |
| 48 | export function parseJsx( |
| 49 | jsx: string, |
| 50 | _options: Partial<ParseMitosisOptions> = {}, |
| 51 | ): MitosisComponent { |
| 52 | let subComponentFunctions: string[] = []; |
| 53 | |
| 54 | const options: ParseMitosisOptions = { |
| 55 | typescript: false, |
| 56 | ..._options, |
| 57 | }; |
| 58 | |
| 59 | const stateToScope: string[] = []; |
| 60 | |
| 61 | const jsxToUse = babelStripTypes(jsx, !options.typescript); |
| 62 | |
| 63 | const output = babelDefaultTransform(jsxToUse, { |
| 64 | JSXExpressionContainer(path, context) { |
| 65 | if (types.isJSXEmptyExpression(path.node.expression)) { |
| 66 | path.remove(); |
| 67 | } |
| 68 | }, |
| 69 | Program(path, context) { |
| 70 | if (context.builder) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | beforeParse(path); |
| 75 | |
| 76 | context.builder = { |
| 77 | component: createMitosisComponent(), |
| 78 | }; |
| 79 | |
| 80 | const keepStatements = path.node.body.filter( |
| 81 | (statement) => isImportOrDefaultExport(statement) || isTypeOrInterface(statement), |
| 82 | ); |
| 83 | |
| 84 | context.builder.component.exports = generateExports(path); |
| 85 | |
| 86 | subComponentFunctions = path.node.body |
| 87 | .filter( |
| 88 | (node) => !types.isExportDefaultDeclaration(node) && types.isFunctionDeclaration(node), |
| 89 | ) |
| 90 | .map((node) => `export default ${generate(node).code!}`); |
| 91 | |
| 92 | const preComponentCode = pipe( |
| 93 | path, |
| 94 | collectModuleScopeHooks(context, options), |
| 95 | types.program, |
| 96 | generate, |
| 97 | (generatorResult) => generatorResult.code, |
| 98 | ); |
| 99 | |
| 100 | // TODO: support multiple? e.g. for others to add imports? |
| 101 | context.builder.component.hooks.preComponent = { code: preComponentCode }; |
| 102 | |
| 103 | path.replaceWith(types.program(keepStatements)); |
| 104 | }, |
| 105 | FunctionDeclaration(path, context) { |