Skip to content

Generator fails on Shakapacker < 9.3.0: missing @babel/preset-react dependency #2281

@ihabadham

Description

@ihabadham

Environment

  1. Ruby version: 3.3.7
  2. Rails version: 8.0.4
  3. Shakapacker version: 8.4.0 (any version < 9.3.0)
  4. React on Rails version: 16.2.0-rc.0

Summary

The react_on_rails:install generator fails on fresh installations when using Shakapacker < 9.3.0 because it creates a babel.config.js that requires @babel/preset-react, but doesn't install that dependency.

Expected behavior

Running rails generate react_on_rails:install should produce a working application that builds successfully with webpack.

Actual behavior

Webpack build fails with:

Error: Cannot find package '@babel/preset-react' imported from babel-virtual-resolve-base.js

Root Cause

PR #2051 removed @babel/preset-react from the generator's installed dependencies with the intent that:

  1. SWC is now the default transpiler (has built-in JSX support)
  2. Babel users should install presets manually

However, this creates issues because:

  1. Shakapacker < 9.3.0 uses Babel by default (not SWC)
  2. The generator still creates babel.config.js that requires @babel/preset-react:
    // Generated babel.config.js line 11
    '@babel/preset-react',
  3. The dependency is not installed, so webpack fails

Why CI doesn't catch this

The dummy app (spec/dummy) has @babel/preset-react pre-installed in its package.json (legacy from before PR #2051). CI tests against the existing dummy app, not a fresh generator output.

Steps to Reproduce

rails new test_app --minimal
cd test_app

# Add to Gemfile:
# gem "react_on_rails"
# gem "shakapacker", "~> 8.2"

bundle install
git init && git add . && git commit -m "Initial"

rails generate react_on_rails:install

# Start the app
foreman start -f Procfile.dev
# -> Webpack fails with "Cannot find package '@babel/preset-react'"

Proposed Fix

Install @babel/preset-react when NOT using SWC:

# js_dependency_manager.rb
BABEL_REACT_DEPENDENCIES = %w[
  @babel/preset-react
].freeze

def add_js_dependencies
  # ... existing code ...
  add_babel_react_dependencies unless using_swc?
  # ...
end

This aligns with:

  • Shakapacker's own documentation (docs/react.md tells users to npm install @babel/preset-react)
  • The gemspec supporting Shakapacker >= 6.0
  • CI testing against Shakapacker 8.2.0 as minimum

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions