Skip to content

Commit 7a4915e

Browse files
committed
Tighten bundler config detection in system checker
1 parent a644fe1 commit 7a4915e

2 files changed

Lines changed: 61 additions & 12 deletions

File tree

react_on_rails/lib/react_on_rails/system_checker.rb

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module ReactOnRails
1111
class SystemChecker
1212
attr_reader :messages
1313

14+
SUPPORTED_ASSETS_BUNDLERS = %w[webpack rspack].freeze
15+
1416
def initialize
1517
@messages = []
1618
end
@@ -134,7 +136,8 @@ def check_shakapacker_configuration
134136
• bin/shakapacker
135137
• bin/shakapacker-dev-server
136138
• config/shakapacker.yml
137-
• config/{webpack,rspack}/{webpack,rspack}.config.{js,ts}
139+
• config/webpack/webpack.config.{js,ts}
140+
• config/rspack/rspack.config.{js,ts}
138141
139142
Run: bundle exec rails shakapacker:install
140143
MSG
@@ -325,9 +328,9 @@ def detect_bundler_config_path
325328
end
326329

327330
add_warning(
328-
"⚠️ Found both webpack and rspack configs. Could not determine active bundler; defaulting to rspack."
331+
"⚠️ Found both webpack and rspack configs. Could not determine active bundler; defaulting to webpack."
329332
)
330-
paths_by_bundler["rspack"].first || paths_by_bundler["webpack"].first
333+
paths_by_bundler["webpack"].first || paths_by_bundler["rspack"].first
331334
end
332335

333336
def suggest_webpack_inspection(config_path)
@@ -519,16 +522,27 @@ def existing_bundler_config_paths(bundler)
519522
end
520523

521524
def configured_assets_bundler
522-
shakapacker_config_path = "config/shakapacker.yml"
523-
return nil unless File.exist?(shakapacker_config_path)
525+
config = parsed_shakapacker_config
526+
return nil unless config
524527

525-
config_content = File.read(shakapacker_config_path)
526-
match = config_content.match(/^\s*assets_bundler:\s*["']?(webpack|rspack)["']?\s*$/)
527-
match&.captures&.first
528-
rescue StandardError
528+
rails_env = ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
529+
bundler_from_shakapacker_section(config, rails_env) || bundler_from_shakapacker_section(config, "default")
530+
rescue StandardError, ScriptError
529531
nil
530532
end
531533

534+
def bundler_from_shakapacker_section(config, section_name)
535+
section = config[section_name] || config[section_name.to_sym]
536+
return nil unless section.is_a?(Hash)
537+
538+
normalize_assets_bundler(section["assets_bundler"] || section[:assets_bundler])
539+
end
540+
541+
def normalize_assets_bundler(value)
542+
normalized = value.to_s.strip.downcase
543+
SUPPORTED_ASSETS_BUNDLERS.include?(normalized) ? normalized : nil
544+
end
545+
532546
def required_react_dependencies
533547
deps = {
534548
"react" => "React library",

react_on_rails/spec/lib/react_on_rails/system_checker_spec.rb

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@
154154
expect(result).to be false
155155
expect(checker.errors?).to be true
156156
expect(checker.messages.last[:content]).to include("Shakapacker is not properly configured")
157+
expect(checker.messages.last[:content]).to include("config/webpack/webpack.config.{js,ts}")
158+
expect(checker.messages.last[:content]).to include("config/rspack/rspack.config.{js,ts}")
157159
end
158160
end
159161

@@ -716,13 +718,46 @@
716718
end).to be true
717719
end
718720

719-
it "warns and defaults to rspack when both bundlers exist and config is ambiguous" do
721+
it "warns and defaults to webpack when both bundlers exist and config is ambiguous" do
720722
allow(File).to receive(:exist?).with("config/rspack/rspack.config.ts").and_return(true)
721723
allow(File).to receive(:exist?).with("config/webpack/webpack.config.ts").and_return(true)
722724
allow(File).to receive(:exist?).with("config/shakapacker.yml").and_return(false)
723725

724-
expect(checker.send(:detect_bundler_config_path)).to eq("config/rspack/rspack.config.ts")
725-
expect(checker.messages.any? { |msg| msg[:content].include?("defaulting to rspack") }).to be true
726+
expect(checker.send(:detect_bundler_config_path)).to eq("config/webpack/webpack.config.ts")
727+
expect(checker.messages.any? { |msg| msg[:content].include?("defaulting to webpack") }).to be true
728+
end
729+
end
730+
731+
describe "#configured_assets_bundler" do
732+
before do
733+
allow(File).to receive(:exist?).with("config/shakapacker.yml").and_return(true)
734+
end
735+
736+
it "parses ERB-backed assets_bundler values from shakapacker.yml" do
737+
allow(File).to receive(:read).with("config/shakapacker.yml").and_return(<<~YAML)
738+
default: &default
739+
assets_bundler: <%= "webpack" %> # inline comment
740+
development:
741+
<<: *default
742+
YAML
743+
744+
expect(checker.send(:configured_assets_bundler)).to eq("webpack")
745+
end
746+
747+
it "prefers the current Rails environment over the default config" do
748+
allow(File).to receive(:read).with("config/shakapacker.yml").and_return(<<~YAML)
749+
default:
750+
assets_bundler: webpack
751+
test:
752+
assets_bundler: rspack
753+
YAML
754+
755+
original_rails_env = ENV.fetch("RAILS_ENV", nil)
756+
ENV["RAILS_ENV"] = "test"
757+
758+
expect(checker.send(:configured_assets_bundler)).to eq("rspack")
759+
ensure
760+
ENV["RAILS_ENV"] = original_rails_env
726761
end
727762
end
728763

0 commit comments

Comments
 (0)