Skip to content

Generator doesn't configure precompile_hook in shakapacker.yml when Shakapacker is pre-installed #2278

@ihabadham

Description

@ihabadham

Environment

  1. Ruby version: 3.3.7
  2. Rails version: 8.0.4
  3. Shakapacker version: 9.4.0
  4. React on Rails version: 16.2.0.rc.0

Summary

When running rails generate react_on_rails:install on a fresh Rails app, the generator creates bin/shakapacker-precompile-hook but doesn't configure it in shakapacker.yml when Shakapacker is installed first (which is the normal flow).

Expected behavior

After running the generator, config/shakapacker.yml should have:

precompile_hook: 'bin/shakapacker-precompile-hook'

This is the intended configuration per PR #1916 commit message:

"This change leverages Shakapacker's new precompile_hook feature to automatically generate packs before webpack compilation, replacing the need to manually modify assets:precompile tasks or bin/dev scripts."

Actual behavior

The generator:

  1. ✅ Creates bin/shakapacker-precompile-hook script
  2. ❌ Leaves precompile_hook: ~ (commented out) in shakapacker.yml
$ ls -la bin/shakapacker-precompile-hook
-rwxr-xr-x 1 user user 1023 Jan 4 18:06 bin/shakapacker-precompile-hook

$ grep precompile_hook config/shakapacker.yml
  # Example: precompile_hook: 'bin/shakapacker-precompile-hook'
  # precompile_hook: ~

Root Cause

In base_generator.rb, when Shakapacker is installed first:

def copy_packer_config
  if File.exist?(".shakapacker_just_installed")
    puts "Skipping Shakapacker config copy..."
    File.delete(".shakapacker_just_installed")
    configure_rspack_in_shakapacker if options.rspack?  # rspack gets configured
    return                                               # but precompile_hook doesn't!
  end
  # ... copies template with precompile_hook configured
end

The generator has configure_rspack_in_shakapacker to modify existing shakapacker.yml for rspack, but no equivalent method for precompile_hook.

Steps to Reproduce

rails new test_app --minimal
cd test_app
# Add react_on_rails to Gemfile
bundle install
rails generate react_on_rails:install
grep precompile_hook config/shakapacker.yml
# Shows: # precompile_hook: ~

Impact

The app still works because bin/dev and adjust_precompile_task have fallback logic. But:

  • The intended/optimized configuration isn't applied
  • Pack generation happens later in the pipeline instead of before webpack
  • Users don't get the benefit of the precompile_hook optimization

Suggested Fix

Add a configure_precompile_hook_in_shakapacker method similar to configure_rspack_in_shakapacker that adds the precompile_hook line to existing shakapacker.yml files.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions