Skip to content

Commit a575302

Browse files
justin808claude
andcommitted
Address version requirement review comments
- Add MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION constant to clarify different version requirements - Update supports_auto_registration? to use new constant - Add supports_basic_pack_generation? method for backward compatibility - Update configuration checks to allow basic pack generation for 6.5.1+ while requiring 7.0+ for advanced features - Improve error messages to be more specific about version requirements - Update system checker warning to clarify nested entries support requirement - Update tests to use instance_double for better type safety - Add tests for new supports_basic_pack_generation? method 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent bc73e88 commit a575302

File tree

6 files changed

+77
-30
lines changed

6 files changed

+77
-30
lines changed

lib/react_on_rails/configuration.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,19 @@ def check_autobundling_requirements
198198
raise_missing_components_subdirectory if auto_load_bundle && !components_subdirectory.present?
199199
return unless components_subdirectory.present?
200200

201-
ReactOnRails::PackerUtils.raise_shakapacker_version_incompatible_for_autobundling unless
202-
ReactOnRails::PackerUtils.supports_auto_registration?
203-
ReactOnRails::PackerUtils.raise_nested_entries_disabled unless ReactOnRails::PackerUtils.nested_entries?
201+
# Check basic pack generation support for auto_load_bundle
202+
ReactOnRails::PackerUtils.raise_shakapacker_version_incompatible_for_basic_pack_generation unless
203+
ReactOnRails::PackerUtils.supports_basic_pack_generation?
204+
205+
# Additional checks for advanced features requiring nested entries
206+
if ReactOnRails::PackerUtils.supports_auto_registration?
207+
ReactOnRails::PackerUtils.raise_nested_entries_disabled unless ReactOnRails::PackerUtils.nested_entries?
208+
else
209+
# Warn users about missing advanced features but don't block basic functionality
210+
min_version = ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION
211+
Rails.logger.warn("React on Rails: Basic pack generation enabled. " \
212+
"Upgrade to Shakapacker #{min_version}+ for advanced auto-registration features.")
213+
end
204214
end
205215

206216
def adjust_precompile_task

lib/react_on_rails/packer_utils.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ def self.supports_async_loading?
3838
shakapacker_version_requirement_met?("8.2.0")
3939
end
4040

41+
def self.supports_basic_pack_generation?
42+
shakapacker_version_requirement_met?(ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION)
43+
end
44+
4145
def self.supports_auto_registration?
42-
packer.config.respond_to?(:nested_entries?) &&
43-
shakapacker_version_requirement_met?("7.0.0")
46+
min_version = ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION
47+
packer.config.respond_to?(:nested_entries?) && shakapacker_version_requirement_met?(min_version)
4448
end
4549

4650
# This returns either a URL for the webpack-dev-server, non-server bundle or
@@ -142,10 +146,20 @@ def self.raise_nested_entries_disabled
142146
end
143147

144148
def self.raise_shakapacker_version_incompatible_for_autobundling
149+
msg = <<~MSG
150+
**ERROR** ReactOnRails: Please upgrade Shakapacker to version #{ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION} or \
151+
above to use the automated bundle generation feature (which requires nested_entries support). \
152+
The currently installed version is #{ReactOnRails::PackerUtils.shakapacker_version}. \
153+
Basic pack generation requires Shakapacker #{ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION} or above.
154+
MSG
155+
156+
raise ReactOnRails::Error, msg
157+
end
158+
159+
def self.raise_shakapacker_version_incompatible_for_basic_pack_generation
145160
msg = <<~MSG
146161
**ERROR** ReactOnRails: Please upgrade Shakapacker to version #{ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION} or \
147-
above to use the automated bundle generation feature. The currently installed version is \
148-
#{ReactOnRails::PackerUtils.shakapacker_version}.
162+
above to use basic pack generation features. The currently installed version is #{ReactOnRails::PackerUtils.shakapacker_version}.
149163
MSG
150164

151165
raise ReactOnRails::Error, msg

lib/react_on_rails/packs_generator.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class PacksGenerator
99
CONTAINS_CLIENT_OR_SERVER_REGEX = /\.(server|client)($|\.)/
1010
COMPONENT_EXTENSIONS = /\.(jsx?|tsx?)$/
1111
MINIMUM_SHAKAPACKER_VERSION = "6.5.1"
12+
# Auto-registration requires nested_entries support which was added in 7.0.0
13+
MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION = "7.0.0"
1214

1315
def self.instance
1416
@instance ||= PacksGenerator.new

lib/react_on_rails/system_checker.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ def report_shakapacker_version_with_threshold
624624
if ReactOnRails::PackerUtils.supports_auto_registration?
625625
add_success("✅ Shakapacker #{version} (supports React on Rails auto-registration)")
626626
else
627-
add_warning("⚠️ Shakapacker #{version} - Version 7.0+ needed for React on Rails auto-registration")
627+
add_warning("⚠️ Shakapacker #{version} - Version 7.0+ needed for auto-registration (nested entries)")
628628
end
629629
rescue ArgumentError
630630
# Fallback for invalid version strings

spec/lib/react_on_rails/doctor_spec.rb

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
# rubocop:disable RSpec/VerifiedDoubles
4-
53
require_relative "../../react_on_rails/spec_helper"
64
require_relative "../../../lib/react_on_rails/doctor"
75

@@ -89,13 +87,15 @@
8987

9088
describe "#determine_server_bundle_path" do
9189
context "when Shakapacker gem is available with relative paths" do
92-
let(:shakapacker_config) { double(source_path: "client/app", source_entry_path: "packs") }
90+
let(:shakapacker_config) do
91+
instance_double(Shakapacker::Configuration, source_path: "client/app", source_entry_path: "packs")
92+
end
9393

9494
before do
95-
shakapacker_module = double("Shakapacker", config: shakapacker_config)
95+
shakapacker_module = instance_double(Shakapacker, config: shakapacker_config)
9696
stub_const("Shakapacker", shakapacker_module)
9797
allow(doctor).to receive(:require).with("shakapacker").and_return(true)
98-
allow(doctor).to receive(:get_server_bundle_filename).and_return("server-bundle.js")
98+
allow(doctor).to receive(:server_bundle_filename).and_return("server-bundle.js")
9999
end
100100

101101
it "uses Shakapacker API configuration with relative paths" do
@@ -106,13 +106,16 @@
106106

107107
context "when Shakapacker gem is available with absolute paths" do
108108
let(:rails_root) { "/Users/test/myapp" }
109-
let(:shakapacker_config) { double(source_path: "#{rails_root}/client/app", source_entry_path: "packs") }
109+
let(:shakapacker_config) do
110+
instance_double(Shakapacker::Configuration, source_path: "#{rails_root}/client/app",
111+
source_entry_path: "packs")
112+
end
110113

111114
before do
112-
shakapacker_module = double("Shakapacker", config: shakapacker_config)
115+
shakapacker_module = instance_double(Shakapacker, config: shakapacker_config)
113116
stub_const("Shakapacker", shakapacker_module)
114117
allow(doctor).to receive(:require).with("shakapacker").and_return(true)
115-
allow(doctor).to receive(:get_server_bundle_filename).and_return("server-bundle.js")
118+
allow(doctor).to receive(:server_bundle_filename).and_return("server-bundle.js")
116119
allow(Dir).to receive(:pwd).and_return(rails_root)
117120
end
118121

@@ -125,14 +128,15 @@
125128
context "when Shakapacker gem returns nested absolute paths" do
126129
let(:rails_root) { "/Users/test/myapp" }
127130
let(:shakapacker_config) do
128-
double(source_path: "#{rails_root}/client/app", source_entry_path: "#{rails_root}/client/app/packs")
131+
instance_double(Shakapacker::Configuration, source_path: "#{rails_root}/client/app",
132+
source_entry_path: "#{rails_root}/client/app/packs")
129133
end
130134

131135
before do
132-
shakapacker_module = double("Shakapacker", config: shakapacker_config)
136+
shakapacker_module = instance_double(Shakapacker, config: shakapacker_config)
133137
stub_const("Shakapacker", shakapacker_module)
134138
allow(doctor).to receive(:require).with("shakapacker").and_return(true)
135-
allow(doctor).to receive(:get_server_bundle_filename).and_return("server-bundle.js")
139+
allow(doctor).to receive(:server_bundle_filename).and_return("server-bundle.js")
136140
allow(Dir).to receive(:pwd).and_return(rails_root)
137141
end
138142

@@ -145,7 +149,7 @@
145149
context "when Shakapacker gem is not available" do
146150
before do
147151
allow(doctor).to receive(:require).with("shakapacker").and_raise(LoadError)
148-
allow(doctor).to receive(:get_server_bundle_filename).and_return("server-bundle.js")
152+
allow(doctor).to receive(:server_bundle_filename).and_return("server-bundle.js")
149153
end
150154

151155
it "uses default path" do
@@ -155,7 +159,7 @@
155159
end
156160
end
157161

158-
describe "#get_server_bundle_filename" do
162+
describe "#server_bundle_filename" do
159163
context "when react_on_rails.rb has custom filename" do
160164
let(:initializer_content) do
161165
'config.server_bundle_js_file = "custom-server-bundle.js"'
@@ -167,7 +171,7 @@
167171
end
168172

169173
it "extracts filename from initializer" do
170-
filename = doctor.send(:get_server_bundle_filename)
174+
filename = doctor.send(:server_bundle_filename)
171175
expect(filename).to eq("custom-server-bundle.js")
172176
end
173177
end
@@ -178,12 +182,10 @@
178182
end
179183

180184
it "returns default filename" do
181-
filename = doctor.send(:get_server_bundle_filename)
185+
filename = doctor.send(:server_bundle_filename)
182186
expect(filename).to eq("server-bundle.js")
183187
end
184188
end
185189
end
186190
end
187191
end
188-
189-
# rubocop:enable RSpec/VerifiedDoubles

spec/react_on_rails/packer_utils_spec.rb

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,31 +91,50 @@ module ReactOnRails
9191
end
9292
end
9393

94+
describe ".supports_basic_pack_generation?" do
95+
it "returns true when Shakapacker >= 6.5.1" do
96+
allow(described_class).to receive(:shakapacker_version_requirement_met?)
97+
.with(ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION).and_return(true)
98+
99+
expect(described_class.supports_basic_pack_generation?).to be(true)
100+
end
101+
102+
it "returns false when Shakapacker < 6.5.1" do
103+
allow(described_class).to receive(:shakapacker_version_requirement_met?)
104+
.with(ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION).and_return(false)
105+
106+
expect(described_class.supports_basic_pack_generation?).to be(false)
107+
end
108+
end
109+
94110
describe ".supports_auto_registration?" do
95-
let(:mock_config) { double("MockConfig") } # rubocop:disable RSpec/VerifiedDoubles
96-
let(:mock_packer) { double("MockPacker", config: mock_config) } # rubocop:disable RSpec/VerifiedDoubles
111+
let(:mock_config) { instance_double(Shakapacker::Config) }
112+
let(:mock_packer) { instance_double(Shakapacker, config: mock_config) }
97113

98114
before do
99115
allow(described_class).to receive(:packer).and_return(mock_packer)
100116
end
101117

102118
it "returns true when Shakapacker >= 7.0.0 with nested_entries support" do
103119
allow(mock_config).to receive(:respond_to?).with(:nested_entries?).and_return(true)
104-
allow(described_class).to receive(:shakapacker_version_requirement_met?).with("7.0.0").and_return(true)
120+
allow(described_class).to receive(:shakapacker_version_requirement_met?)
121+
.with(ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION).and_return(true)
105122

106123
expect(described_class.supports_auto_registration?).to be(true)
107124
end
108125

109126
it "returns false when Shakapacker < 7.0.0" do
110127
allow(mock_config).to receive(:respond_to?).with(:nested_entries?).and_return(true)
111-
allow(described_class).to receive(:shakapacker_version_requirement_met?).with("7.0.0").and_return(false)
128+
allow(described_class).to receive(:shakapacker_version_requirement_met?)
129+
.with(ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION).and_return(false)
112130

113131
expect(described_class.supports_auto_registration?).to be(false)
114132
end
115133

116134
it "returns false when nested_entries method is not available" do
117135
allow(mock_config).to receive(:respond_to?).with(:nested_entries?).and_return(false)
118-
allow(described_class).to receive(:shakapacker_version_requirement_met?).with("7.0.0").and_return(true)
136+
allow(described_class).to receive(:shakapacker_version_requirement_met?)
137+
.with(ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION).and_return(true)
119138

120139
expect(described_class.supports_auto_registration?).to be(false)
121140
end

0 commit comments

Comments
 (0)