| 30 | options: PluginOptions | null; |
| 31 | }; |
| 32 | export async function compile({ |
| 33 | text, |
| 34 | file, |
| 35 | options, |
| 36 | }: CompileOptions): Promise<BabelCore.BabelFileResult> { |
| 37 | const ast = await parseAsync(text, { |
| 38 | sourceFileName: file, |
| 39 | parserOpts: { |
| 40 | plugins: ['typescript', 'jsx'], |
| 41 | }, |
| 42 | sourceType: 'module', |
| 43 | }); |
| 44 | if (ast == null) { |
| 45 | throw new Error('Could not parse'); |
| 46 | } |
| 47 | const plugins = |
| 48 | options != null |
| 49 | ? [[BabelPluginReactCompiler, options]] |
| 50 | : [[BabelPluginReactCompiler]]; |
| 51 | const result = await transformFromAstAsync(ast, text, { |
| 52 | filename: file, |
| 53 | highlightCode: false, |
| 54 | retainLines: true, |
| 55 | plugins, |
| 56 | sourceType: 'module', |
| 57 | sourceFileName: file, |
| 58 | }); |
| 59 | if (result?.code == null) { |
| 60 | throw new Error( |
| 61 | `Expected BabelPluginReactCompiler to compile successfully, got ${result}`, |
| 62 | ); |
| 63 | } |
| 64 | try { |
| 65 | result.code = await prettier.format(result.code, { |
| 66 | semi: false, |
| 67 | parser: 'babel-ts', |
| 68 | }); |
| 69 | if (result.code != null) { |
| 70 | lastResult = result; |
| 71 | } |
| 72 | } catch (err) { |
| 73 | // If prettier failed just log, no need to crash |
| 74 | console.error(err); |
| 75 | } |
| 76 | return result; |
| 77 | } |