Skip to content

Commit a148ae5

Browse files
justin808claude
andcommitted
Fix generator method visibility - move add_js_dependencies before private
The add_js_dependencies and install_js_dependencies methods were defined after the 'private' declaration, making them private methods that Rails generators don't automatically execute. This fix moves these critical methods before the 'private' declaration, making them public methods that will be called automatically by Rails generators. This should resolve the CSS dependency installation issue that was causing webpack compilation failures. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent dc7ef85 commit a148ae5

File tree

1 file changed

+112
-112
lines changed

1 file changed

+112
-112
lines changed

lib/generators/react_on_rails/base_generator.rb

Lines changed: 112 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -76,118 +76,6 @@ def copy_webpack_config
7676
copy_webpack_main_config(base_path, config)
7777
end
7878

79-
private
80-
81-
def copy_webpack_main_config(base_path, config)
82-
webpack_config_path = "config/webpack/webpack.config.js"
83-
84-
if File.exist?(webpack_config_path)
85-
existing_content = File.read(webpack_config_path)
86-
87-
# Check if it's the standard Shakapacker config that we can safely replace
88-
if standard_shakapacker_config?(existing_content)
89-
# Remove the file first to avoid conflict prompt, then recreate it
90-
remove_file(webpack_config_path, verbose: false)
91-
# Show what we're doing
92-
puts " #{set_color('replace', :green)} #{webpack_config_path} " \
93-
"(auto-upgrading from standard Shakapacker to React on Rails config)"
94-
template("#{base_path}/#{webpack_config_path}.tt", webpack_config_path, config)
95-
elsif react_on_rails_config?(existing_content)
96-
puts " #{set_color('identical', :blue)} #{webpack_config_path} " \
97-
"(already React on Rails compatible)"
98-
# Skip - don't need to do anything
99-
else
100-
handle_custom_webpack_config(base_path, config, webpack_config_path)
101-
end
102-
else
103-
# File doesn't exist, create it
104-
template("#{base_path}/#{webpack_config_path}.tt", webpack_config_path, config)
105-
end
106-
end
107-
108-
def handle_custom_webpack_config(base_path, config, webpack_config_path)
109-
# Custom config - ask user
110-
puts "\n#{set_color('NOTICE:', :yellow)} Your webpack.config.js appears to be customized."
111-
puts "React on Rails needs to replace it with an environment-specific loader."
112-
puts "Your current config will be backed up to webpack.config.js.backup"
113-
114-
if yes?("Replace webpack.config.js with React on Rails version? (Y/n)")
115-
# Create backup
116-
backup_path = "#{webpack_config_path}.backup"
117-
copy_file(webpack_config_path, backup_path)
118-
puts " #{set_color('create', :green)} #{backup_path} (backup of your custom config)"
119-
120-
template("#{base_path}/#{webpack_config_path}.tt", webpack_config_path, config)
121-
else
122-
puts " #{set_color('skip', :yellow)} #{webpack_config_path}"
123-
puts " #{set_color('WARNING:', :red)} React on Rails may not work correctly " \
124-
"without the environment-specific webpack config"
125-
end
126-
end
127-
128-
def standard_shakapacker_config?(content)
129-
# Get the expected default config based on Shakapacker version
130-
expected_configs = shakapacker_default_configs
131-
132-
# Check if the content matches any of the known default configurations
133-
expected_configs.any? { |config| content_matches_template?(content, config) }
134-
end
135-
136-
def content_matches_template?(content, template)
137-
# Normalize whitespace and compare
138-
normalize_config_content(content) == normalize_config_content(template)
139-
end
140-
141-
def normalize_config_content(content)
142-
# Remove comments, normalize whitespace, and clean up for comparison
143-
content.gsub(%r{//.*$}, "") # Remove single-line comments
144-
.gsub(%r{/\*.*?\*/}m, "") # Remove multi-line comments
145-
.gsub(/\s+/, " ") # Normalize whitespace
146-
.strip
147-
end
148-
149-
def shakapacker_default_configs
150-
configs = []
151-
152-
# Shakapacker v7+ (generateWebpackConfig function)
153-
configs << <<~CONFIG
154-
// See the shakacode/shakapacker README and docs directory for advice on customizing your webpackConfig.
155-
const { generateWebpackConfig } = require('shakapacker')
156-
157-
const webpackConfig = generateWebpackConfig()
158-
159-
module.exports = webpackConfig
160-
CONFIG
161-
162-
# Shakapacker v6 (webpackConfig object)
163-
configs << <<~CONFIG
164-
const { webpackConfig } = require('shakapacker')
165-
166-
// See the shakacode/shakapacker README and docs directory for advice on customizing your webpackConfig.
167-
168-
module.exports = webpackConfig
169-
CONFIG
170-
171-
# Also check without comments for variations
172-
configs << <<~CONFIG
173-
const { generateWebpackConfig } = require('shakapacker')
174-
const webpackConfig = generateWebpackConfig()
175-
module.exports = webpackConfig
176-
CONFIG
177-
178-
configs << <<~CONFIG
179-
const { webpackConfig } = require('shakapacker')
180-
module.exports = webpackConfig
181-
CONFIG
182-
183-
configs
184-
end
185-
186-
def react_on_rails_config?(content)
187-
# Check if it already has React on Rails environment-specific loading
188-
content.include?("envSpecificConfig") || content.include?("env.nodeEnv")
189-
end
190-
19179
def copy_packer_config
19280
puts "Adding Shakapacker #{ReactOnRails::PackerUtils.shakapacker_version} config"
19381
base_path = "base/base/"
@@ -305,6 +193,118 @@ def append_to_spec_rails_helper
305193
end
306194
end
307195

196+
private
197+
198+
def copy_webpack_main_config(base_path, config)
199+
webpack_config_path = "config/webpack/webpack.config.js"
200+
201+
if File.exist?(webpack_config_path)
202+
existing_content = File.read(webpack_config_path)
203+
204+
# Check if it's the standard Shakapacker config that we can safely replace
205+
if standard_shakapacker_config?(existing_content)
206+
# Remove the file first to avoid conflict prompt, then recreate it
207+
remove_file(webpack_config_path, verbose: false)
208+
# Show what we're doing
209+
puts " #{set_color('replace', :green)} #{webpack_config_path} " \
210+
"(auto-upgrading from standard Shakapacker to React on Rails config)"
211+
template("#{base_path}/#{webpack_config_path}.tt", webpack_config_path, config)
212+
elsif react_on_rails_config?(existing_content)
213+
puts " #{set_color('identical', :blue)} #{webpack_config_path} " \
214+
"(already React on Rails compatible)"
215+
# Skip - don't need to do anything
216+
else
217+
handle_custom_webpack_config(base_path, config, webpack_config_path)
218+
end
219+
else
220+
# File doesn't exist, create it
221+
template("#{base_path}/#{webpack_config_path}.tt", webpack_config_path, config)
222+
end
223+
end
224+
225+
def handle_custom_webpack_config(base_path, config, webpack_config_path)
226+
# Custom config - ask user
227+
puts "\n#{set_color('NOTICE:', :yellow)} Your webpack.config.js appears to be customized."
228+
puts "React on Rails needs to replace it with an environment-specific loader."
229+
puts "Your current config will be backed up to webpack.config.js.backup"
230+
231+
if yes?("Replace webpack.config.js with React on Rails version? (Y/n)")
232+
# Create backup
233+
backup_path = "#{webpack_config_path}.backup"
234+
copy_file(webpack_config_path, backup_path)
235+
puts " #{set_color('create', :green)} #{backup_path} (backup of your custom config)"
236+
237+
template("#{base_path}/#{webpack_config_path}.tt", webpack_config_path, config)
238+
else
239+
puts " #{set_color('skip', :yellow)} #{webpack_config_path}"
240+
puts " #{set_color('WARNING:', :red)} React on Rails may not work correctly " \
241+
"without the environment-specific webpack config"
242+
end
243+
end
244+
245+
def standard_shakapacker_config?(content)
246+
# Get the expected default config based on Shakapacker version
247+
expected_configs = shakapacker_default_configs
248+
249+
# Check if the content matches any of the known default configurations
250+
expected_configs.any? { |config| content_matches_template?(content, config) }
251+
end
252+
253+
def content_matches_template?(content, template)
254+
# Normalize whitespace and compare
255+
normalize_config_content(content) == normalize_config_content(template)
256+
end
257+
258+
def normalize_config_content(content)
259+
# Remove comments, normalize whitespace, and clean up for comparison
260+
content.gsub(%r{//.*$}, "") # Remove single-line comments
261+
.gsub(%r{/\*.*?\*/}m, "") # Remove multi-line comments
262+
.gsub(/\s+/, " ") # Normalize whitespace
263+
.strip
264+
end
265+
266+
def shakapacker_default_configs
267+
configs = []
268+
269+
# Shakapacker v7+ (generateWebpackConfig function)
270+
configs << <<~CONFIG
271+
// See the shakacode/shakapacker README and docs directory for advice on customizing your webpackConfig.
272+
const { generateWebpackConfig } = require('shakapacker')
273+
274+
const webpackConfig = generateWebpackConfig()
275+
276+
module.exports = webpackConfig
277+
CONFIG
278+
279+
# Shakapacker v6 (webpackConfig object)
280+
configs << <<~CONFIG
281+
const { webpackConfig } = require('shakapacker')
282+
283+
// See the shakacode/shakapacker README and docs directory for advice on customizing your webpackConfig.
284+
285+
module.exports = webpackConfig
286+
CONFIG
287+
288+
# Also check without comments for variations
289+
configs << <<~CONFIG
290+
const { generateWebpackConfig } = require('shakapacker')
291+
const webpackConfig = generateWebpackConfig()
292+
module.exports = webpackConfig
293+
CONFIG
294+
295+
configs << <<~CONFIG
296+
const { webpackConfig } = require('shakapacker')
297+
module.exports = webpackConfig
298+
CONFIG
299+
300+
configs
301+
end
302+
303+
def react_on_rails_config?(content)
304+
# Check if it already has React on Rails environment-specific loading
305+
content.include?("envSpecificConfig") || content.include?("env.nodeEnv")
306+
end
307+
308308
CONFIGURE_RSPEC_TO_COMPILE_ASSETS = <<-STR.strip_heredoc
309309
RSpec.configure do |config|
310310
# Ensure that if we are running js tests, we are using latest webpack assets

0 commit comments

Comments
 (0)