Skip to content

Commit 1bb4404

Browse files
justin808claude
andcommitted
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 <[email protected]>
1 parent e4752b6 commit 1bb4404

14 files changed

+231
-163
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,7 @@ lib/bs
5252
/lib/ocaml
5353

5454
client/app/bundles/comments/rescript/**/*.bs.js
55+
56+
# Generated React on Rails packs
57+
**/generated/**
58+
ssr-generated

Procfile.dev-prod-assets

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Procfile for development with production assets
2+
# Uses production-optimized, precompiled assets with development environment
13
# You can run these commands in separate shells
24
web: bin/rails s -p 3001
35
redis: redis-server

babel.config.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
1+
// The source code including full typescript support is available at:
2+
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/babel.config.js
3+
14
module.exports = function (api) {
2-
const defaultConfigFunc = require('shakapacker/package/babel/preset.js');
3-
const resultConfig = defaultConfigFunc(api);
4-
const isProductionEnv = api.env('production');
5+
const defaultConfigFunc = require('shakapacker/package/babel/preset.js')
6+
const resultConfig = defaultConfigFunc(api)
7+
const isProductionEnv = api.env('production')
58

69
const changesOnDefault = {
710
presets: [
811
[
912
'@babel/preset-react',
1013
{
11-
runtime: 'automatic',
1214
development: !isProductionEnv,
1315
useBuiltIns: true,
14-
},
15-
],
16+
runtime: 'automatic'
17+
}
18+
]
1619
].filter(Boolean),
1720
plugins: [
18-
process.env.WEBPACK_SERVE && 'react-refresh/babel',
19-
isProductionEnv && [
20-
'babel-plugin-transform-react-remove-prop-types',
21+
// Enable React Refresh (Fast Refresh) only when webpack-dev-server is running (HMR mode)
22+
// This prevents React Refresh from trying to connect when using static compilation
23+
!isProductionEnv && process.env.WEBPACK_SERVE && 'react-refresh/babel',
24+
isProductionEnv && ['babel-plugin-transform-react-remove-prop-types',
2125
{
22-
removeImport: true,
23-
},
24-
],
26+
removeImport: true
27+
}
28+
]
2529
].filter(Boolean),
26-
};
30+
}
2731

28-
resultConfig.presets = [...resultConfig.presets, ...changesOnDefault.presets];
29-
resultConfig.plugins = [...resultConfig.plugins, ...changesOnDefault.plugins];
32+
resultConfig.presets = [...resultConfig.presets, ...changesOnDefault.presets]
33+
resultConfig.plugins = [...resultConfig.plugins, ...changesOnDefault.plugins ]
3034

31-
return resultConfig;
32-
};
35+
return resultConfig
36+
}

bin/dev

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#
1010
# Each command uses a specific Procfile for process management:
1111
# - bin/dev (default/hmr): Uses Procfile.dev
12-
# - bin/dev static: Uses Procfile.dev-static-assets-assets
12+
# - bin/dev static: Uses Procfile.dev-static-assets
1313
# - bin/dev prod: Uses Procfile.dev-prod-assets
1414
#
1515
# To customize development environment:
@@ -18,14 +18,8 @@
1818
# 3. Extend ReactOnRails::Dev classes in your Rails app for advanced customization
1919
# 4. Use classes directly: ReactOnRails::Dev::ServerManager.start(:development, "Custom.procfile")
2020

21-
begin
22-
require "bundler/setup"
23-
require "react_on_rails/dev"
24-
rescue LoadError
25-
# Fallback for when gem is not yet installed
26-
puts "Loading ReactOnRails development tools..."
27-
require_relative "../../lib/react_on_rails/dev"
28-
end
21+
require "bundler/setup"
22+
require "react_on_rails/dev"
2923

3024
# Main execution
3125
ReactOnRails::Dev::ServerManager.run_from_command_line(ARGV)
Lines changed: 48 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,67 @@
11
# frozen_string_literal: true
22

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

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

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

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

2730
################################################################################
28-
# CLIENT RENDERING OPTIONS
29-
# Below options can be overriden by passing options to the react_on_rails
30-
# `render_component` view helper method.
31-
################################################################################
32-
33-
# Default is false. Can be overriden at the component level.
34-
# Set to false for debugging issues before turning on to true.
35-
config.prerender = true
36-
37-
# default is true for development, off otherwise
38-
config.trace = Rails.env.development?
39-
4031
################################################################################
4132
# SERVER RENDERING OPTIONS
42-
# Applicable options can be overriden by passing options to the react_on_rails
43-
# `render_component` view helper method.
4433
################################################################################
34+
# This is the file used for server rendering of React when using `(prerender: true)`
35+
# If you are never using server rendering, you should set this to "".
36+
# Note, there is only one server bundle, unlike JavaScript where you want to minimize the size
37+
# of the JS sent to the client. For the server rendering, React on Rails creates a pool of
38+
# JavaScript execution instances which should handle any component requested.
39+
#
40+
# While you may configure this to be the same as your client bundle file, this file is typically
41+
# different. You should have ONE server bundle which can create all of your server rendered
42+
# React components.
43+
#
44+
config.server_bundle_js_file = "server-bundle.js"
4545

46-
# If set to true, this forces Rails to reload the server bundle if it is modified
47-
config.development_mode = Rails.env.development?
48-
49-
# For server rendering. This can be set to false so that server side messages are discarded.
50-
# Default is true. Be cautious about turning this off.
51-
config.replay_console = true
52-
53-
# Default is true. Logs server rendering messages to Rails.logger.info
54-
config.logging_on_server = true
55-
56-
# Change to true to raise exception on server if the JS code throws. Let's do this only if not
57-
# in production, as the JS code might still work on the client and we don't want to blow up the
58-
# whole Rails page.
59-
config.raise_on_prerender_error = !Rails.env.production?
46+
# Configure where server bundles are output. Defaults to "ssr-generated".
47+
# This should match your webpack configuration for server bundles.
48+
config.server_bundle_output_path = "ssr-generated"
6049

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

7055
################################################################################
71-
# I18N OPTIONS
72-
################################################################################
73-
# Replace the following line to the location where you keep translation.js & default.js.
74-
config.i18n_dir = Rails.root.join("client/app/libs/i18n")
75-
7656
################################################################################
77-
# MISCELLANEOUS OPTIONS
57+
# FILE SYSTEM BASED COMPONENT REGISTRY
7858
################################################################################
79-
80-
# This allows you to add additional values to the Rails Context. Implement one static method
81-
# called `custom_context(view_context)` and return a Hash.
82-
config.rendering_extension = nil
83-
config.i18n_output_format = "js"
59+
# `components_subdirectory` is the name of the matching directories that contain automatically registered components
60+
# for use in the Rails views. The default is nil, you can enable the feature by updating it in the next line.
61+
config.components_subdirectory = "ror_components"
62+
#
63+
# For automated component registry, `render_component` view helper method tries to load bundle for component from
64+
# generated directory. default is false, you can pass option at the time of individual usage or update the default
65+
# in the following line
66+
config.auto_load_bundle = true
8467
end

config/shakapacker.yml

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,109 @@
11
# Note: You must restart bin/shakapacker-dev-server for changes to take effect
2+
# This file contains the defaults used by shakapacker.
23

34
default: &default
4-
source_path: client/app
5+
source_path: app/javascript
6+
7+
# You can have a subdirectory of the source_path, like 'packs' (recommended).
8+
# Alternatively, you can use '/' to use the whole source_path directory.
9+
# Notice that this is a relative path to source_path
510
source_entry_path: packs
11+
12+
# If nested_entries is true, then we'll pick up subdirectories within the source_entry_path.
13+
# You cannot set this option to true if you set source_entry_path to '/'
14+
nested_entries: true
15+
16+
# While using a File-System-based automated bundle generation feature, miscellaneous warnings suggesting css order
17+
# conflicts may arise due to the mini-css-extract-plugin. For projects where css ordering has been mitigated through
18+
# consistent use of scoping or naming conventions, the css order warnings can be disabled by setting
19+
# css_extract_ignore_order_warnings to true
20+
css_extract_ignore_order_warnings: false
21+
622
public_root_path: public
723
public_output_path: packs
824
cache_path: tmp/shakapacker
925
webpack_compile_output: true
26+
# See https://github.com/shakacode/shakapacker#deployment
27+
shakapacker_precompile: true
1028

11-
# Additional paths webpack should lookup modules
29+
# Location for manifest.json, defaults to {public_output_path}/manifest.json if unset
30+
# manifest_path: public/packs/manifest.json
31+
32+
# Additional paths webpack should look up modules
1233
# ['app/assets', 'engine/foo/app/assets']
1334
additional_paths: []
1435

1536
# Reload manifest.json on all requests so we reload latest compiled packs
1637
cache_manifest: false
17-
18-
# Use the config.build_production_command in config/initializers/react_on_rails.rb
19-
shakapacker_precompile: false
38+
39+
# Select loader to use, available options are 'babel' (default), 'swc' or 'esbuild'
40+
webpack_loader: 'babel'
41+
42+
# Raises an error if there is a mismatch in the shakapacker gem and npm package being used
43+
ensure_consistent_versioning: true
44+
45+
# Select whether the compiler will use SHA digest ('digest' option) or most recent modified timestamp ('mtime') to determine freshness
46+
compiler_strategy: digest
47+
48+
# Select whether the compiler will always use a content hash and not just in production
49+
# Don't use contentHash except for production for performance
50+
# https://webpack.js.org/guides/build-performance/#avoid-production-specific-tooling
51+
useContentHash: false
52+
53+
# Setting the asset host here will override Rails.application.config.asset_host.
54+
# Here, you can set different asset_host per environment. Note that
55+
# SHAKAPACKER_ASSET_HOST will override both configurations.
56+
# asset_host: custom-path
57+
58+
# Utilizing webpack-subresource-integrity plugin, will generate integrity hashes for all entries in manifest.json
59+
# https://github.com/waysact/webpack-subresource-integrity/tree/main/webpack-subresource-integrity
60+
integrity:
61+
enabled: false
62+
# Which cryptographic function(s) to use, for generating the integrity hash(es). Default sha-384. Other possible values sha256, sha512
63+
hash_functions: ["sha384"]
64+
# Default "anonymous". Other possible value "use-credentials"
65+
# https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity#cross-origin_resource_sharing_and_subresource_integrity
66+
cross_origin: "anonymous"
2067

2168
development:
2269
<<: *default
23-
# This is false since we're running `bin/shakapacker -w` in Procfile.dev-setic
24-
compile: false
70+
compile: true
71+
compiler_strategy: mtime
2572

2673
# Reference: https://webpack.js.org/configuration/dev-server/
74+
# Keys not described there are documented inline and in https://github.com/shakacode/shakapacker/
2775
dev_server:
28-
https: false
76+
# For running dev server with https, set `server: https`.
77+
# server: https
78+
2979
host: localhost
3080
port: 3035
3181
# Hot Module Replacement updates modules while the application is running without a full reload
82+
# Used instead of the `hot` key in https://webpack.js.org/configuration/dev-server/#devserverhot
3283
hmr: true
84+
# If HMR is on, CSS will be inlined by delivering it as part of the script payload via style-loader. Be sure
85+
# that you add style-loader to your project dependencies.
86+
#
87+
# If you want to instead deliver CSS via <link> with the mini-css-extract-plugin, set inline_css to false.
88+
# In that case, style-loader is not needed as a dependency.
89+
#
90+
# mini-css-extract-plugin is a required dependency in both cases.
91+
inline_css: true
92+
# Defaults to the inverse of hmr. Uncomment to manually set this.
93+
# live_reload: true
3394
client:
3495
# Should we show a full-screen overlay in the browser when there are compiler errors or warnings?
3596
overlay: true
3697
# May also be a string
3798
# webSocketURL:
38-
# hostname: "0.0.0.0"
39-
# pathname: "/ws"
99+
# hostname: '0.0.0.0'
100+
# pathname: '/ws'
40101
# port: 8080
102+
# Should we use gzip compression?
41103
compress: true
42104
# Note that apps that do not check the host are vulnerable to DNS rebinding attacks
43-
allowed_hosts: [ 'localhost' ]
105+
allowed_hosts: 'auto'
106+
# Shows progress and colorizes output of bin/shakapacker[-dev-server]
44107
pretty: true
45108
headers:
46109
'Access-Control-Allow-Origin': '*'
@@ -52,11 +115,17 @@ test:
52115
<<: *default
53116
compile: true
54117

118+
# Compile test packs to a separate directory
119+
public_output_path: packs-test
120+
55121
production:
56122
<<: *default
57123

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

127+
# Use content hash for naming assets. Cannot be overridden in production.
128+
useContentHash: true
129+
61130
# Cache manifest.json for performance
62131
cache_manifest: true

config/webpack/clientWebpackConfig.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
1-
// The source code including full typescript support is available at:
2-
// https://github.com/shakacode/react_on_rails_tutorial_with_ssr_and_hmr_fast_refresh/blob/master/config/webpack/clientWebpackConfig.js
1+
// The source code including full typescript support is available at:
2+
// https://github.com/shakacode/react_on_rails_demo_ssr_hmr/blob/master/config/webpack/clientWebpackConfig.js
33

4-
const webpack = require('webpack');
54
const commonWebpackConfig = require('./commonWebpackConfig');
65

76
const configureClient = () => {
87
const clientConfig = commonWebpackConfig();
98

10-
clientConfig.plugins.push(
11-
new webpack.ProvidePlugin({
12-
$: 'jquery',
13-
jQuery: 'jquery',
14-
ActionCable: '@rails/actioncable',
15-
}),
16-
);
17-
189
// server-bundle is special and should ONLY be built by the serverConfig
1910
// In case this entry is not deleted, a very strange "window" not found
2011
// error shows referring to window["webpackJsonp"]. That is because the

0 commit comments

Comments
 (0)