Skip to content

Commit c9582dc

Browse files
justin808claude
andcommitted
improve: smart webpack.config.js handling in generator
Enhanced the React on Rails generator to intelligently handle webpack.config.js conflicts instead of always prompting users: - Auto-replace standard Shakapacker configs without prompting - Skip replacement if already React on Rails compatible - Only prompt for truly custom configs with clear explanation - Create backup when replacing custom configs - Provide helpful warning if user declines replacement This eliminates confusing conflicts for the common case while still protecting custom configurations. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 0d25f25 commit c9582dc

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

lib/generators/react_on_rails/base_generator.rb

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,72 @@ def copy_webpack_config
6464
config/webpack/development.js
6565
config/webpack/production.js
6666
config/webpack/serverWebpackConfig.js
67-
config/webpack/webpack.config.js
6867
config/webpack/generateWebpackConfigs.js]
6968
config = {
7069
message: "// The source code including full typescript support is available at:"
7170
}
7271
base_files.each { |file| template("#{base_path}/#{file}.tt", file, config) }
72+
73+
# Handle webpack.config.js separately with smart replacement
74+
copy_webpack_main_config(base_path, config)
75+
end
76+
77+
private
78+
79+
def copy_webpack_main_config(base_path, config)
80+
webpack_config_path = "config/webpack/webpack.config.js"
81+
82+
if File.exist?(webpack_config_path)
83+
existing_content = File.read(webpack_config_path)
84+
85+
# Check if it's the standard Shakapacker config that we can safely replace
86+
if standard_shakapacker_config?(existing_content)
87+
puts " #{set_color('replace', :yellow)} #{webpack_config_path} " \
88+
"(upgrading from standard Shakapacker config)"
89+
template("#{base_path}/#{webpack_config_path}.tt", webpack_config_path, config)
90+
elsif react_on_rails_config?(existing_content)
91+
puts " #{set_color('identical', :blue)} #{webpack_config_path} " \
92+
"(already React on Rails compatible)"
93+
else
94+
handle_custom_webpack_config(base_path, config, webpack_config_path)
95+
end
96+
else
97+
# File doesn't exist, create it
98+
template("#{base_path}/#{webpack_config_path}.tt", webpack_config_path, config)
99+
end
100+
end
101+
102+
def handle_custom_webpack_config(base_path, config, webpack_config_path)
103+
# Custom config - ask user
104+
puts "\n#{set_color('NOTICE:', :yellow)} Your webpack.config.js appears to be customized."
105+
puts "React on Rails needs to replace it with an environment-specific loader."
106+
puts "Your current config will be backed up to webpack.config.js.backup"
107+
108+
if yes?("Replace webpack.config.js with React on Rails version? (Y/n)")
109+
# Create backup
110+
backup_path = "#{webpack_config_path}.backup"
111+
copy_file(webpack_config_path, backup_path)
112+
puts " #{set_color('create', :green)} #{backup_path} (backup of your custom config)"
113+
114+
template("#{base_path}/#{webpack_config_path}.tt", webpack_config_path, config)
115+
else
116+
puts " #{set_color('skip', :yellow)} #{webpack_config_path}"
117+
puts " #{set_color('WARNING:', :red)} React on Rails may not work correctly " \
118+
"without the environment-specific webpack config"
119+
end
120+
end
121+
122+
def standard_shakapacker_config?(content)
123+
# Check if it's the standard Shakapacker config
124+
content.include?("generateWebpackConfig") &&
125+
content.include?("shakapacker") &&
126+
!content.include?("envSpecificConfig") &&
127+
!content.include?("env.nodeEnv")
128+
end
129+
130+
def react_on_rails_config?(content)
131+
# Check if it already has React on Rails environment-specific loading
132+
content.include?("envSpecificConfig") || content.include?("env.nodeEnv")
73133
end
74134

75135
def copy_packer_config
@@ -170,8 +230,6 @@ def append_to_spec_rails_helper
170230
ReactOnRails::TestHelper.configure_rspec_to_compile_assets(config)
171231
STR
172232

173-
private
174-
175233
# From https://github.com/rails/rails/blob/4c940b2dbfb457f67c6250b720f63501d74a45fd/railties/lib/rails/generators/rails/app/app_generator.rb
176234
def app_name
177235
@app_name ||= (defined_app_const_base? ? defined_app_name : File.basename(destination_root))

0 commit comments

Comments
 (0)