Skip to content

Commit 43ff487

Browse files
committed
[compiler] Option to only compile component syntax
Summary: Projects which have heavily adopted Flow component syntax may wish to enable the compiler only for components and hooks that use the syntax, rather than trying to guess which functions are components and hooks. This provides that option. ghstack-source-id: d957cc7 Pull Request resolved: #29864
1 parent 0c65538 commit 43ff487

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ const CompilationModeSchema = z.enum([
131131
* This is the default mode
132132
*/
133133
"infer",
134+
// Compile only components using Flow component syntax and hooks using hook syntax.
135+
"syntax",
134136
// Compile only functions which are explicitly annotated with "use forget"
135137
"annotation",
136138
// Compile all top-level functions

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -499,23 +499,28 @@ function getReactFunctionType(
499499
return getComponentOrHookLike(fn, hookPattern) ?? "Other";
500500
}
501501
}
502+
503+
// Component and hook declarations are known components/hooks
504+
let componentSyntaxType: ReactFunctionType | null = null;
505+
if (fn.isFunctionDeclaration()) {
506+
if (isComponentDeclaration(fn.node)) {
507+
componentSyntaxType = "Component";
508+
} else if (isHookDeclaration(fn.node)) {
509+
componentSyntaxType = "Hook";
510+
}
511+
}
512+
502513
switch (pass.opts.compilationMode) {
503514
case "annotation": {
504515
// opt-ins are checked above
505516
return null;
506517
}
507518
case "infer": {
508-
// Component and hook declarations are known components/hooks
509-
if (fn.isFunctionDeclaration()) {
510-
if (isComponentDeclaration(fn.node)) {
511-
return "Component";
512-
} else if (isHookDeclaration(fn.node)) {
513-
return "Hook";
514-
}
515-
}
516-
517-
// Otherwise check if this is a component or hook-like function
518-
return getComponentOrHookLike(fn, hookPattern);
519+
// Check if this is a component or hook-like function
520+
return componentSyntaxType ?? getComponentOrHookLike(fn, hookPattern);
521+
}
522+
case "syntax": {
523+
return componentSyntaxType;
519524
}
520525
case "all": {
521526
// Compile only top level functions

0 commit comments

Comments
 (0)