Skip to content

Commit b29ba69

Browse files
justin808claude
andcommitted
Fix using_swc? to properly parse YAML and default to babel
Change using_swc? helper to parse shakapacker.yml with YAML.safe_load_file instead of raw string matching, which incorrectly interpreted commented lines as active configuration. Changes: - Add require "yaml" to base_generator.rb - Parse YAML to read javascript_compiler from default section - Return true only when javascript_compiler is explicitly set to "swc" - Default to false (babel) when file is missing or key is absent - Add error handling for YAML parsing failures - Update template comment to reflect babel as default This ensures commented configuration lines don't affect compiler detection and provides more predictable behavior. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 34f3410 commit b29ba69

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

lib/generators/react_on_rails/base_generator.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "rails/generators"
44
require "fileutils"
5+
require "yaml"
56
require_relative "generator_messages"
67
require_relative "generator_helper"
78
module ReactOnRails
@@ -232,13 +233,21 @@ def add_dev_dependencies
232233

233234
def using_swc?
234235
# Check shakapacker.yml for javascript_compiler setting
235-
# Shakapacker 9.3.0 defaults to SWC if not specified
236+
# Parse YAML to properly handle comments and get actual configured value
236237
shakapacker_config_path = File.join(destination_root, "config", "shakapacker.yml")
237-
return true unless File.exist?(shakapacker_config_path)
238-
239-
config_content = File.read(shakapacker_config_path)
240-
# Return true (SWC) unless explicitly set to babel
241-
!config_content.include?("javascript_compiler: babel")
238+
return false unless File.exist?(shakapacker_config_path)
239+
240+
begin
241+
config = YAML.safe_load_file(shakapacker_config_path)
242+
# Check default section for javascript_compiler setting
243+
javascript_compiler = config.dig("default", "javascript_compiler")
244+
# Return true only if explicitly set to "swc"
245+
javascript_compiler == "swc"
246+
rescue StandardError => e
247+
# If YAML parsing fails, default to babel (false)
248+
puts "Warning: Could not parse shakapacker.yml: #{e.message}"
249+
false
250+
end
242251
end
243252

244253
def install_js_dependencies

lib/generators/react_on_rails/templates/base/base/config/shakapacker.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ default: &default
5858

5959
# Compiler to use for JavaScript: 'babel' or 'swc'
6060
# SWC is faster but Babel has more plugins and wider ecosystem support
61-
# Shakapacker 9.3.0+ defaults to 'swc' if not specified
62-
# Uncomment to use Babel instead:
63-
# javascript_compiler: babel
61+
# React on Rails defaults to Babel if not specified for wider compatibility
62+
# Uncomment and set to 'swc' for faster compilation:
63+
# javascript_compiler: swc
6464

6565
# Setting the asset host here will override Rails.application.config.asset_host.
6666
# Here, you can set different asset_host per environment. Note that

0 commit comments

Comments
 (0)