Module: ReactOnRails::Generators::JsDependencyManager

Included in:
BaseGenerator, InstallGenerator, ProGenerator, RscGenerator
Defined in:
lib/generators/react_on_rails/js_dependency_manager.rb

Overview

Shared module for managing JavaScript dependencies across generators This module provides common functionality for adding and installing JS dependencies to avoid code duplication between generators.

Since react_on_rails requires shakapacker, and shakapacker includes package_json as a dependency, the package_json gem is always available.

Required Methods

Including classes must include GeneratorHelper module which provides:

  • add_npm_dependencies(packages, dev: false): Add packages via package_json gem

  • package_json: Access to PackageJson instance (always available via shakapacker)

  • destination_root: Generator destination directory

  • using_rspack?: Returns true if rspack is the configured bundler (called unconditionally; provided by GeneratorHelper)

  • using_swc?: Returns true if SWC is the configured transpiler (called unconditionally; provided by GeneratorHelper)

Optional Methods

Including classes may define:

  • use_pro?: Returns true if React on Rails Pro should be used

  • use_rsc?: Returns true if React Server Components should be used

Installation Behavior

The module ALWAYS runs package manager install after adding dependencies. This is safe because package_json gem’s install method is idempotent - it only installs what’s actually needed from package.json. This prevents edge cases where package.json was modified but dependencies weren’t installed.

Error Handling Philosophy

All dependency addition methods use a graceful degradation approach:

  • Methods return false on failure instead of raising exceptions

  • StandardError is caught at the lowest level (add_package) and higher levels (add_*_dependencies)

  • Failures trigger user-facing warnings via GeneratorMessages

  • Warnings provide clear manual installation instructions

This ensures the generator ALWAYS completes successfully, even when:

  • Network connectivity issues prevent package downloads

  • Package manager (npm/yarn/pnpm) has permission errors

  • package_json gem encounters unexpected states

Users can manually run package installation commands after generator completion. This is preferable to generator crashes that leave Rails apps in incomplete states.

Usage

Include this module in generator classes and call setup_js_dependencies to handle all JS dependency installation via package_json gem.

Constant Summary collapse

REACT_DEPENDENCIES =

Core React dependencies required for React on Rails Note: @babel/preset-react and babel plugins are NOT included here because:

  • Shakapacker handles JavaScript transpiler configuration (babel, swc, or esbuild)

  • Users configure their preferred transpiler via shakapacker.yml javascript_transpiler setting

  • SWC is now the default and doesn’t need Babel presets

  • For Babel users, shakapacker will install babel-loader and its dependencies

%w[
  react
  react-dom
  prop-types
].freeze
CSS_DEPENDENCIES =

CSS processing dependencies for webpack

%w[
  css-loader
  css-minimizer-webpack-plugin
  mini-css-extract-plugin
  style-loader
].freeze
DEV_DEPENDENCIES =

Development-only dependencies for hot reloading (Webpack)

%w[
  @pmmmwh/react-refresh-webpack-plugin
  react-refresh
].freeze
RSPACK_DEPENDENCIES =

Rspack core dependencies (only installed when –rspack flag is used)

%w[
  @rspack/core
  rspack-manifest-plugin
].freeze
RSPACK_DEV_DEPENDENCIES =

Rspack development dependencies for hot reloading

%w[
  @rspack/cli
  @rspack/plugin-react-refresh
  react-refresh
].freeze
TYPESCRIPT_DEPENDENCIES =

TypeScript dependencies (only installed when –typescript flag is used) Note: @babel/preset-typescript is NOT included because:

  • SWC is now the default javascript_transpiler (has built-in TypeScript support)

  • Shakapacker handles the transpiler configuration via shakapacker.yml

  • If users choose javascript_transpiler: ‘babel’, they should manually add @babel/preset-typescript and configure it in their babel.config.js

%w[
  typescript
  @types/react
  @types/react-dom
].freeze
SWC_DEPENDENCIES =

SWC transpiler dependencies (for Shakapacker 9.3.0+ default transpiler) SWC is ~20x faster than Babel and is the default for new Shakapacker installations

%w[
  @swc/core
  swc-loader
].freeze
PRO_DEPENDENCIES =

React on Rails Pro dependencies (only installed when –pro or –rsc flag is used) These packages are published publicly on npmjs.org but require a license for production use

%w[
  react-on-rails-pro
  react-on-rails-pro-node-renderer
].freeze
RSC_DEPENDENCIES =

React Server Components dependencies (only installed when –rsc flag is used) Requires React 19.0.x - see react.dev/reference/rsc/server-components

%w[
  react-on-rails-rsc
].freeze