main
ts 47 lines 1.34 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8 import type * as BabelCore from '@babel/core';
9 import {transformFromAstSync} from '@babel/core';
10 import * as BabelParser from '@babel/parser';
11 import invariant from 'invariant';
12 import type {PluginOptions} from '../Entrypoint';
13 import BabelPluginReactCompiler from './BabelPlugin';
14
15 export const DEFAULT_PLUGINS = ['babel-plugin-fbt', 'babel-plugin-fbt-runtime'];
16 export function runBabelPluginReactCompiler(
17 text: string,
18 file: string,
19 language: 'flow' | 'typescript',
20 options: PluginOptions | null,
21 includeAst: boolean = false,
22 ): BabelCore.BabelFileResult {
23 const ast = BabelParser.parse(text, {
24 sourceFilename: file,
25 plugins: [language, 'jsx'],
26 sourceType: 'module',
27 });
28 const result = transformFromAstSync(ast, text, {
29 ast: includeAst,
30 filename: file,
31 highlightCode: false,
32 retainLines: true,
33 plugins: [
34 [BabelPluginReactCompiler, options],
35 'babel-plugin-fbt',
36 'babel-plugin-fbt-runtime',
37 ],
38 sourceType: 'module',
39 configFile: false,
40 babelrc: false,
41 });
42 invariant(
43 result?.code != null,
44 `Expected BabelPluginReactForget to codegen successfully, got: ${result}`,
45 );
46 return result;
47 }