Skip to content

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

Open
justin808 wants to merge 3 commits intomasterfrom
codex/fix-2281-babel-preset-react-dep
Open

Install @babel/preset-react for non-SWC generator installs#2421
justin808 wants to merge 3 commits intomasterfrom
codex/fix-2281-babel-preset-react-dep

Conversation

@justin808
Copy link
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

    • Dependency management now installs the Babel React preset when the SWC transpiler is not active, with user-facing warnings and fallback instructions if installation fails.
  • Tests

    • Expanded test coverage to verify conditional Babel React installation, warning behavior, and absence when SWC is used.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 15, 2026

Walkthrough

The generator now installs @babel/preset-react for non-SWC setups by adding a new constant and code path that conditionally adds the Babel React preset when SWC is not the active transpiler.

Changes

Cohort / File(s) Summary
Core Dependency Manager Implementation
react_on_rails/lib/generators/react_on_rails/js_dependency_manager.rb
Added BABEL_REACT_DEPENDENCIES constant and a private add_babel_react_dependencies path; changed control flow to install Babel React preset only when SWC is not used.
Generator Integration Test
react_on_rails/spec/react_on_rails/generators/install_generator_spec.rb
Updated test to expect @babel/preset-react in devDependencies for older Shakapacker (non-SWC) cases.
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, added assertions for BABEL_REACT_DEPENDENCIES, and expanded tests for the new Babel-specific add path and its warning/error behavior.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I hopped through packages, sniffed the stack so neat,
Found Babel's missing preset and made the fix complete,
When SWC skips JSX, now @babel/preset-react lands,
No broken builds, just tidy rails and happy hands. ✨📦

🚥 Pre-merge checks | ✅ 5 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: installing @babel/preset-react for non-SWC generator configurations, which aligns with the core objective in the PR.
Linked Issues check ✅ Passed The PR successfully implements all coding requirements from issue #2281: adds BABEL_REACT_DEPENDENCIES constant, conditionally installs @babel/preset-react for non-SWC configs, skips it for SWC, and extends specs to verify both paths.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the issue objective: dependency manager updates, new Babel preset handling logic, and test coverage for both SWC and non-SWC scenarios without extraneous modifications.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into master

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 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

No actionable comments were generated in the recent review. 🎉


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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codex PRs created from codex-named branches 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