MCPcopy
hub / github.com/BuilderIO/mitosis / componentToBuilder

Function componentToBuilder

packages/core/src/generators/builder/generator.ts:935–1048  ·  view source on GitHub ↗
(options: ToBuilderOptions = {})

Source from the content-addressed store, hash-verified

933
934export 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 });

Callers 11

builder.test.tsFile · 0.90
symbols.test.tsFile · 0.90
events.test.tsFile · 0.90
text.test.tsFile · 0.90
builder.test.tsFile · 0.90
updateOutputFunction · 0.85
generateCodeFunction · 0.85
getOutputGeneratorFunction · 0.85

Calls 15

checkHasStateFunction · 0.90
fastCloneFunction · 0.90
dedentFunction · 0.90
hasPropsFunction · 0.90
traverseFunction · 0.90
isBuilderElementFunction · 0.90
blockToBuilderFunction · 0.85

Tested by

no test coverage detected