Skip to content

Commit e678d8a

Browse files
committed
[compiler] Add support for canonical reactrc configs
This PR adds experimental support for a canoncial reactrc config file to be provided. This will be used later by other tooling such as a compiler upgrade script, IDE extension and so on, as the canonical configuration source for the compiler.
1 parent 4309bde commit e678d8a

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

compiler/packages/babel-plugin-react-compiler/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"babel-jest": "^29.0.3",
4343
"babel-plugin-fbt": "^1.0.0",
4444
"babel-plugin-fbt-runtime": "^1.0.0",
45+
"cosmiconfig": "^9.0.0",
4546
"eslint": "^8.57.1",
4647
"invariant": "^2.2.4",
4748
"jest": "^29.0.3",

compiler/packages/babel-plugin-react-compiler/src/Babel/BabelPlugin.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
*/
77

88
import type * as BabelCore from '@babel/core';
9-
import {compileProgram, parsePluginOptions} from '../Entrypoint';
9+
import {
10+
compileProgram,
11+
findReactConfig,
12+
parsePluginOptions,
13+
type PluginOptions,
14+
} from '../Entrypoint';
1015
import {
1116
injectReanimatedFlag,
1217
pipelineUsesReanimatedPlugin,
@@ -29,7 +34,18 @@ export default function BabelPluginReactCompiler(
2934
* want Forget to run true to source as possible.
3035
*/
3136
Program(prog, pass): void {
32-
let opts = parsePluginOptions(pass.opts);
37+
const reactConfig = findReactConfig();
38+
let opts: PluginOptions | null = null;
39+
if (reactConfig != null) {
40+
opts = parsePluginOptions(reactConfig.config);
41+
if (pass.opts != null) {
42+
console.warn(
43+
`Duplicate React Compiler config found, defaulting to reactrc found in: ${reactConfig.filepath}`,
44+
);
45+
}
46+
} else {
47+
opts = parsePluginOptions(pass.opts);
48+
}
3349
const isDev =
3450
(typeof __DEV__ !== 'undefined' && __DEV__ === true) ||
3551
process.env['NODE_ENV'] === 'development';

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import {hasOwnProperty} from '../Utils/utils';
1717
import {fromZodError} from 'zod-validation-error';
1818
import {CompilerPipelineValue} from './Pipeline';
19+
import {type CosmiconfigResult, cosmiconfigSync} from 'cosmiconfig';
1920

2021
const PanicThresholdOptionsSchema = z.enum([
2122
/*
@@ -286,3 +287,11 @@ export function parseTargetConfig(value: unknown): CompilerReactTarget {
286287
function isCompilerFlag(s: string): s is keyof PluginOptions {
287288
return hasOwnProperty(defaultOptions, s);
288289
}
290+
291+
export function findReactConfig(): CosmiconfigResult {
292+
const explorerSync = cosmiconfigSync('react', {
293+
searchStrategy: 'project',
294+
cache: true,
295+
});
296+
return explorerSync.search();
297+
}

compiler/yarn.lock

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3841,6 +3841,16 @@ core-js-compat@^3.30.1, core-js-compat@^3.30.2:
38413841
dependencies:
38423842
browserslist "^4.21.5"
38433843

3844+
cosmiconfig@^9.0.0:
3845+
version "9.0.0"
3846+
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-9.0.0.tgz#34c3fc58287b915f3ae905ab6dc3de258b55ad9d"
3847+
integrity sha512-itvL5h8RETACmOTFc4UfIyB2RfEHi71Ax6E/PivVxq9NseKbOWpeyHEOIbmAw1rs8Ak0VursQNww7lf7YtUwzg==
3848+
dependencies:
3849+
env-paths "^2.2.1"
3850+
import-fresh "^3.3.0"
3851+
js-yaml "^4.1.0"
3852+
parse-json "^5.2.0"
3853+
38443854
create-require@^1.1.0:
38453855
version "1.1.1"
38463856
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
@@ -4076,6 +4086,11 @@ entities@^4.4.0:
40764086
resolved "https://registry.yarnpkg.com/entities/-/entities-4.4.0.tgz#97bdaba170339446495e653cfd2db78962900174"
40774087
integrity sha512-oYp7156SP8LkeGD0GF85ad1X9Ai79WtRsZ2gxJqtBuzH+98YUV6jkHEKlZkMbcrjJjIVJNIDP/3WL9wQkoPbWA==
40784088

4089+
env-paths@^2.2.1:
4090+
version "2.2.1"
4091+
resolved "https://registry.yarnpkg.com/env-paths/-/env-paths-2.2.1.tgz#420399d416ce1fbe9bc0a07c62fa68d67fd0f8f2"
4092+
integrity sha512-+h1lkLKhZMTYjog1VEpJNG7NZJWcuc2DDk/qsqSTRRCOXiLjeQ1d1/udrUGhqMxUgAlwKNZ0cf2uqan5GLuS2A==
4093+
40794094
error-ex@^1.3.1:
40804095
version "1.3.2"
40814096
resolved "https://registry.yarnpkg.com/error-ex/-/error-ex-1.3.2.tgz#b4ac40648107fdcdcfae242f428bea8a14d4f1bf"
@@ -4758,7 +4773,7 @@ ignore@^5.3.1:
47584773
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.3.2.tgz#3cd40e729f3643fd87cb04e50bf0eb722bc596f5"
47594774
integrity sha512-hsBTNUqQTDwkWtcdYI2i06Y/nUBEsNEDJKjWdigLvegy8kDuJAS8uRlpkkcQpyEXL0Z/pjDy5HBmMjRCJ2gq+g==
47604775

4761-
import-fresh@^3.2.1:
4776+
import-fresh@^3.2.1, import-fresh@^3.3.0:
47624777
version "3.3.0"
47634778
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.0.tgz#37162c25fcb9ebaa2e6e53d5b4d88ce17d9e0c2b"
47644779
integrity sha512-veYYhQa+D1QBKznvhUHxb8faxlrwUnxseDAbAp457E0wLNio2bOSKnjYDhMj+YiAq61xrMGhQk9iXVk5FzgQMw==

0 commit comments

Comments
 (0)