Skip to content

Commit 4b59220

Browse files
committed
[compiler] add fire imports
Summary: Adds import {useFire} from 'react' when fire syntax is used. This is experimentation and may not become a stable feature in the compiler. --
1 parent 03297e0 commit 4b59220

File tree

9 files changed

+19
-1
lines changed

9 files changed

+19
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,11 @@ export function compileProgram(
564564
if (environment.enableChangeDetectionForDebugging != null) {
565565
externalFunctions.push(environment.enableChangeDetectionForDebugging);
566566
}
567+
568+
const hasFireRewrite = compiledFns.some(c => c.compiledFn.hasFireRewrite);
569+
if (environment.enableFire && hasFireRewrite) {
570+
externalFunctions.push({source: 'react', importSpecifierName: 'useFire'});
571+
}
567572
} catch (err) {
568573
handleError(err, pass, null);
569574
return;

compiler/packages/babel-plugin-react-compiler/src/HIR/Environment.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ export class Environment {
787787
fnType: ReactFunctionType;
788788
useMemoCacheIdentifier: string;
789789
hasLoweredContextAccess: boolean;
790+
hasFireRewrite: boolean;
790791

791792
#contextIdentifiers: Set<t.Identifier>;
792793
#hoistedIdentifiers: Set<t.Identifier>;
@@ -811,6 +812,7 @@ export class Environment {
811812
this.#shapes = new Map(DEFAULT_SHAPES);
812813
this.#globals = new Map(DEFAULT_GLOBALS);
813814
this.hasLoweredContextAccess = false;
815+
this.hasFireRewrite = false;
814816

815817
if (
816818
config.disableMemoizationForDebugging &&

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ export type CodegenFunction = {
103103
* This is true if the compiler has the lowered useContext calls.
104104
*/
105105
hasLoweredContextAccess: boolean;
106+
107+
/**
108+
* This is true if the compiler has compiled a fire to a useFire call
109+
*/
110+
hasFireRewrite: boolean;
106111
};
107112

108113
export function codegenFunction(
@@ -355,6 +360,7 @@ function codegenReactiveFunction(
355360
prunedMemoValues: countMemoBlockVisitor.prunedMemoValues,
356361
outlined: [],
357362
hasLoweredContextAccess: fn.env.hasLoweredContextAccess,
363+
hasFireRewrite: fn.env.hasFireRewrite,
358364
});
359365
}
360366

compiler/packages/babel-plugin-react-compiler/src/Transform/TransformFire.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {BuiltInFireId, DefaultNonmutatingHook} from '../HIR/ObjectShape';
3232
/*
3333
* TODO(jmbrown):
3434
* In this stack:
35-
* - Insert useFire import
3635
* - Assert no lingering fire calls
3736
* - Ensure a fired function is not called regularly elsewhere in the same effect
3837
*
@@ -226,6 +225,7 @@ function replaceFireFunctions(fn: HIRFunction, context: Context): void {
226225

227226
if (rewriteInstrs.size > 0 || deleteInstrs.size > 0) {
228227
hasRewrite = true;
228+
fn.env.hasFireRewrite = true;
229229
}
230230
}
231231

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/basic.expect.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function Component(props) {
2121
## Code
2222

2323
```javascript
24+
import { useFire } from "react";
2425
import { c as _c } from "react/compiler-runtime"; // @enableFire
2526
import { fire } from "react";
2627

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/deep-scope.expect.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ function Component(props) {
3030
## Code
3131

3232
```javascript
33+
import { useFire } from "react";
3334
import { c as _c } from "react/compiler-runtime"; // @enableFire
3435
import { fire } from "react";
3536

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/multiple-scope.expect.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function Component(props) {
2929
## Code
3030

3131
```javascript
32+
import { useFire } from "react";
3233
import { c as _c } from "react/compiler-runtime"; // @enableFire
3334
import { fire } from "react";
3435

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/repeated-calls.expect.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function Component(props) {
2222
## Code
2323

2424
```javascript
25+
import { useFire } from "react";
2526
import { c as _c } from "react/compiler-runtime"; // @enableFire
2627
import { fire } from "react";
2728

compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/transform-fire/shared-hook-calls.expect.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function Component({bar, baz}) {
2626
## Code
2727

2828
```javascript
29+
import { useFire } from "react";
2930
import { c as _c } from "react/compiler-runtime"; // @enableFire
3031
import { fire } from "react";
3132

0 commit comments

Comments
 (0)