Skip to content

Commit 8d03cc6

Browse files
justin808claude
andcommitted
Remove PackerUtils.packer method entirely and use Shakapacker directly
- Eliminate the packer method abstraction completely - Replace all packer method calls with direct Shakapacker references - Update all tests to mock Shakapacker directly instead of the packer method - Add require "shakapacker" at module level for test compatibility - Remove all unnecessary abstraction layers as suggested in code review - Simplifies codebase by removing unnecessary indirection - All tests passing with direct Shakapacker usage 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e038121 commit 8d03cc6

File tree

4 files changed

+65
-71
lines changed

4 files changed

+65
-71
lines changed

lib/react_on_rails/packer_utils.rb

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
# frozen_string_literal: true
22

3+
require "shakapacker"
4+
35
module ReactOnRails
46
module PackerUtils
5-
def self.packer
6-
@packer ||= begin
7-
require "shakapacker"
8-
::Shakapacker
9-
end
10-
end
11-
127
def self.dev_server_running?
13-
packer.dev_server.running?
8+
Shakapacker.dev_server.running?
149
end
1510

1611
def self.dev_server_url
17-
"#{packer.dev_server.protocol}://#{packer.dev_server.host_with_port}"
12+
"#{Shakapacker.dev_server.protocol}://#{Shakapacker.dev_server.host_with_port}"
1813
end
1914

2015
def self.shakapacker_version
@@ -47,14 +42,14 @@ def self.supports_basic_pack_generation?
4742

4843
def self.supports_autobundling?
4944
min_version = ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION
50-
packer.config.respond_to?(:nested_entries?) && shakapacker_version_requirement_met?(min_version)
45+
::Shakapacker.config.respond_to?(:nested_entries?) && shakapacker_version_requirement_met?(min_version)
5146
end
5247

5348
# This returns either a URL for the webpack-dev-server, non-server bundle or
5449
# the hashed server bundle if using the same bundle for the client.
5550
# Otherwise returns a file path.
5651
def self.bundle_js_uri_from_packer(bundle_name)
57-
hashed_bundle_name = packer.manifest.lookup!(bundle_name)
52+
hashed_bundle_name = ::Shakapacker.manifest.lookup!(bundle_name)
5853

5954
# Support for hashing the server-bundle and having that built
6055
# the webpack-dev-server is provided by the config value
@@ -63,7 +58,7 @@ def self.bundle_js_uri_from_packer(bundle_name)
6358
is_bundle_running_on_server = (bundle_name == ReactOnRails.configuration.server_bundle_js_file) ||
6459
(bundle_name == ReactOnRails.configuration.rsc_bundle_js_file)
6560

66-
if packer.dev_server.running? && (!is_bundle_running_on_server ||
61+
if ::Shakapacker.dev_server.running? && (!is_bundle_running_on_server ||
6762
ReactOnRails.configuration.same_bundle_for_client_and_server)
6863
"#{dev_server_url}#{hashed_bundle_name}"
6964
else
@@ -72,7 +67,7 @@ def self.bundle_js_uri_from_packer(bundle_name)
7267
end
7368

7469
def self.public_output_uri_path
75-
"#{packer.config.public_output_path.relative_path_from(packer.config.public_path)}/"
70+
"#{::Shakapacker.config.public_output_path.relative_path_from(::Shakapacker.config.public_path)}/"
7671
end
7772

7873
# The function doesn't ensure that the asset exists.
@@ -91,36 +86,36 @@ def self.precompile?
9186
end
9287

9388
def self.packer_source_path
94-
packer.config.source_path
89+
::Shakapacker.config.source_path
9590
end
9691

9792
def self.packer_source_entry_path
98-
packer.config.source_entry_path
93+
::Shakapacker.config.source_entry_path
9994
end
10095

10196
def self.nested_entries?
102-
packer.config.nested_entries?
97+
::Shakapacker.config.nested_entries?
10398
end
10499

105100
def self.packer_public_output_path
106-
packer.config.public_output_path.to_s
101+
::Shakapacker.config.public_output_path.to_s
107102
end
108103

109104
def self.manifest_exists?
110-
packer.config.public_manifest_path.exist?
105+
::Shakapacker.config.public_manifest_path.exist?
111106
end
112107

113108
def self.packer_source_path_explicit?
114-
packer.config.send(:data)[:source_path].present?
109+
::Shakapacker.config.send(:data)[:source_path].present?
115110
end
116111

117112
def self.check_manifest_not_cached
118-
return unless packer.config.cache_manifest?
113+
return unless ::Shakapacker.config.cache_manifest?
119114

120115
msg = <<-MSG.strip_heredoc
121116
ERROR: you have enabled cache_manifest in the #{Rails.env} env when using the
122117
ReactOnRails::TestHelper.configure_rspec_to_compile_assets helper
123-
To fix this: edit your config/shakapacker.yml file and set cache_manifest to false for test.
118+
To fix this: edit your config/shaka::Shakapacker.yml file and set cache_manifest to false for test.
124119
MSG
125120
puts wrap_message(msg)
126121
exit!
@@ -140,8 +135,8 @@ def self.webpack_assets_status_checker
140135

141136
def self.raise_nested_entries_disabled
142137
msg = <<~MSG
143-
**ERROR** ReactOnRails: `nested_entries` is configured to be disabled in shakapacker. Please update \
144-
config/shakapacker.yml to enable nested entries. for more information read
138+
**ERROR** ReactOnRails: `nested_entries` is configured to be disabled in shaka::Shakapacker. Please update \
139+
config/shaka::Shakapacker.yml to enable nested entries. for more information read
145140
https://www.shakacode.com/react-on-rails/docs/guides/file-system-based-automated-bundle-generation.md#enable-nested_entries-for-shakapacker
146141
MSG
147142

@@ -150,18 +145,18 @@ def self.raise_nested_entries_disabled
150145

151146
def self.raise_shakapacker_version_incompatible_for_autobundling
152147
msg = <<~MSG
153-
**ERROR** ReactOnRails: Please upgrade Shakapacker to version #{ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION} or \
148+
**ERROR** ReactOnRails: Please upgrade ::Shakapacker to version #{ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION} or \
154149
above to use the automated bundle generation feature (which requires nested_entries support). \
155150
The currently installed version is #{ReactOnRails::PackerUtils.shakapacker_version}. \
156-
Basic pack generation requires Shakapacker #{ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION} or above.
151+
Basic pack generation requires ::Shakapacker #{ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION} or above.
157152
MSG
158153

159154
raise ReactOnRails::Error, msg
160155
end
161156

162157
def self.raise_shakapacker_version_incompatible_for_basic_pack_generation
163158
msg = <<~MSG
164-
**ERROR** ReactOnRails: Please upgrade Shakapacker to version #{ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION} or \
159+
**ERROR** ReactOnRails: Please upgrade ::Shakapacker to version #{ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION} or \
165160
above to use basic pack generation features. The currently installed version is #{ReactOnRails::PackerUtils.shakapacker_version}.
166161
MSG
167162

@@ -170,7 +165,7 @@ def self.raise_shakapacker_version_incompatible_for_basic_pack_generation
170165

171166
def self.raise_shakapacker_not_installed
172167
msg = <<~MSG
173-
**ERROR** ReactOnRails: Missing Shakapacker gem. Please upgrade to use Shakapacker \
168+
**ERROR** ReactOnRails: Missing ::Shakapacker gem. Please upgrade to use ::Shakapacker \
174169
#{ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION} or above to use the \
175170
automated bundle generation feature.
176171
MSG

spec/react_on_rails/packer_utils_spec.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ module ReactOnRails
4242

4343
context "when dev server is running" do
4444
before do
45-
allow(described_class.packer).to receive(:dev_server).and_return(
45+
allow(::Shakapacker).to receive(:dev_server).and_return(
4646
instance_double(
47-
ReactOnRails::PackerUtils.packer::DevServer,
47+
::Shakapacker::DevServer,
4848
running?: true,
4949
protocol: "http",
5050
host_with_port: "localhost:3035"
5151
)
5252
)
5353

54-
allow(described_class.packer).to receive_message_chain("config.public_output_path")
54+
allow(::Shakapacker).to receive_message_chain("config.public_output_path")
5555
.and_return(Pathname.new(public_output_path))
56-
allow(described_class.packer).to receive_message_chain("config.public_path")
56+
allow(::Shakapacker).to receive_message_chain("config.public_path")
5757
.and_return(Pathname.new("/path/to/public"))
5858
end
5959

@@ -65,8 +65,8 @@ module ReactOnRails
6565

6666
context "when dev server is not running" do
6767
before do
68-
allow(described_class.packer).to receive_message_chain("dev_server.running?").and_return(false)
69-
allow(described_class.packer).to receive_message_chain("config.public_output_path")
68+
allow(::Shakapacker).to receive_message_chain("dev_server.running?").and_return(false)
69+
allow(::Shakapacker).to receive_message_chain("config.public_output_path")
7070
.and_return(Pathname.new(public_output_path))
7171
end
7272

@@ -78,28 +78,28 @@ module ReactOnRails
7878
end
7979

8080
describe ".supports_async_loading?" do
81-
it "returns true when Shakapacker >= 8.2.0" do
81+
it "returns true when ::Shakapacker >= 8.2.0" do
8282
allow(described_class).to receive(:shakapacker_version_requirement_met?).with("8.2.0").and_return(true)
8383

8484
expect(described_class.supports_async_loading?).to be(true)
8585
end
8686

87-
it "returns false when Shakapacker < 8.2.0" do
87+
it "returns false when ::Shakapacker < 8.2.0" do
8888
allow(described_class).to receive(:shakapacker_version_requirement_met?).with("8.2.0").and_return(false)
8989

9090
expect(described_class.supports_async_loading?).to be(false)
9191
end
9292
end
9393

9494
describe ".supports_basic_pack_generation?" do
95-
it "returns true when Shakapacker >= 6.5.1" do
95+
it "returns true when ::Shakapacker >= 6.5.1" do
9696
allow(described_class).to receive(:shakapacker_version_requirement_met?)
9797
.with(ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION).and_return(true)
9898

9999
expect(described_class.supports_basic_pack_generation?).to be(true)
100100
end
101101

102-
it "returns false when Shakapacker < 6.5.1" do
102+
it "returns false when ::Shakapacker < 6.5.1" do
103103
allow(described_class).to receive(:shakapacker_version_requirement_met?)
104104
.with(ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION).and_return(false)
105105

@@ -108,22 +108,22 @@ module ReactOnRails
108108
end
109109

110110
describe ".supports_autobundling?" do
111-
let(:mock_config) { instance_double("Shakapacker::Config") } # rubocop:disable RSpec/VerifiedDoubleReference
112-
let(:mock_packer) { instance_double("Shakapacker", config: mock_config) } # rubocop:disable RSpec/VerifiedDoubleReference
111+
let(:mock_config) { instance_double("::Shakapacker::Config") } # rubocop:disable RSpec/VerifiedDoubleReference
112+
let(:mock_packer) { instance_double("::Shakapacker", config: mock_config) } # rubocop:disable RSpec/VerifiedDoubleReference
113113

114114
before do
115-
allow(described_class).to receive(:packer).and_return(mock_packer)
115+
allow(::Shakapacker).to receive(:config).and_return(mock_config)
116116
end
117117

118-
it "returns true when Shakapacker >= 7.0.0 with nested_entries support" do
118+
it "returns true when ::Shakapacker >= 7.0.0 with nested_entries support" do
119119
allow(mock_config).to receive(:respond_to?).with(:nested_entries?).and_return(true)
120120
allow(described_class).to receive(:shakapacker_version_requirement_met?)
121121
.with(ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION).and_return(true)
122122

123123
expect(described_class.supports_autobundling?).to be(true)
124124
end
125125

126-
it "returns false when Shakapacker < 7.0.0" do
126+
it "returns false when ::Shakapacker < 7.0.0" do
127127
allow(mock_config).to receive(:respond_to?).with(:nested_entries?).and_return(true)
128128
allow(described_class).to receive(:shakapacker_version_requirement_met?)
129129
.with(ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION).and_return(false)

spec/react_on_rails/test_helper/webpack_assets_status_checker_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
let(:fixture_dirname) { "assets_with_manifest_exist_server_bundle_separate" }
6363

6464
before do
65-
Packer = ReactOnRails::PackerUtils.packer # rubocop:disable Lint/ConstantDefinitionInBlock, RSpec/LeakyConstantDeclaration
65+
Packer = Shakapacker # rubocop:disable Lint/ConstantDefinitionInBlock, RSpec/LeakyConstantDeclaration
6666
allow(ReactOnRails::PackerUtils).to receive_messages(
6767
manifest_exists?: true,
6868
packer_public_output_path: generated_assets_full_path

0 commit comments

Comments
 (0)