Home / File/ OptimizeForSSR.ts — react Source File

OptimizeForSSR.ts — react Source File

Architecture documentation for OptimizeForSSR.ts, a typescript file in the react codebase. 7 imports, 1 dependents.

File typescript BabelCompiler Optimization 7 imports 1 dependents 3 functions

Entity Profile

Dependency Diagram

graph LR
  1acbc29a_0c93_81f5_ff8e_d4434aae5ed9["OptimizeForSSR.ts"]
  0423f759_97e0_9101_4634_ed555abc5ca9["index.ts"]
  1acbc29a_0c93_81f5_ff8e_d4434aae5ed9 --> 0423f759_97e0_9101_4634_ed555abc5ca9
  2f3caf55_cc64_415c_55dd_9771ba7dc210["visitors.ts"]
  1acbc29a_0c93_81f5_ff8e_d4434aae5ed9 --> 2f3caf55_cc64_415c_55dd_9771ba7dc210
  b2fc2985_a7ba_9865_c2a3_2a7531f27d44["eachInstructionValueOperand"]
  1acbc29a_0c93_81f5_ff8e_d4434aae5ed9 --> b2fc2985_a7ba_9865_c2a3_2a7531f27d44
  41232a25_deb6_6e83_05a8_ae9f961656f7["eachTerminalOperand"]
  1acbc29a_0c93_81f5_ff8e_d4434aae5ed9 --> 41232a25_deb6_6e83_05a8_ae9f961656f7
  eb9d33f9_42c1_205c_93e6_8e1365a31839["utils.ts"]
  1acbc29a_0c93_81f5_ff8e_d4434aae5ed9 --> eb9d33f9_42c1_205c_93e6_8e1365a31839
  c447b97e_0b8e_b187_e3a8_4be412d6f495["retainWhere"]
  1acbc29a_0c93_81f5_ff8e_d4434aae5ed9 --> c447b97e_0b8e_b187_e3a8_4be412d6f495
  2ed45bcd_6c82_3ccd_0e20_fa96b5111055[".."]
  1acbc29a_0c93_81f5_ff8e_d4434aae5ed9 --> 2ed45bcd_6c82_3ccd_0e20_fa96b5111055
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b["Pipeline.ts"]
  e3cfc07a_10c8_5dcd_e270_e8e14c29309b --> 1acbc29a_0c93_81f5_ff8e_d4434aae5ed9
  style 1acbc29a_0c93_81f5_ff8e_d4434aae5ed9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

import {CompilerError} from '..';
import {
  CallExpression,
  getHookKind,
  HIRFunction,
  IdentifierId,
  InstructionValue,
  isArrayType,
  isPlainObjectType,
  isPrimitiveType,
  isSetStateType,
  isStartTransitionType,
  LoadLocal,
  StoreLocal,
} from '../HIR';
import {
  eachInstructionValueOperand,
  eachTerminalOperand,
} from '../HIR/visitors';
import {retainWhere} from '../Utils/utils';

/**
 * Optimizes the code for running specifically in an SSR environment. This optimization
 * asssumes that setState will not be called during render during initial mount, which
 * allows inlining useState/useReducer.
 *
 * Optimizations:
 * - Inline useState/useReducer
 * - Remove effects
 * - Remove refs where known to be unused during render (eg directly passed to a dom node)
 * - Remove event handlers
 *
 * Note that an earlier pass already inlines useMemo/useCallback
 */
export function optimizeForSSR(fn: HIRFunction): void {
  const inlinedState = new Map<IdentifierId, InstructionValue>();
  /**
   * First pass identifies useState/useReducer which can be safely inlined. Any use
   * of the hook return other than destructuring (with a specific pattern) prevents
   * inlining.
   *
   * Supported cases:
   * - `const [state, ] = useState( <primitive-array-or-object> )`
   * - `const [state, ] = useReducer(..., <value>)`
   * - `const [state, ] = useReducer[..., <value>, <init>]`
   */
  for (const block of fn.body.blocks.values()) {
    for (const instr of block.instructions) {
      const {value} = instr;
      switch (value.kind) {
        case 'Destructure': {
          if (
            inlinedState.has(value.value.identifier.id) &&
// ... (204 more lines)

Domain

Subdomains

Frequently Asked Questions

What does OptimizeForSSR.ts do?
OptimizeForSSR.ts is a source file in the react codebase, written in typescript. It belongs to the BabelCompiler domain, Optimization subdomain.
What functions are defined in OptimizeForSSR.ts?
OptimizeForSSR.ts defines 3 function(s): hasKnownNonRenderCall, isKnownEventHandler, optimizeForSSR.
What does OptimizeForSSR.ts depend on?
OptimizeForSSR.ts imports 7 module(s): .., eachInstructionValueOperand, eachTerminalOperand, index.ts, retainWhere, utils.ts, visitors.ts.
What files import OptimizeForSSR.ts?
OptimizeForSSR.ts is imported by 1 file(s): Pipeline.ts.
Where is OptimizeForSSR.ts in the architecture?
OptimizeForSSR.ts is located at compiler/packages/babel-plugin-react-compiler/src/Optimization/OptimizeForSSR.ts (domain: BabelCompiler, subdomain: Optimization, directory: compiler/packages/babel-plugin-react-compiler/src/Optimization).

Analyze Your Own Codebase

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

Try Supermodel Free