Module: ReactOnRails::Generators::RscSetup

Includes:
DemoPageConfig, ClientReferences, Layouts
Included in:
InstallGenerator, RscGenerator
Defined in:
lib/generators/react_on_rails/rsc_setup.rb,
lib/generators/react_on_rails/rsc_setup/layouts.rb,
lib/generators/react_on_rails/rsc_setup/client_references.rb

Overview

Provides RSC (React Server Components) setup functionality for React on Rails generators.

This module extracts RSC-specific setup methods that can be shared between:

  • InstallGenerator (when --rsc flag is used)
  • RscGenerator (standalone generator for upgrading existing Pro apps)

Required Dependencies

Including classes must provide (typically via Rails::Generators::Base):

  • destination_root: Path to the target Rails application
  • template, copy_file, append_to_file, empty_directory, route: Thor file manipulation methods
  • options: Generator options hash (for options.typescript?)

Including classes must also include GeneratorHelper which provides:

  • use_rsc?: Feature flag helper
  • component_extension: Returns 'jsx' or 'tsx' based on TypeScript option
  • detect_react_version: Detects installed React version

Defined Under Namespace

Modules: ClientReferences, Layouts

Constant Summary collapse

DEFAULT_LAYOUT_NAME =
"react_on_rails_default"
LEGACY_LAYOUT_NAME =
"hello_world"
RSC_FALLBACK_LAYOUT_NAME =
"react_on_rails_rsc"
RSC_GENERATED_LAYOUT_NAME_PATTERN =
/\Areact_on_rails_rsc(?:_(?:[2-9]|[1-9]\d+))?\z/
RSC_REACT_VERSION_RANGE =
ReactOnRails::Generators::JsDependencyManager::RSC_REACT_VERSION_RANGE
RSC_MINIMUM_REACT_VERSION =
RSC_REACT_VERSION_RANGE.sub(/\A[~^]/, "")
RSC_MINIMUM_REACT_VERSION_TUPLE =
RSC_MINIMUM_REACT_VERSION.split(".").map(&:to_i).freeze
RSC_SUPPORTED_REACT_MAJOR =
RSC_MINIMUM_REACT_VERSION_TUPLE.fetch(0)
RSC_SUPPORTED_REACT_MINOR =
RSC_MINIMUM_REACT_VERSION_TUPLE.fetch(1)
RSC_MINIMUM_REACT_PATCH =
RSC_MINIMUM_REACT_VERSION_TUPLE.fetch(2)
RSC_SUPPORTED_REACT_LINE =
"#{RSC_SUPPORTED_REACT_MAJOR}.#{RSC_SUPPORTED_REACT_MINOR}.x".freeze
MAX_LAYOUT_NAME_ATTEMPTS =
99

Constants included from ClientReferences

ClientReferences::JS_COMMENT_SECOND_CHARS, ClientReferences::JS_COMMENT_STATES, ClientReferences::JS_STRING_DELIMITERS, ClientReferences::REGEX_LITERAL_PRECEDERS, ClientReferences::RSC_PLUGIN_INVOCATION_REGEX

Instance Method Summary collapse

Methods included from DemoPageConfig

#build_hello_server_view_config, #build_hello_world_view_config

Instance Method Details

#setup_rscObject

Note:

NPM dependencies are handled separately by JsDependencyManager

Main entry point for RSC setup. Orchestrates creation of all RSC-related files and configuration.

Creates:

  • config/webpack/rscWebpackConfig.js (config/rspack/ when using rspack)
  • Procfile.dev entry for RSC bundle watcher
  • HelloServer component (jsx or tsx based on --typescript flag)
  • HelloServerController
  • HelloServer view
  • RSC routes


58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/generators/react_on_rails/rsc_setup.rb', line 58

def setup_rsc
  print_rsc_setup_banner

  add_rsc_config_to_pro_initializer
  create_rsc_webpack_config
  update_webpack_configs_for_rsc
  add_rsc_to_procfile
  create_hello_server_component
  create_hello_server_controller
  create_hello_server_view
  add_rsc_routes

  print_rsc_complete_banner
end

#warn_about_react_version_for_rsc(force: false) ⇒ Object

Warn if React version is not compatible with RSC. RSC requires the generated React support line with the configured minimum patch.

Parameters:

  • force (Boolean) (defaults to: false)

    When true, always performs the check. When false (default), only checks if RSC is enabled (use_rsc? returns true). Use force: true in standalone generators where RSC is always the purpose.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/generators/react_on_rails/rsc_setup.rb', line 79

def warn_about_react_version_for_rsc(force: false)
  return unless force || use_rsc?

  react_version = detect_react_version
  return if react_version.nil? # React not installed yet, will be installed by generator

  major, minor, patch = react_version.split(".").map(&:to_i)

  if major != RSC_SUPPORTED_REACT_MAJOR || minor != RSC_SUPPORTED_REACT_MINOR
    GeneratorMessages.add_warning(<<~MSG.strip)
      ⚠️  RSC requires React #{RSC_SUPPORTED_REACT_LINE} (detected: #{react_version})

      React Server Components in React on Rails Pro currently only supports
      React #{RSC_SUPPORTED_REACT_LINE} with patch >= #{RSC_MINIMUM_REACT_VERSION}. Other React minor versions are
      not yet supported.

      To install a compatible React version:
        #{manual_add_packages_command(["react@#{RSC_REACT_VERSION_RANGE}", "react-dom@#{RSC_REACT_VERSION_RANGE}"])}
    MSG
  elsif patch < RSC_MINIMUM_REACT_PATCH
    GeneratorMessages.add_warning(<<~MSG.strip)
      ⚠️  React #{react_version} is below the recommended minimum for RSC.

      Please upgrade to at least React #{RSC_MINIMUM_REACT_VERSION}:
        #{manual_add_packages_command(["react@#{RSC_MINIMUM_REACT_VERSION}", "react-dom@#{RSC_MINIMUM_REACT_VERSION}"])}

      react-on-rails-rsc 19.2.x with patch >= 19.2.1 is coordinated with
      React/React DOM #{RSC_MINIMUM_REACT_VERSION}+ for the React on Rails Pro 17 RSC runtime.
    MSG
  end
end