Skip to content
Merged
Prev Previous commit
Next Next commit
Apply react_on_rails:install generator updates
Ran the react_on_rails:install generator to get latest v16.1.1 improvements: - Enhanced bin/dev script with better development server management - Added Procfile variations for different development modes: - Procfile.dev: HMR mode with webpack-dev-server - Procfile.dev-static-assets: Watch mode without HMR - Procfile.dev-prod-assets: Development with production assets - Updated webpack configurations for v16.1.1 compatibility - Updated shakapacker.yml configuration - Updated babel.config.js - Updated react_on_rails initializer - Added generateWebpackConfigs.js helper These updates provide better development experience and align with React on Rails v16.1.1 best practices. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
  • Loading branch information
justin808 and claude committed Sep 28, 2025
commit d35babea4a6644bcbe5687a40fc51a4c750a20df
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,7 @@ lib/bs
/lib/ocaml

client/app/bundles/comments/rescript/**/*.bs.js

# Generated React on Rails packs
**/generated/**
ssr-generated
2 changes: 2 additions & 0 deletions Procfile.dev-prod-assets
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Procfile for development with production assets
# Uses production-optimized, precompiled assets with development environment
# You can run these commands in separate shells
web: bin/rails s -p 3001
redis: redis-server
Expand Down
38 changes: 21 additions & 17 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
// The source code including full typescript support is available at:
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/babel.config.js

module.exports = function (api) {
const defaultConfigFunc = require('shakapacker/package/babel/preset.js');
const resultConfig = defaultConfigFunc(api);
const isProductionEnv = api.env('production');
const defaultConfigFunc = require('shakapacker/package/babel/preset.js')
const resultConfig = defaultConfigFunc(api)
const isProductionEnv = api.env('production')

const changesOnDefault = {
presets: [
[
'@babel/preset-react',
{
runtime: 'automatic',
development: !isProductionEnv,
useBuiltIns: true,
},
],
runtime: 'automatic'
}
]
].filter(Boolean),
plugins: [
process.env.WEBPACK_SERVE && 'react-refresh/babel',
isProductionEnv && [
'babel-plugin-transform-react-remove-prop-types',
// Enable React Refresh (Fast Refresh) only when webpack-dev-server is running (HMR mode)
// This prevents React Refresh from trying to connect when using static compilation
!isProductionEnv && process.env.WEBPACK_SERVE && 'react-refresh/babel',
isProductionEnv && ['babel-plugin-transform-react-remove-prop-types',
{
removeImport: true,
},
],
removeImport: true
}
]
].filter(Boolean),
};
}

resultConfig.presets = [...resultConfig.presets, ...changesOnDefault.presets];
resultConfig.plugins = [...resultConfig.plugins, ...changesOnDefault.plugins];
resultConfig.presets = [...resultConfig.presets, ...changesOnDefault.presets]
resultConfig.plugins = [...resultConfig.plugins, ...changesOnDefault.plugins ]

return resultConfig;
};
return resultConfig
}
12 changes: 3 additions & 9 deletions bin/dev
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#
# Each command uses a specific Procfile for process management:
# - bin/dev (default/hmr): Uses Procfile.dev
# - bin/dev static: Uses Procfile.dev-static-assets-assets
# - bin/dev static: Uses Procfile.dev-static-assets
# - bin/dev prod: Uses Procfile.dev-prod-assets
#
# To customize development environment:
Expand All @@ -18,14 +18,8 @@
# 3. Extend ReactOnRails::Dev classes in your Rails app for advanced customization
# 4. Use classes directly: ReactOnRails::Dev::ServerManager.start(:development, "Custom.procfile")

begin
require "bundler/setup"
require "react_on_rails/dev"
rescue LoadError
# Fallback for when gem is not yet installed
puts "Loading ReactOnRails development tools..."
require_relative "../../lib/react_on_rails/dev"
end
require "bundler/setup"
require "react_on_rails/dev"

# Main execution
ReactOnRails::Dev::ServerManager.run_from_command_line(ARGV)
113 changes: 48 additions & 65 deletions config/initializers/react_on_rails.rb
Original file line number Diff line number Diff line change
@@ -1,84 +1,67 @@
# frozen_string_literal: true

# Shown below are the defaults for configuration
ReactOnRails.configure do |config|
# Define the files for we need to check for webpack compilation when running tests
config.webpack_generated_files = %w[client-bundle.js server-bundle.js]
# See https://github.com/shakacode/react_on_rails/blob/master/docs/guides/configuration.md
# for many more options.

config.build_test_command = "RAILS_ENV=test bin/shakapacker"
config.build_production_command = "RAILS_ENV=production NODE_ENV=production bin/shakapacker"
ReactOnRails.configure do |config|
# This configures the script to run to build the production assets by webpack. Set this to nil
# if you don't want react_on_rails building this file for you.
# If nil, then the standard shakacode/shakapacker assets:precompile will run
# config.build_production_command = nil

# This is the file used for server rendering of React when using `(prerender: true)`
# If you are never using server rendering, you may set this to "".
# If you are using the same file for client and server rendering, having this set probably does
# not affect performance.
config.server_bundle_js_file = "server-bundle.js"
################################################################################
################################################################################
# TEST CONFIGURATION OPTIONS
# Below options are used with the use of this test helper:
# ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
################################################################################

# React on Rails 16 compatibility: Workaround for removed error handling
# If you are using this in your spec_helper.rb (or rails_helper.rb):
#
# ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
#
# BREAKING CHANGE in v16: React on Rails 14.2.1 had robust error handling that would
# fallback to the Shakapacker output path when bundle lookup failed. This was removed
# in v16.0.1.rc.2, causing it to look in the wrong directory during tests.
# with rspec then this controls what npm command is run
# to automatically refresh your webpack assets on every test run.
#
# This configuration tells React on Rails where to find bundles in test environment.
# Without this, it defaults to public/webpack/test/ instead of public/packs/
config.generated_assets_dir = Rails.public_path.join("packs").to_s if Rails.env.test?
# Alternately, you can remove the `ReactOnRails::TestHelper.configure_rspec_to_compile_assets`
# and set the config/shakapacker.yml option for test to true.
config.build_test_command = "RAILS_ENV=test bin/shakapacker"

################################################################################
# CLIENT RENDERING OPTIONS
# Below options can be overriden by passing options to the react_on_rails
# `render_component` view helper method.
################################################################################

# Default is false. Can be overriden at the component level.
# Set to false for debugging issues before turning on to true.
config.prerender = true

# default is true for development, off otherwise
config.trace = Rails.env.development?

################################################################################
# SERVER RENDERING OPTIONS
# Applicable options can be overriden by passing options to the react_on_rails
# `render_component` view helper method.
################################################################################
# This is the file used for server rendering of React when using `(prerender: true)`
# If you are never using server rendering, you should set this to "".
# Note, there is only one server bundle, unlike JavaScript where you want to minimize the size
# of the JS sent to the client. For the server rendering, React on Rails creates a pool of
# JavaScript execution instances which should handle any component requested.
#
# While you may configure this to be the same as your client bundle file, this file is typically
# different. You should have ONE server bundle which can create all of your server rendered
# React components.
#
config.server_bundle_js_file = "server-bundle.js"

# If set to true, this forces Rails to reload the server bundle if it is modified
config.development_mode = Rails.env.development?

# For server rendering. This can be set to false so that server side messages are discarded.
# Default is true. Be cautious about turning this off.
config.replay_console = true

# Default is true. Logs server rendering messages to Rails.logger.info
config.logging_on_server = true

# Change to true to raise exception on server if the JS code throws. Let's do this only if not
# in production, as the JS code might still work on the client and we don't want to blow up the
# whole Rails page.
config.raise_on_prerender_error = !Rails.env.production?
# Configure where server bundles are output. Defaults to "ssr-generated".
# This should match your webpack configuration for server bundles.
config.server_bundle_output_path = "ssr-generated"

# Server rendering only (not for render_component helper)
# You can configure your pool of JS virtual machines and specify where it should load code:
# On MRI, use `therubyracer` for the best performance
# (see [discussion](https://github.com/reactjs/react-rails/pull/290))
# On MRI, you'll get a deadlock with `pool_size` > 1
# If you're using JRuby, you can increase `pool_size` to have real multi-threaded rendering.
config.server_renderer_pool_size = 1 # increase if you're on JRuby
config.server_renderer_timeout = 20 # seconds
# Enforce that server bundles are only loaded from private (non-public) directories.
# When true, server bundles will only be loaded from the configured server_bundle_output_path.
# This is recommended for production to prevent server-side code from being exposed.
config.enforce_private_server_bundles = true

################################################################################
# I18N OPTIONS
################################################################################
# Replace the following line to the location where you keep translation.js & default.js.
config.i18n_dir = Rails.root.join("client/app/libs/i18n")

################################################################################
# MISCELLANEOUS OPTIONS
# FILE SYSTEM BASED COMPONENT REGISTRY
################################################################################

# This allows you to add additional values to the Rails Context. Implement one static method
# called `custom_context(view_context)` and return a Hash.
config.rendering_extension = nil
config.i18n_output_format = "js"
# `components_subdirectory` is the name of the matching directories that contain automatically registered components
# for use in the Rails views. The default is nil, you can enable the feature by updating it in the next line.
config.components_subdirectory = "ror_components"
#
# For automated component registry, `render_component` view helper method tries to load bundle for component from
# generated directory. default is false, you can pass option at the time of individual usage or update the default
# in the following line
config.auto_load_bundle = true
end
91 changes: 80 additions & 11 deletions config/shakapacker.yml
Original file line number Diff line number Diff line change
@@ -1,46 +1,109 @@
# Note: You must restart bin/shakapacker-dev-server for changes to take effect
# This file contains the defaults used by shakapacker.

default: &default
source_path: client/app
source_path: app/javascript

# You can have a subdirectory of the source_path, like 'packs' (recommended).
# Alternatively, you can use '/' to use the whole source_path directory.
# Notice that this is a relative path to source_path
source_entry_path: packs

# If nested_entries is true, then we'll pick up subdirectories within the source_entry_path.
# You cannot set this option to true if you set source_entry_path to '/'
nested_entries: true

# While using a File-System-based automated bundle generation feature, miscellaneous warnings suggesting css order
# conflicts may arise due to the mini-css-extract-plugin. For projects where css ordering has been mitigated through
# consistent use of scoping or naming conventions, the css order warnings can be disabled by setting
# css_extract_ignore_order_warnings to true
css_extract_ignore_order_warnings: false

public_root_path: public
public_output_path: packs
cache_path: tmp/shakapacker
webpack_compile_output: true
# See https://github.com/shakacode/shakapacker#deployment
shakapacker_precompile: true

# Additional paths webpack should lookup modules
# Location for manifest.json, defaults to {public_output_path}/manifest.json if unset
# manifest_path: public/packs/manifest.json

# Additional paths webpack should look up modules
# ['app/assets', 'engine/foo/app/assets']
additional_paths: []

# Reload manifest.json on all requests so we reload latest compiled packs
cache_manifest: false

# Use the config.build_production_command in config/initializers/react_on_rails.rb
shakapacker_precompile: false

# Select loader to use, available options are 'babel' (default), 'swc' or 'esbuild'
webpack_loader: 'babel'

# Raises an error if there is a mismatch in the shakapacker gem and npm package being used
ensure_consistent_versioning: true

# Select whether the compiler will use SHA digest ('digest' option) or most recent modified timestamp ('mtime') to determine freshness
compiler_strategy: digest

# Select whether the compiler will always use a content hash and not just in production
# Don't use contentHash except for production for performance
# https://webpack.js.org/guides/build-performance/#avoid-production-specific-tooling
useContentHash: false

# Setting the asset host here will override Rails.application.config.asset_host.
# Here, you can set different asset_host per environment. Note that
# SHAKAPACKER_ASSET_HOST will override both configurations.
# asset_host: custom-path

# Utilizing webpack-subresource-integrity plugin, will generate integrity hashes for all entries in manifest.json
# https://github.com/waysact/webpack-subresource-integrity/tree/main/webpack-subresource-integrity
integrity:
enabled: false
# Which cryptographic function(s) to use, for generating the integrity hash(es). Default sha-384. Other possible values sha256, sha512
hash_functions: ["sha384"]
# Default "anonymous". Other possible value "use-credentials"
# https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity#cross-origin_resource_sharing_and_subresource_integrity
cross_origin: "anonymous"

development:
<<: *default
# This is false since we're running `bin/shakapacker -w` in Procfile.dev-setic
compile: false
compile: true
compiler_strategy: mtime

# Reference: https://webpack.js.org/configuration/dev-server/
# Keys not described there are documented inline and in https://github.com/shakacode/shakapacker/
dev_server:
https: false
# For running dev server with https, set `server: https`.
# server: https

host: localhost
port: 3035
# Hot Module Replacement updates modules while the application is running without a full reload
# Used instead of the `hot` key in https://webpack.js.org/configuration/dev-server/#devserverhot
hmr: true
# If HMR is on, CSS will be inlined by delivering it as part of the script payload via style-loader. Be sure
# that you add style-loader to your project dependencies.
#
# If you want to instead deliver CSS via <link> with the mini-css-extract-plugin, set inline_css to false.
# In that case, style-loader is not needed as a dependency.
#
# mini-css-extract-plugin is a required dependency in both cases.
inline_css: true
# Defaults to the inverse of hmr. Uncomment to manually set this.
# live_reload: true
client:
# Should we show a full-screen overlay in the browser when there are compiler errors or warnings?
overlay: true
# May also be a string
# webSocketURL:
# hostname: "0.0.0.0"
# pathname: "/ws"
# hostname: '0.0.0.0'
# pathname: '/ws'
# port: 8080
# Should we use gzip compression?
compress: true
# Note that apps that do not check the host are vulnerable to DNS rebinding attacks
allowed_hosts: [ 'localhost' ]
allowed_hosts: 'auto'
# Shows progress and colorizes output of bin/shakapacker[-dev-server]
pretty: true
headers:
'Access-Control-Allow-Origin': '*'
Expand All @@ -52,11 +115,17 @@ test:
<<: *default
compile: true

# Compile test packs to a separate directory
public_output_path: packs-test

production:
<<: *default

# Production depends on precompilation of packs prior to booting for performance.
compile: false

# Use content hash for naming assets. Cannot be overridden in production.
useContentHash: true

# Cache manifest.json for performance
cache_manifest: true
13 changes: 2 additions & 11 deletions config/webpack/clientWebpackConfig.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
// The source code including full typescript support is available at:
// https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/config/webpack/clientWebpackConfig.js
// The source code including full typescript support is available at:
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/config/webpack/clientWebpackConfig.js

const webpack = require('webpack');
const commonWebpackConfig = require('./commonWebpackConfig');

const configureClient = () => {
const clientConfig = commonWebpackConfig();

clientConfig.plugins.push(
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
ActionCable: '@rails/actioncable',
}),
);

// server-bundle is special and should ONLY be built by the serverConfig
// In case this entry is not deleted, a very strange "window" not found
// error shows referring to window["webpackJsonp"]. That is because the
Expand Down
Loading