(toMitosisOptions = {})
| 304 | |
| 305 | export 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` |
no test coverage detected