Module: ReactOnRails::Generators::JsDependencyManager
- Included in:
- BaseGenerator, InstallGenerator
- 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
Optional Methods
Including classes may define:
-
options.rspack?: Returns true if –rspack flag is set (for Rspack support)
-
options.typescript?: Returns true if –typescript flag is set (for TypeScript support)
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