Skip to content

Commit 9c5422f

Browse files
justin808claude
andcommitted
Improve pack generation to avoid unnecessary subprocess spawning
Refactors pack_generator.rb to intelligently choose between direct Rake task execution and bundle exec based on the current context. When running in a Rails/Bundler environment (e.g., from bin/dev), the pack generation now runs the Rake task directly, avoiding the overhead of spawning a subprocess. Key improvements: - Adds run_pack_generation method that detects Rails availability - Implements run_rake_task_directly for in-process execution when Rails is available - Falls back to run_via_bundle_exec when Rails is not available - Includes proper silent mode handling for both execution paths - Prevents dead code by ensuring Rails is always defined before use This change is based on the optimization from the shakapacker-9.3.0 branch (commit b716f08), which already excluded the dead code that would have checked for Rails being defined when it's guaranteed to be present. Fixes #1909 Generated with Claude Code (https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent fe363a9 commit 9c5422f

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

lib/react_on_rails/dev/pack_generator.rb

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class << self
99
def generate(verbose: false)
1010
if verbose
1111
puts "📦 Generating React on Rails packs..."
12-
success = system "bundle exec rake react_on_rails:generate_packs"
12+
success = run_pack_generation
1313
else
1414
print "📦 Generating packs... "
15-
success = system "bundle exec rake react_on_rails:generate_packs > /dev/null 2>&1"
15+
success = run_pack_generation(silent: true)
1616
puts success ? "✅" : "❌"
1717
end
1818

@@ -21,6 +21,63 @@ def generate(verbose: false)
2121
puts "❌ Pack generation failed"
2222
exit 1
2323
end
24+
25+
private
26+
27+
def run_pack_generation(silent: false)
28+
# If we're already inside a Bundler context AND Rails is available (e.g., called from bin/dev),
29+
# we can directly require and run the task. Otherwise, use bundle exec.
30+
if defined?(Bundler) && rails_available?
31+
run_rake_task_directly(silent: silent)
32+
else
33+
run_via_bundle_exec(silent: silent)
34+
end
35+
end
36+
37+
def rails_available?
38+
return false unless defined?(Rails)
39+
return false unless Rails.respond_to?(:application)
40+
return false if Rails.application.nil?
41+
42+
true
43+
end
44+
45+
def run_rake_task_directly(silent: false)
46+
require "rake"
47+
48+
# Load tasks only if not already loaded (don't clear all tasks)
49+
Rails.application.load_tasks unless Rake::Task.task_defined?("react_on_rails:generate_packs")
50+
51+
if silent
52+
original_stdout = $stdout
53+
original_stderr = $stderr
54+
$stdout = StringIO.new
55+
$stderr = StringIO.new
56+
end
57+
58+
begin
59+
task = Rake::Task["react_on_rails:generate_packs"]
60+
task.reenable # Allow re-execution if called multiple times
61+
task.invoke
62+
true
63+
rescue StandardError => e
64+
warn "Error generating packs: #{e.message}" unless silent
65+
false
66+
ensure
67+
if silent
68+
$stdout = original_stdout
69+
$stderr = original_stderr
70+
end
71+
end
72+
end
73+
74+
def run_via_bundle_exec(silent: false)
75+
if silent
76+
system "bundle exec rake react_on_rails:generate_packs > /dev/null 2>&1"
77+
else
78+
system "bundle exec rake react_on_rails:generate_packs"
79+
end
80+
end
2481
end
2582
end
2683
end

0 commit comments

Comments
 (0)