Skip to content

Install @babel/preset-react for non-SWC generator installs#2421

Merged
justin808 merged 4 commits intomasterfrom
codex/fix-2281-babel-preset-react-dep
Mar 10, 2026
Merged

Install @babel/preset-react for non-SWC generator installs#2421
justin808 merged 4 commits intomasterfrom
codex/fix-2281-babel-preset-react-dep

Conversation

@justin808
Copy link
Copy Markdown
Member

@justin808 justin808 commented Feb 15, 2026

Summary

  • Add BABEL_REACT_DEPENDENCIES to the generator dependency manager
  • Install @babel/preset-react as a dev dependency when SWC is not the active transpiler
  • Keep SWC path unchanged (no Babel preset installed when SWC is used)
  • Extend generator specs to verify:
    • Babel preset is included for non-SWC setups
    • Babel preset is skipped for SWC setups
    • install generator output includes @babel/preset-react for older Shakapacker/Babel defaults

Closes #2281

Test plan

  • Proposed fix (UNTESTED in this environment)
  • Intended Ruby checks:
    • bundle exec rspec react_on_rails/spec/react_on_rails/generators/js_dependency_manager_spec.rb
    • bundle exec rspec react_on_rails/spec/react_on_rails/generators/install_generator_spec.rb
  • Blocked by local Ruby version (2.6.10) vs project requirement (>= 3.0.0)

Summary by CodeRabbit

  • New Features

    • Installer now adds the Babel React preset for projects that do not use the SWC transpiler (and skips it when SWC or rspack is active), with user-facing warnings and fallback guidance if installation fails.
  • Tests

    • Expanded test coverage for conditional Babel preset installation, omission when SWC/rspack are used, and warning/error handling during installation.

Note

Low Risk
Small, generator-only dependency change with expanded test coverage; main risk is unintended package.json output differences for non-SWC installs.

Overview
Ensures generator installs @babel/preset-react as a dev dependency for non-SWC setups, while keeping the existing SWC path unchanged.

JsDependencyManager#add_js_dependencies now conditionally installs either SWC_DEPENDENCIES or the new BABEL_REACT_DEPENDENCIES, with matching warning/"install manually" messaging on failure. Specs are updated to assert the new dependency behavior (including install_generator_spec expectations for older Shakapacker/Babel defaults) and to track multiple add_npm_dependencies calls for verification.

Written by Cursor Bugbot for commit 1e0da86. Configure here.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Feb 15, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 821c9a74-50b4-4700-8b45-1be31ced3649

📥 Commits

Reviewing files that changed from the base of the PR and between 1e0da86 and 43f6648.

📒 Files selected for processing (2)
  • react_on_rails/lib/generators/react_on_rails/js_dependency_manager.rb
  • react_on_rails/spec/react_on_rails/generators/js_dependency_manager_spec.rb

Walkthrough

The generator now installs @babel/preset-react for non‑SWC transpiler configurations via a new constant and helper methods; SWC installation flow remains intact and @babel/preset-react is skipped when SWC or rspack is used.

Changes

Cohort / File(s) Summary
Core Dependency Manager
react_on_rails/lib/generators/react_on_rails/js_dependency_manager.rb
Added BABEL_REACT_DEPENDENCIES constant, add_transpiler_dependencies and add_babel_react_dependencies methods; add_js_dependencies now delegates transpiler-specific installs to add_transpiler_dependencies to add @babel/preset-react only when SWC is not active and rspack is not used.
Generator spec (integration)
react_on_rails/spec/react_on_rails/generators/install_generator_spec.rb
Updated test expectations for older Shakapacker/non‑SWC scenarios to require @babel/preset-react in devDependencies and adjusted comments to reflect Babel preset requirement.
Dependency manager unit tests
react_on_rails/spec/react_on_rails/generators/js_dependency_manager_spec.rb
Adjusted test double signature for add_npm_dependencies(packages, dev: false), exposed call history via add_npm_dependencies_calls, added tests for BABEL_REACT_DEPENDENCIES, add_babel_react_dependencies behavior and warning paths, and expanded add_js_dependencies coverage for SWC/rspack branches.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I sniffed the build and found a missing treat,
A little preset hidden where Babel and webpack meet,
I planted @babel/preset-react when SWC sleeps tight,
Now builds wake cheerful in the morning light,
Hoppity‑hop—no more missing-package fright.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: installing @babel/preset-react for non-SWC generator installs, which directly addresses the PR's primary objective.
Linked Issues check ✅ Passed All core requirements from issue #2281 are met: @babel/preset-react is installed for non-SWC configurations, skipped for SWC/rspack, babel.config.js is properly supported, and fresh installs will work without webpack failures.
Out of Scope Changes check ✅ Passed All changes are directly related to issue #2281: new Babel preset handling in js_dependency_manager.rb, updated specs to verify Babel vs SWC behavior, and install generator spec updates to confirm preset inclusion for older Shakapacker versions.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch codex/fix-2281-babel-preset-react-dep

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Feb 15, 2026

Greptile Summary

This PR fixes a missing dependency issue where the generator creates a babel.config.js file that requires @babel/preset-react, but wasn't installing it for non-SWC setups.

Key Changes:

  • Adds BABEL_REACT_DEPENDENCIES constant containing @babel/preset-react
  • Creates new add_babel_react_dependencies method following the existing error handling pattern
  • Updates add_js_dependencies to conditionally install either SWC dependencies OR Babel preset based on using_swc? check
  • Extends test coverage to verify the preset is installed for Babel setups and skipped for SWC setups
  • Adds add_npm_dependencies_calls tracking to test mock for better assertion capabilities

Why This Matters:
The generated babel.config.js template references @babel/preset-react on line 11, which would cause build failures if the package isn't installed. This fix ensures the dependency is present when needed.

Confidence Score: 5/5

  • This PR is safe to merge with no identified risks
  • The implementation follows established patterns in the codebase, includes comprehensive test coverage, and addresses a legitimate bug where generated files would reference missing dependencies. The conditional logic properly handles both SWC and Babel paths without breaking existing functionality.
  • No files require special attention

Important Files Changed

Filename Overview
react_on_rails/lib/generators/react_on_rails/js_dependency_manager.rb Adds BABEL_REACT_DEPENDENCIES constant and add_babel_react_dependencies method, installs @babel/preset-react for non-SWC setups
react_on_rails/spec/react_on_rails/generators/install_generator_spec.rb Adds test assertion to verify @babel/preset-react is installed for older Shakapacker/Babel configurations
react_on_rails/spec/react_on_rails/generators/js_dependency_manager_spec.rb Adds comprehensive tests for BABEL_REACT_DEPENDENCIES, add_babel_react_dependencies, and conditional Babel preset installation logic

Flowchart

flowchart TD
    A[Generator starts] --> B[add_js_dependencies called]
    B --> C{using_swc?}
    C -->|true| D[add_swc_dependencies]
    C -->|false| E[add_babel_react_dependencies]
    D --> F[Install @swc/core and swc-loader as devDependencies]
    E --> G[Install @babel/preset-react as devDependency]
    F --> H[Continue with other dependencies]
    G --> H
    H --> I[babel.config.js template requires @babel/preset-react]
    I --> J{SWC path?}
    J -->|Yes| K[No babel.config.js needed - SWC handles transpilation]
    J -->|No| L[babel.config.js uses @babel/preset-react preset]
Loading

Last reviewed commit: ff49925

@justin808 justin808 added codex PRs created from codex-named branches release:16.4.0-must-have Must-have for 16.4.0: critical bug/perf/usability labels Feb 25, 2026
@justin808 justin808 added the P2 Backlog priority label Mar 5, 2026
@justin808 justin808 self-assigned this Mar 5, 2026
@justin808 justin808 added the bug label Mar 8, 2026
@justin808 justin808 force-pushed the codex/fix-2281-babel-preset-react-dep branch from a66cc1e to 1e0da86 Compare March 9, 2026 06:50
@claude
Copy link
Copy Markdown
Contributor

claude Bot commented Mar 9, 2026

PR top-level review: see inline comments below for specific line-level feedback.

Summary of issues found:

  1. RSPACK BUG: Rspack users will incorrectly get @babel/preset-react installed. The condition only checks using_swc? but not using_rspack?. Rspack has its own built-in SWC and does not use babel.config.js at all. The guard in add_js_dependencies should be changed from the current if/else to: add_swc_dependencies if using_swc? followed by add_babel_react_dependencies if !using_swc? && !using_rspack?

  2. MISSING TEST: The rescue StandardError branch in add_babel_react_dependencies has no test coverage, unlike every other add_* method in the module which tests both the return-false and the exception paths.

  3. UNTESTED: PR is self-described as untested locally due to a Ruby version mismatch. Since this changes installer behavior for all non-SWC non-rspack users, verifying specs pass before merge is important.

  4. MINOR: A comment on BABEL_REACT_DEPENDENCIES explaining why only the preset is needed (Shakapacker already handles babel-loader and @babel/core for Babel setups) would help future maintainers.

Comment thread react_on_rails/lib/generators/react_on_rails/js_dependency_manager.rb Outdated

def add_transpiler_dependencies
add_swc_dependencies if using_swc?
add_babel_react_dependencies if !using_swc? && !using_rspack?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: esbuild transpiler installs @babel/preset-react incorrectly

using_swc? returns false for any non-SWC transpiler, including esbuild and none. If a user has javascript_transpiler: 'esbuild' in their shakapacker.yml, this condition will incorrectly install @babel/preset-react, which esbuild doesn't use.

The intent appears to be "install only when Babel is the active transpiler", not "install whenever SWC isn't". Consider adding a using_babel? helper in GeneratorHelper that explicitly checks for transpiler == "babel", or guard this with a check specifically for the babel case:

Suggested change
add_babel_react_dependencies if !using_swc? && !using_rspack?
add_babel_react_dependencies if using_babel?

Where using_babel? would be something like:

def using_babel?
  !using_swc? && !using_rspack? && transpiler_is_babel?
end

This also avoids the double-negative condition which is harder to read at a glance.

MSG
end

def add_babel_react_dependencies
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider whether @babel/core is missing from BABEL_REACT_DEPENDENCIES

@babel/preset-react requires @babel/core as a peer dependency. This works today because Shakapacker (when configured with Babel) already installs @babel/core as a transitive dependency. However, this is an implicit coupling — if a user installs @babel/preset-react and their Shakapacker version does not pull in @babel/core automatically, they'll get a confusing peer dependency warning.

Worth a brief comment explaining why @babel/core is intentionally omitted (i.e., "Shakapacker's babel-loader already provides @babel/core").

expect(babel_calls).to eq([])
end

it "does not add Babel React preset when rspack is used and SWC is not configured" do
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test case: esbuild transpiler

There's a test for the rspack case, but there's no test verifying the behavior when javascript_transpiler: 'esbuild' is in use. Given the bug noted on add_transpiler_dependencies — where using_swc? == false for esbuild — adding a spec here would both document the expected behavior and catch the regression:

it "does not add Babel React preset when esbuild is used" do
  instance.using_swc = false
  # simulate esbuild: currently the code would incorrectly install @babel/preset-react
  # This test documents the expected (correct) behavior once the guard is fixed
  ...
end

@claude
Copy link
Copy Markdown
Contributor

claude Bot commented Mar 9, 2026

Review Summary

The direction is correct — fixing #2281 by explicitly installing @babel/preset-react for Babel-based setups is the right approach. The test coverage for the new method is good. However there is one meaningful correctness issue to address before merging.

Bug: esbuild transpiler incorrectly triggers Babel preset installation

The condition in add_transpiler_dependencies relies on the assumption that 'not SWC and not rspack' means 'Babel'. But using_swc? returns false for any non-SWC transpiler, including esbuild and none. A user with javascript_transpiler: 'esbuild' in their shakapacker.yml will have @babel/preset-react unnecessarily installed into their project. Inline comment posted with details.

Other notes:

  • @babel/core not listed in BABEL_REACT_DEPENDENCIES — works because Shakapacker's babel-loader brings it in transitively, but a brief comment explaining the omission would help future readers.
  • Tests are unrun by the author (acknowledged in the PR) — the specs look structurally sound but should be validated in CI before merge. The PR notes it is blocked by a local Ruby version mismatch.
  • Minor: the BABEL_REACT_DEPENDENCIES constant for a single package is slightly over-engineered but is consistent with the module's existing patterns.

@justin808 justin808 merged commit 130f100 into master Mar 10, 2026
27 checks passed
@justin808 justin808 deleted the codex/fix-2281-babel-preset-react-dep branch March 10, 2026 02:42
justin808 added a commit that referenced this pull request Mar 10, 2026
- Stamp version header for 16.4.0.rc.7
- Collapse rc.6 section into rc.7 (single prerelease section)
- Add new entries: #2418 (CSP nonce for RSC), #2421 (babel preset),
  #2581 (doctor false positives)
- Remove duplicate PR 2407 entry (appeared in both sections)
- Update version diff links

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug codex PRs created from codex-named branches P2 Backlog priority release:16.4.0-must-have Must-have for 16.4.0: critical bug/perf/usability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant