Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions react_on_rails/lib/generators/react_on_rails/base_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,21 +298,22 @@ def configure_rspack_in_shakapacker

puts Rainbow("🔧 Configuring Shakapacker for Rspack...").yellow

# Parse YAML config properly to avoid fragile regex manipulation
# Support both old and new Psych versions
config = begin
YAML.load_file(shakapacker_config_path, aliases: true)
rescue ArgumentError
# Older Psych versions don't support the aliases parameter
YAML.load_file(shakapacker_config_path)
end
# Update default section
config["default"] ||= {}
config["default"]["assets_bundler"] = "rspack"
config["default"]["webpack_loader"] = "swc"
# Use regex replacement to preserve file structure (comments, anchors, aliases)
# This replaces ALL occurrences of assets_bundler, not just in default section
# Using gsub_file (Thor method) for consistency with Rails generator patterns
gsub_file(
shakapacker_config_path,
/^(\s*assets_bundler:\s*)["']?webpack["']?(\s*(?:#.*)?)$/,
'\1rspack\2'
)

# Update webpack_loader to swc (rspack works best with SWC)
gsub_file(
shakapacker_config_path,
/^(\s*webpack_loader:\s*)["']?babel["']?(\s*(?:#.*)?)$/,
'\1swc\2'
)

# Write back as YAML
File.write(shakapacker_config_path, YAML.dump(config))
puts Rainbow("✅ Updated shakapacker.yml for Rspack").green
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# frozen_string_literal: true

require "fileutils"
require "yaml"
require "json"

# Script to switch between webpack and rspack bundlers
Expand Down Expand Up @@ -48,16 +47,25 @@ class BundlerSwitcher
abort "❌ #{@shakapacker_config} not found" unless File.exist?(@shakapacker_config)

puts "📝 Updating #{@shakapacker_config}..."
config = YAML.load_file(@shakapacker_config)

config["default"] ||= {}
config["default"]["assets_bundler"] = @target_bundler

# Update webpack_loader based on bundler
# Rspack works best with SWC, webpack typically uses babel
config["default"]["webpack_loader"] = @target_bundler == "rspack" ? "swc" : "babel"

File.write(@shakapacker_config, YAML.dump(config))
content = File.read(@shakapacker_config)

# Use regex replacement to preserve file structure (comments, anchors, aliases)
# This replaces ALL occurrences of assets_bundler, not just in default section
old_bundler = @target_bundler == "rspack" ? "webpack" : "rspack"
new_content = content.gsub(
/^(\s*assets_bundler:\s*)["']?#{old_bundler}["']?(\s*(?:#.*)?)$/,
"\\1#{@target_bundler}\\2"
)

# Update webpack_loader based on bundler (rspack works best with SWC)
new_loader = @target_bundler == "rspack" ? "swc" : "babel"
old_loader = @target_bundler == "rspack" ? "babel" : "swc"
new_content = new_content.gsub(
/^(\s*webpack_loader:\s*)["']?#{old_loader}["']?(\s*(?:#.*)?)$/,
"\\1#{new_loader}\\2"
)

File.write(@shakapacker_config, new_content)
puts "✅ Updated assets_bundler to '#{@target_bundler}'"
end

Expand Down
Loading