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

Function componentToReact

packages/core/src/generators/react/generator.ts:177–274  ·  view source on GitHub ↗
(reactOptions = {})

Source from the content-addressed store, hash-verified

175
176export const componentToReact: TranspilerGenerator<Partial<ToReactOptions>> =
177 (reactOptions = {}) =>
178 ({ component, path }) => {
179 let json = fastClone(component);
180
181 const target = reactOptions.preact
182 ? 'preact'
183 : reactOptions.type === 'native'
184 ? 'reactNative'
185 : reactOptions.type === 'taro'
186 ? 'taro'
187 : reactOptions.rsc
188 ? 'rsc'
189 : 'react';
190
191 const stateType = reactOptions.stateType || 'useState';
192
193 const DEFAULT_OPTIONS: ToReactOptions = {
194 addUseClientDirectiveIfNeeded: true,
195 stateType,
196 stylesType: 'styled-jsx',
197 styleTagsPlacement: 'bottom',
198 type: 'dom',
199 plugins: [
200 processOnEventHooksPlugin({ setBindings: false }),
201 ...(stateType === 'variables'
202 ? [
203 CODE_PROCESSOR_PLUGIN((codeType, json) => (code, hookType) => {
204 if (codeType === 'types') return code;
205
206 code = replaceNodes({
207 code,
208 nodeMaps: Object.entries(json.state)
209 .filter(([key, value]) => value?.type === 'getter')
210 .map(([key, value]) => {
211 const expr = types.memberExpression(
212 types.identifier('state'),
213 types.identifier(key),
214 );
215 return {
216 from: expr,
217 // condition: (path) => !types.isObjectMethod(path.parent),
218 to: types.callExpression(expr, []),
219 };
220 }),
221 });
222
223 code = replaceStateIdentifier(null)(code);
224
225 return code;
226 }),
227 ]
228 : []),
229 ],
230 };
231
232 const options = initializeOptions({
233 target,
234 component,

Callers 13

componentToMitosisFunction · 0.90
componentToTaroFunction · 0.90
componentToReactNativeFunction · 0.90
componentToRscFunction · 0.90
react.test.tsFile · 0.90
context.test.tsFile · 0.90
builder.test.tsFile · 0.90
componentToPreactFunction · 0.85
updateOutputFunction · 0.85
generateCodeFunction · 0.85

Calls 10

fastCloneFunction · 0.90
replaceNodesFunction · 0.90
replaceStateIdentifierFunction · 0.90
initializeOptionsFunction · 0.90
runPreJsonPluginsFunction · 0.90
runPreCodePluginsFunction · 0.90
runPostCodePluginsFunction · 0.90
_componentToReactFunction · 0.85
formatFunction · 0.85

Tested by

no test coverage detected