Skip to content

Commit 67210ef

Browse files
justin808claude
andcommitted
Fix thread safety and simplify Shakapacker packer memoization
- Replace defined?(@Packer) pattern with thread-safe @Packer ||= approach - Remove unnecessary LoadError handling since Shakapacker is required in gemspec - Fix RuboCop line length violations - Extract long constant names to variables for readability 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 9db5190 commit 67210ef

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

lib/react_on_rails/configuration.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,10 @@ def configure_generated_assets_dirs_deprecation
290290
return if generated_assets_dirs.blank?
291291

292292
packer_public_output_path = ReactOnRails::PackerUtils.packer_public_output_path
293-
Rails.logger.warn "You specified generated_assets_dirs in `config/initializers/react_on_rails.rb` with Shakapacker. " \
294-
"Remove this configuration as the output path is automatically determined by " \
295-
"`public_output_path` in shakapacker.yml (currently: #{packer_public_output_path})."
293+
Rails.logger.warn "You specified generated_assets_dirs in `config/initializers/react_on_rails.rb` " \
294+
"with Shakapacker. Remove this configuration as the output path is automatically " \
295+
"determined by `public_output_path` in shakapacker.yml " \
296+
"(currently: #{packer_public_output_path})."
296297
end
297298

298299
def ensure_webpack_generated_files_exists

lib/react_on_rails/packer_utils.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
module ReactOnRails
44
module PackerUtils
55
def self.packer
6-
return @packer if defined?(@packer)
7-
8-
@packer = begin
6+
@packer ||= begin
97
require "shakapacker"
108
::Shakapacker
11-
rescue LoadError => e
12-
raise ReactOnRails::Error, "Shakapacker gem is required but not available. Please add 'shakapacker' to your Gemfile. Error: #{e.message}"
139
end
1410
end
1511

@@ -50,8 +46,8 @@ def self.supports_basic_pack_generation?
5046
end
5147

5248
def self.supports_auto_registration?
53-
packer.config.respond_to?(:nested_entries?) &&
54-
shakapacker_version_requirement_met?(ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION)
49+
min_version = ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION
50+
packer.config.respond_to?(:nested_entries?) && shakapacker_version_requirement_met?(min_version)
5551
end
5652

5753
# This returns either a URL for the webpack-dev-server, non-server bundle or

lib/react_on_rails/utils.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
require "active_support/core_ext/string"
88

99
module ReactOnRails
10-
module Utils # rubocop:disable Metrics/ModuleLength
10+
module Utils
1111
TRUNCATION_FILLER = "\n... TRUNCATED #{
1212
Rainbow('To see the full output, set FULL_TEXT_ERRORS=true.').red
1313
} ...\n".freeze

spec/react_on_rails/packer_utils_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,12 @@ module ReactOnRails
149149

150150
it "ensures version requirements are logically consistent" do
151151
basic_version = Gem::Version.new(ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION)
152-
auto_reg_version = Gem::Version.new(ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION)
152+
auto_reg_version = Gem::Version.new(
153+
ReactOnRails::PacksGenerator::MINIMUM_SHAKAPACKER_VERSION_FOR_AUTO_REGISTRATION
154+
)
153155

154156
expect(auto_reg_version).to be >= basic_version,
155-
"Auto-registration version should be >= basic pack generation version"
157+
"Auto-registration version should be >= basic pack generation version"
156158
end
157159

158160
it "validates version checks are cached properly" do

0 commit comments

Comments
 (0)