Home / Function/ codegenFunction() — react Function Reference

codegenFunction() — react Function Reference

Architecture documentation for the codegenFunction() function in CodegenReactiveFunction.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  33e8ddc7_2075_1da1_7e05_66d1f4c34a3c["codegenFunction()"]
  dc7f10c2_c914_a162_d02b_a10a15c64a5f["CodegenReactiveFunction.ts"]
  33e8ddc7_2075_1da1_7e05_66d1f4c34a3c -->|defined in| dc7f10c2_c914_a162_d02b_a10a15c64a5f
  cc29904c_66f3_5155_9ed7_837866d52047["codegenReactiveFunction()"]
  33e8ddc7_2075_1da1_7e05_66d1f4c34a3c -->|calls| cc29904c_66f3_5155_9ed7_837866d52047
  d0ed8568_2850_9a95_ed1a_3c9e915252da["createHookGuard()"]
  33e8ddc7_2075_1da1_7e05_66d1f4c34a3c -->|calls| d0ed8568_2850_9a95_ed1a_3c9e915252da
  073c81a5_c389_d108_5b8f_4d6dc6eece83["push()"]
  33e8ddc7_2075_1da1_7e05_66d1f4c34a3c -->|calls| 073c81a5_c389_d108_5b8f_4d6dc6eece83
  a599ee29_a03a_9f21_bb90_a7ef23973fc6["synthesizeName()"]
  33e8ddc7_2075_1da1_7e05_66d1f4c34a3c -->|calls| a599ee29_a03a_9f21_bb90_a7ef23973fc6
  041ca752_10c1_3cda_1f5c_02f44a01310e["invariant()"]
  33e8ddc7_2075_1da1_7e05_66d1f4c34a3c -->|calls| 041ca752_10c1_3cda_1f5c_02f44a01310e
  3418bcf4_aa1c_21c8_528f_3022fdc9b6a5["getOutlinedFunctions()"]
  33e8ddc7_2075_1da1_7e05_66d1f4c34a3c -->|calls| 3418bcf4_aa1c_21c8_528f_3022fdc9b6a5
  9d06bb9d_27c1_2f15_be7c_0fe428122d2d["buildReactiveFunction()"]
  33e8ddc7_2075_1da1_7e05_66d1f4c34a3c -->|calls| 9d06bb9d_27c1_2f15_be7c_0fe428122d2d
  8f45c458_198e_ea00_a21c_eae224736051["isErr()"]
  33e8ddc7_2075_1da1_7e05_66d1f4c34a3c -->|calls| 8f45c458_198e_ea00_a21c_eae224736051
  5228ee8c_dc7c_d859_3f41_614fd9c00374["unwrap()"]
  33e8ddc7_2075_1da1_7e05_66d1f4c34a3c -->|calls| 5228ee8c_dc7c_d859_3f41_614fd9c00374
  style 33e8ddc7_2075_1da1_7e05_66d1f4c34a3c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts lines 116–345

export function codegenFunction(
  fn: ReactiveFunction,
  {
    uniqueIdentifiers,
    fbtOperands,
  }: {
    uniqueIdentifiers: Set<string>;
    fbtOperands: Set<IdentifierId>;
  },
): Result<CodegenFunction, CompilerError> {
  const cx = new Context(
    fn.env,
    fn.id ?? '[[ anonymous ]]',
    uniqueIdentifiers,
    fbtOperands,
    null,
  );

  /**
   * Fast Refresh reuses component instances at runtime even as the source of the component changes.
   * The generated code needs to prevent values from one version of the code being reused after a code cange.
   * If HMR detection is enabled and we know the source code of the component, assign a cache slot to track
   * the source hash, and later, emit code to check for source changes and reset the cache on source changes.
   */
  let fastRefreshState: {
    cacheIndex: number;
    hash: string;
  } | null = null;
  if (
    fn.env.config.enableResetCacheOnSourceFileChanges &&
    fn.env.code !== null
  ) {
    const hash = createHmac('sha256', fn.env.code).digest('hex');
    fastRefreshState = {
      cacheIndex: cx.nextCacheIndex,
      hash,
    };
  }

  const compileResult = codegenReactiveFunction(cx, fn);
  if (compileResult.isErr()) {
    return compileResult;
  }
  const compiled = compileResult.unwrap();

  const hookGuard = fn.env.config.enableEmitHookGuards;
  if (hookGuard != null && fn.env.outputMode === 'client') {
    compiled.body = t.blockStatement([
      createHookGuard(
        hookGuard,
        fn.env.programContext,
        compiled.body.body,
        GuardKind.PushHookGuard,
        GuardKind.PopHookGuard,
      ),
    ]);
  }

  const cacheCount = compiled.memoSlotsUsed;
  if (cacheCount !== 0) {
    const preface: Array<t.Statement> = [];
    const useMemoCacheIdentifier =
      fn.env.programContext.addMemoCacheImport().name;

    // The import declaration for `useMemoCache` is inserted in the Babel plugin
    preface.push(
      t.variableDeclaration('const', [
        t.variableDeclarator(
          t.identifier(cx.synthesizeName('$')),
          t.callExpression(t.identifier(useMemoCacheIdentifier), [
            t.numericLiteral(cacheCount),
          ]),
        ),
      ]),
    );
    if (fastRefreshState !== null) {
      // HMR detection is enabled, emit code to reset the memo cache on source changes
      const index = cx.synthesizeName('$i');
      preface.push(
        t.ifStatement(
          t.binaryExpression(

Domain

Subdomains

Frequently Asked Questions

What does codegenFunction() do?
codegenFunction() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts.
Where is codegenFunction() defined?
codegenFunction() is defined in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts at line 116.
What does codegenFunction() call?
codegenFunction() calls 9 function(s): buildReactiveFunction, codegenReactiveFunction, createHookGuard, getOutlinedFunctions, invariant, isErr, push, synthesizeName, and 1 more.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free