(options: ToBuilderOptions = {})
| 933 | |
| 934 | export const componentToBuilder = |
| 935 | (options: ToBuilderOptions = {}) => |
| 936 | ({ component }: TranspilerArgs): BuilderContent => { |
| 937 | const hasState = checkHasState(component); |
| 938 | |
| 939 | if (!options.stateMap) { |
| 940 | options.stateMap = new Map<string, string>(); |
| 941 | } |
| 942 | |
| 943 | const result: BuilderContent = fastClone({ |
| 944 | data: { |
| 945 | httpRequests: component?.meta?.useMetadata?.httpRequests, |
| 946 | jsCode: tryFormat(dedent` |
| 947 | ${!hasProps(component) ? '' : `var props = state;`} |
| 948 | |
| 949 | ${!hasState ? '' : `Object.assign(state, ${getStateObjectStringFromComponent(component)});`} |
| 950 | |
| 951 | ${stringifySingleScopeOnMount(component)} |
| 952 | `), |
| 953 | tsCode: tryFormat(dedent` |
| 954 | ${!hasProps(component) ? '' : `var props = state;`} |
| 955 | |
| 956 | ${!hasState ? '' : `useStore(${getStateObjectStringFromComponent(component)});`} |
| 957 | |
| 958 | ${ |
| 959 | !component.hooks.onMount.length |
| 960 | ? '' |
| 961 | : `onMount(() => { |
| 962 | ${stringifySingleScopeOnMount(component)} |
| 963 | })` |
| 964 | } |
| 965 | `), |
| 966 | cssCode: component?.style, |
| 967 | ...(() => { |
| 968 | const stateData = findStateWithinMitosisComponent( |
| 969 | component, |
| 970 | options, |
| 971 | { ...convertMitosisStateToBuilderState(component.state) }, |
| 972 | options.stateMap, |
| 973 | ); |
| 974 | return { state: stateData }; |
| 975 | })(), |
| 976 | blocks: component.children |
| 977 | .filter(filterEmptyTextNodes) |
| 978 | .map((child) => blockToBuilder(child, options)), |
| 979 | }, |
| 980 | }); |
| 981 | |
| 982 | if (result.data?.state && Object.keys(result.data.state).length === 0) { |
| 983 | delete result.data.state; |
| 984 | } |
| 985 | |
| 986 | const subComponentMap: Record<string, BuilderContent> = {}; |
| 987 | |
| 988 | for (const subComponent of component.subComponents) { |
| 989 | const name = subComponent.name; |
| 990 | subComponentMap[name] = componentToBuilder(options)({ |
| 991 | component: subComponent, |
| 992 | }); |
no test coverage detected