Skip to content

Commit 5682b3d

Browse files
justin808claude
andcommitted
Simplify generator template precompile hook to minimal bash script
Reverted from the hacky bash grep approach back to a clean Ruby script. The bash version had several problems: 1. Hacky grep parsing to detect config - fragile and error-prone 2. Duplicates logic that already exists in PacksGenerator 3. More complex (20+ lines vs 21 lines) 4. Harder to maintain The Ruby version is superior because: 1. Leverages PacksGenerator.generate_packs_if_stale which already knows when to generate vs skip 2. Simple and clean - just load Rails and call the method 3. Proper error handling with backtrace 4. Let the engine's built-in validation logic handle skipping The engine already skips validation appropriately via: - package_json_missing? check - running_generator? check So we don't need REACT_ON_RAILS_SKIP_VALIDATION here either. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent d584749 commit 5682b3d

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed
Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
#!/bin/sh
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
24
# Shakapacker precompile hook for React on Rails
35
#
46
# This script runs before webpack compilation to generate pack files
5-
# for auto-bundled components (if configured).
7+
# for auto-bundled components. It's called automatically by Shakapacker
8+
# when configured in config/shakapacker.yml:
9+
# precompile_hook: 'bin/shakapacker-precompile-hook'
610
#
711
# See: https://github.com/shakacode/shakapacker/blob/main/docs/precompile_hook.md
812

9-
set -e
13+
require_relative "../config/environment"
1014

11-
# Generate packs if React on Rails is configured with auto_load_bundle
12-
if [ -f "config/initializers/react_on_rails.rb" ]; then
13-
# Check for uncommented config lines (ignore lines starting with # after optional whitespace)
14-
if grep -E "^[[:space:]]*[^#]*config\.auto_load_bundle[[:space:]]*=" config/initializers/react_on_rails.rb >/dev/null || \
15-
grep -E "^[[:space:]]*[^#]*config\.components_subdirectory[[:space:]]*=" config/initializers/react_on_rails.rb >/dev/null; then
16-
echo "📦 Generating React on Rails packs..."
17-
bundle exec rails react_on_rails:generate_packs
18-
echo "✅ Pack generation completed successfully"
19-
fi
20-
fi
15+
begin
16+
ReactOnRails::PacksGenerator.instance.generate_packs_if_stale
17+
rescue StandardError => e
18+
warn "❌ Error in precompile hook: #{e.message}"
19+
warn e.backtrace.first(5).join("\n")
20+
exit 1
21+
end

0 commit comments

Comments
 (0)