(reactOptions = {})
| 175 | |
| 176 | export 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, |
no test coverage detected