Home / Function/ compileProgram() — react Function Reference

compileProgram() — react Function Reference

Architecture documentation for the compileProgram() function in Program.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  1c9af54b_10e9_8985_e772_1f517e46c560["compileProgram()"]
  9aa4477d_960b_1ea1_b6d9_36076aaa70bd["Program.ts"]
  1c9af54b_10e9_8985_e772_1f517e46c560 -->|defined in| 9aa4477d_960b_1ea1_b6d9_36076aaa70bd
  9e49688f_6b43_84fe_0dd2_18e5142c1f71["shouldSkipCompilation()"]
  1c9af54b_10e9_8985_e772_1f517e46c560 -->|calls| 9e49688f_6b43_84fe_0dd2_18e5142c1f71
  67757659_3cd9_ac9a_4dd3_4c5ae8672636["validateRestrictedImports()"]
  1c9af54b_10e9_8985_e772_1f517e46c560 -->|calls| 67757659_3cd9_ac9a_4dd3_4c5ae8672636
  4f591349_3d90_e4e5_51a6_a3b7ad077584["handleError()"]
  1c9af54b_10e9_8985_e772_1f517e46c560 -->|calls| 4f591349_3d90_e4e5_51a6_a3b7ad077584
  6a20a35f_9061_eb10_3e52_2cce36820b84["findProgramSuppressions()"]
  1c9af54b_10e9_8985_e772_1f517e46c560 -->|calls| 6a20a35f_9061_eb10_3e52_2cce36820b84
  bfe4a215_80e5_3bbc_bd03_14596ae8b8ab["findDirectiveDisablingMemoization()"]
  1c9af54b_10e9_8985_e772_1f517e46c560 -->|calls| bfe4a215_80e5_3bbc_bd03_14596ae8b8ab
  7fe1be87_de24_09ca_229a_0644af0874a5["findFunctionsToCompile()"]
  1c9af54b_10e9_8985_e772_1f517e46c560 -->|calls| 7fe1be87_de24_09ca_229a_0644af0874a5
  db0bec13_2016_3880_d2b9_adcd797cdf2c["processFn()"]
  1c9af54b_10e9_8985_e772_1f517e46c560 -->|calls| db0bec13_2016_3880_d2b9_adcd797cdf2c
  041ca752_10c1_3cda_1f5c_02f44a01310e["invariant()"]
  1c9af54b_10e9_8985_e772_1f517e46c560 -->|calls| 041ca752_10c1_3cda_1f5c_02f44a01310e
  ade5f434_a213_5611_b4fa_0fdd254474e1["insertNewOutlinedFunctionNode()"]
  1c9af54b_10e9_8985_e772_1f517e46c560 -->|calls| ade5f434_a213_5611_b4fa_0fdd254474e1
  073c81a5_c389_d108_5b8f_4d6dc6eece83["push()"]
  1c9af54b_10e9_8985_e772_1f517e46c560 -->|calls| 073c81a5_c389_d108_5b8f_4d6dc6eece83
  825ba705_282a_7031_34fa_49b26f1c9bed["pushErrorDetail()"]
  1c9af54b_10e9_8985_e772_1f517e46c560 -->|calls| 825ba705_282a_7031_34fa_49b26f1c9bed
  11c511ca_2971_a23f_cfd6_2897f2fe1b13["applyCompiledFunctions()"]
  1c9af54b_10e9_8985_e772_1f517e46c560 -->|calls| 11c511ca_2971_a23f_cfd6_2897f2fe1b13
  style 1c9af54b_10e9_8985_e772_1f517e46c560 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts lines 364–492

export function compileProgram(
  program: NodePath<t.Program>,
  pass: CompilerPass,
): CompileProgramMetadata | null {
  /**
   * This is directly invoked by the react-compiler babel plugin, so exceptions
   * thrown by this function will fail the babel build.
   * - call `handleError` if your error is recoverable.
   *   Unless the error is a warning / info diagnostic, compilation of a function
   *   / entire file should also be skipped.
   * - throw an exception if the error is fatal / not recoverable.
   *   Examples of this are invalid compiler configs or failure to codegen outlined
   *   functions *after* already emitting optimized components / hooks that invoke
   *   the outlined functions.
   */
  if (shouldSkipCompilation(program, pass)) {
    return null;
  }
  const restrictedImportsErr = validateRestrictedImports(
    program,
    pass.opts.environment,
  );
  if (restrictedImportsErr) {
    handleError(restrictedImportsErr, pass, null);
    return null;
  }
  /*
   * Record lint errors and critical errors as depending on Forget's config,
   * we may still need to run Forget's analysis on every function (even if we
   * have already encountered errors) for reporting.
   */
  const suppressions = findProgramSuppressions(
    pass.comments,
    /*
     * If the compiler is validating hooks rules and exhaustive memo dependencies, we don't need to check
     * for React ESLint suppressions
     */
    pass.opts.environment.validateExhaustiveMemoizationDependencies &&
      pass.opts.environment.validateHooksUsage
      ? null
      : (pass.opts.eslintSuppressionRules ?? DEFAULT_ESLINT_SUPPRESSIONS),
    // Always bail on Flow suppressions
    pass.opts.flowSuppressions,
  );

  const programContext = new ProgramContext({
    program: program,
    opts: pass.opts,
    filename: pass.filename,
    code: pass.code,
    suppressions,
    hasModuleScopeOptOut:
      findDirectiveDisablingMemoization(program.node.directives, pass.opts) !=
      null,
  });

  const queue: Array<CompileSource> = findFunctionsToCompile(
    program,
    pass,
    programContext,
  );
  const compiledFns: Array<CompileResult> = [];

  // outputMode takes precedence if specified
  const outputMode: CompilerOutputMode =
    pass.opts.outputMode ?? (pass.opts.noEmit ? 'lint' : 'client');
  while (queue.length !== 0) {
    const current = queue.shift()!;
    const compiled = processFn(
      current.fn,
      current.fnType,
      programContext,
      outputMode,
    );

    if (compiled != null) {
      for (const outlined of compiled.outlined) {
        CompilerError.invariant(outlined.fn.outlined.length === 0, {
          reason: 'Unexpected nested outlined functions',
          loc: outlined.fn.loc,
        });

Domain

Subdomains

Frequently Asked Questions

What does compileProgram() do?
compileProgram() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts.
Where is compileProgram() defined?
compileProgram() is defined in compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts at line 364.
What does compileProgram() call?
compileProgram() calls 12 function(s): applyCompiledFunctions, findDirectiveDisablingMemoization, findFunctionsToCompile, findProgramSuppressions, handleError, insertNewOutlinedFunctionNode, invariant, processFn, and 4 more.

Analyze Your Own Codebase

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

Try Supermodel Free