(options: ContextToReactOptions = { typescript: false, preact: false })
| 10 | |
| 11 | export const contextToReact = |
| 12 | (options: ContextToReactOptions = { typescript: false, preact: false }) => |
| 13 | ({ context }: { context: MitosisContext }): string => { |
| 14 | let str = ` |
| 15 | import { createContext } from '${options.preact ? 'preact' : 'react'}'; |
| 16 | |
| 17 | export default createContext${options.typescript ? '<any>' : ''}(${stringifyContextValue( |
| 18 | context.value, |
| 19 | )}) |
| 20 | `; |
| 21 | |
| 22 | if (options.format !== false) { |
| 23 | try { |
| 24 | str = format(str, { |
| 25 | parser: 'typescript', |
| 26 | plugins: [ |
| 27 | require('prettier/parser-typescript'), // To support running in browsers |
| 28 | ], |
| 29 | }); |
| 30 | } catch (err) { |
| 31 | if (process.env.NODE_ENV !== 'test') { |
| 32 | console.error('Format error for file:', str); |
| 33 | } |
| 34 | throw err; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return str; |
| 39 | }; |
no test coverage detected