|
1 | | -#!/usr/bin/env ruby |
2 | | -# frozen_string_literal: true |
3 | | - |
4 | | -# Shakapacker precompile hook |
5 | | -# This script runs before Shakapacker compilation in both development and production. |
| 1 | +#!/bin/sh |
| 2 | +# Shakapacker precompile hook for React on Rails |
| 3 | +# |
| 4 | +# This script runs before webpack compilation to: |
| 5 | +# 1. Build ReScript files (if configured) |
| 6 | +# 2. Generate pack files for auto-bundled components |
| 7 | +# |
| 8 | +# It's called automatically by Shakapacker when configured in config/shakapacker.yml: |
| 9 | +# precompile_hook: 'bin/shakapacker-precompile-hook' |
| 10 | +# |
6 | 11 | # See: https://github.com/shakacode/shakapacker/blob/main/docs/precompile_hook.md |
7 | 12 |
|
8 | | -require "fileutils" |
| 13 | +set -e |
9 | 14 |
|
10 | 15 | # Find Rails root by walking upward looking for config/environment.rb |
11 | | -def find_rails_root |
12 | | - dir = Dir.pwd |
13 | | - loop do |
14 | | - return dir if File.exist?(File.join(dir, "config", "environment.rb")) |
15 | | - |
16 | | - parent = File.dirname(dir) |
17 | | - return nil if parent == dir # Reached filesystem root |
18 | | - |
19 | | - dir = parent |
20 | | - end |
21 | | -end |
| 16 | +find_rails_root() { |
| 17 | + dir="$PWD" |
| 18 | + while [ "$dir" != "/" ]; do |
| 19 | + if [ -f "$dir/config/environment.rb" ]; then |
| 20 | + echo "$dir" |
| 21 | + return 0 |
| 22 | + fi |
| 23 | + dir=$(dirname "$dir") |
| 24 | + done |
| 25 | + return 1 |
| 26 | +} |
22 | 27 |
|
23 | 28 | # Build ReScript if needed |
24 | | -def build_rescript_if_needed |
25 | | - # Find Rails root directory |
26 | | - rails_root = find_rails_root |
27 | | - unless rails_root |
28 | | - warn "⚠️ Warning: Could not find Rails root. Skipping ReScript build." |
29 | | - return |
30 | | - end |
| 29 | +build_rescript_if_needed() { |
| 30 | + rails_root=$(find_rails_root) |
| 31 | + if [ -z "$rails_root" ]; then |
| 32 | + echo "⚠️ Warning: Could not find Rails root. Skipping ReScript build." |
| 33 | + return 0 |
| 34 | + fi |
31 | 35 |
|
32 | 36 | # Check for both old (bsconfig.json) and new (rescript.json) config files |
33 | | - bsconfig_path = File.join(rails_root, "bsconfig.json") |
34 | | - rescript_config_path = File.join(rails_root, "rescript.json") |
35 | | - return unless File.exist?(bsconfig_path) || File.exist?(rescript_config_path) |
| 37 | + if [ ! -f "$rails_root/bsconfig.json" ] && [ ! -f "$rails_root/rescript.json" ]; then |
| 38 | + return 0 |
| 39 | + fi |
| 40 | + |
| 41 | + echo "🔧 Building ReScript..." |
36 | 42 |
|
37 | | - puts "🔧 Building ReScript..." |
| 43 | + # Change to Rails root to run build commands |
| 44 | + cd "$rails_root" || return 1 |
38 | 45 |
|
39 | 46 | # Cross-platform package manager detection |
40 | | - yarn_available = system("yarn", "--version", out: File::NULL, err: File::NULL) |
41 | | - npm_available = system("npm", "--version", out: File::NULL, err: File::NULL) |
42 | | - |
43 | | - # Run build command from Rails root directory |
44 | | - success = Dir.chdir(rails_root) do |
45 | | - if yarn_available |
46 | | - system("yarn", "build:rescript") |
47 | | - elsif npm_available |
48 | | - system("npm", "run", "build:rescript") |
| 47 | + if command -v yarn >/dev/null 2>&1; then |
| 48 | + if yarn build:rescript; then |
| 49 | + echo "✅ ReScript build completed successfully" |
| 50 | + return 0 |
49 | 51 | else |
50 | | - warn "⚠️ Warning: Neither yarn nor npm found. Skipping ReScript build." |
51 | | - return false |
52 | | - end |
53 | | - end |
54 | | - |
55 | | - if success |
56 | | - puts "✅ ReScript build completed successfully" |
| 52 | + echo "❌ ReScript build failed" >&2 |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | + elif command -v npm >/dev/null 2>&1; then |
| 56 | + if npm run build:rescript; then |
| 57 | + echo "✅ ReScript build completed successfully" |
| 58 | + return 0 |
| 59 | + else |
| 60 | + echo "❌ ReScript build failed" >&2 |
| 61 | + exit 1 |
| 62 | + fi |
57 | 63 | else |
58 | | - warn "❌ ReScript build failed" |
59 | | - exit 1 |
60 | | - end |
61 | | -end |
| 64 | + echo "⚠️ Warning: Neither yarn nor npm found. Skipping ReScript build." |
| 65 | + return 0 |
| 66 | + fi |
| 67 | +} |
62 | 68 |
|
63 | 69 | # Generate React on Rails packs if needed |
64 | | -def generate_packs_if_needed |
65 | | - # Find Rails root directory |
66 | | - rails_root = find_rails_root |
67 | | - return unless rails_root |
| 70 | +generate_packs_if_needed() { |
| 71 | + rails_root=$(find_rails_root) |
| 72 | + if [ -z "$rails_root" ]; then |
| 73 | + return 0 |
| 74 | + fi |
68 | 75 |
|
69 | 76 | # Check if React on Rails initializer exists |
70 | | - initializer_path = File.join(rails_root, "config", "initializers", "react_on_rails.rb") |
71 | | - return unless File.exist?(initializer_path) |
72 | | - |
73 | | - # Check if auto-pack generation is configured (match actual config assignments, not comments) |
74 | | - config_file = File.read(initializer_path) |
75 | | - # Match uncommented configuration lines only (lines not starting with #) |
76 | | - has_auto_load = config_file =~ /^\s*(?!#).*config\.auto_load_bundle\s*=/ |
77 | | - has_components_subdir = config_file =~ /^\s*(?!#).*config\.components_subdirectory\s*=/ |
78 | | - return unless has_auto_load || has_components_subdir |
79 | | - |
80 | | - puts "📦 Generating React on Rails packs..." |
81 | | - |
82 | | - # Cross-platform bundle availability check |
83 | | - bundle_available = system("bundle", "--version", out: File::NULL, err: File::NULL) |
84 | | - return unless bundle_available |
85 | | - |
86 | | - # Check if rake task exists (use array form for security) |
87 | | - task_list = IO.popen(["bundle", "exec", "rails", "-T"], err: %i[child out], &:read) |
88 | | - return unless task_list.include?("react_on_rails:generate_packs") |
89 | | - |
90 | | - # Use array form for better cross-platform support |
91 | | - success = system("bundle", "exec", "rails", "react_on_rails:generate_packs") |
92 | | - |
93 | | - if success |
94 | | - puts "✅ Pack generation completed successfully" |
| 77 | + initializer_path="$rails_root/config/initializers/react_on_rails.rb" |
| 78 | + if [ ! -f "$initializer_path" ]; then |
| 79 | + return 0 |
| 80 | + fi |
| 81 | + |
| 82 | + # Check if auto-pack generation is configured (ignore comments) |
| 83 | + # Look for uncommented config.auto_load_bundle or config.components_subdirectory |
| 84 | + if ! grep -q "^[[:space:]]*config\.auto_load_bundle[[:space:]]*=" "$initializer_path" && \ |
| 85 | + ! grep -q "^[[:space:]]*config\.components_subdirectory[[:space:]]*=" "$initializer_path"; then |
| 86 | + return 0 |
| 87 | + fi |
| 88 | + |
| 89 | + echo "📦 Generating React on Rails packs..." |
| 90 | + |
| 91 | + # Check if bundle is available |
| 92 | + if ! command -v bundle >/dev/null 2>&1; then |
| 93 | + return 0 |
| 94 | + fi |
| 95 | + |
| 96 | + # Change to Rails root |
| 97 | + cd "$rails_root" || return 1 |
| 98 | + |
| 99 | + # Check if rake task exists |
| 100 | + if ! bundle exec rails -T | grep -q "react_on_rails:generate_packs"; then |
| 101 | + return 0 |
| 102 | + fi |
| 103 | + |
| 104 | + # Skip validation during precompile hook execution |
| 105 | + # The hook runs early in the build process and doesn't need package version validation |
| 106 | + export REACT_ON_RAILS_SKIP_VALIDATION=true |
| 107 | + |
| 108 | + # Run pack generation |
| 109 | + if bundle exec rails react_on_rails:generate_packs; then |
| 110 | + echo "✅ Pack generation completed successfully" |
| 111 | + return 0 |
95 | 112 | else |
96 | | - warn "❌ Pack generation failed" |
| 113 | + echo "❌ Pack generation failed" >&2 |
97 | 114 | exit 1 |
98 | | - end |
99 | | -end |
| 115 | + fi |
| 116 | +} |
100 | 117 |
|
101 | 118 | # Main execution |
102 | | -begin |
103 | | - build_rescript_if_needed |
104 | | - generate_packs_if_needed |
105 | | - |
106 | | - exit 0 |
107 | | -rescue StandardError => e |
108 | | - warn "❌ Precompile hook failed: #{e.message}" |
109 | | - warn e.backtrace.join("\n") |
110 | | - exit 1 |
111 | | -end |
| 119 | +build_rescript_if_needed |
| 120 | +generate_packs_if_needed |
| 121 | + |
| 122 | +exit 0 |
0 commit comments