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

Function componentToMitosis

packages/core/src/generators/mitosis/generator.ts:306–427  ·  view source on GitHub ↗
(toMitosisOptions = {})

Source from the content-addressed store, hash-verified

304
305export const componentToMitosis: TranspilerGenerator<Partial<ToMitosisOptions>> =
306 (toMitosisOptions = {}) =>
307 ({ component }) => {
308 const options: ToMitosisOptions = {
309 format: DEFAULT_FORMAT,
310 ...toMitosisOptions,
311 };
312
313 if (options.format === 'react') {
314 return componentToReact({
315 format: 'lite',
316 stateType: 'useState',
317 stylesType: 'emotion',
318 prettier: options.prettier,
319 })({ component });
320 }
321
322 let json = fastClone(component);
323
324 if (options.plugins) {
325 json = runPreJsonPlugins({ json, plugins: options.plugins });
326 }
327
328 const domRefs = getRefs(component);
329 // grab refs not used for bindings
330 const jsRefs = Object.keys(component.refs).filter((ref) => domRefs.has(ref));
331
332 const refs = [...jsRefs, ...Array.from(domRefs)];
333
334 mapRefs(json, (refName) => {
335 return `${refName}${domRefs.has(refName) ? `.current` : ''}`;
336 });
337
338 const addWrapper = json.children.length !== 1 || isRootTextNode(json);
339
340 const components = Array.from(getComponents(json));
341 const mitosisCoreComponents: string[] = [];
342 if (!options.nativeConditionals) {
343 mitosisCoreComponents.push('Show');
344 }
345 if (!options.nativeLoops) {
346 mitosisCoreComponents.push('For');
347 }
348
349 const mitosisComponents = components.filter((item) => mitosisCoreComponents.includes(item));
350 const otherComponents = components.filter((item) => !mitosisCoreComponents.includes(item));
351
352 if (options.plugins) {
353 json = runPostJsonPlugins({ json, plugins: options.plugins });
354 }
355
356 const hasState = checkHasState(component);
357
358 const needsMitosisCoreImport = Boolean(hasState || refs.length || mitosisComponents.length);
359
360 const stringifiedUseMetadata = json5.stringify(component.meta.useMetadata);
361
362 // TODO: smart only pull in imports as needed
363 let str = dedent`

Callers 12

builder.test.tsFile · 0.90
mitosis.test.tsFile · 0.90
symbols.test.tsFile · 0.90
text.test.tsFile · 0.90
builder.test.tsFile · 0.90
generateCodeFunction · 0.85
getOutputGeneratorFunction · 0.85

Calls 15

componentToReactFunction · 0.90
fastCloneFunction · 0.90
runPreJsonPluginsFunction · 0.90
getRefsFunction · 0.90
mapRefsFunction · 0.90
isRootTextNodeFunction · 0.90
getComponentsFunction · 0.90
runPostJsonPluginsFunction · 0.90
checkHasStateFunction · 0.90
dedentFunction · 0.90
renderPreComponentFunction · 0.90

Tested by

no test coverage detected