Skip to content

Commit

Permalink
Replace Webpacker with Shakapacker
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangohjw committed May 21, 2024
1 parent e1c1b2d commit f2099d7
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 90 deletions.
12 changes: 4 additions & 8 deletions lib/react_on_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,27 +256,23 @@ def raise_missing_components_subdirectory
end

def shakapacker_precompile?
return Webpacker.config.webpacker_precompile? if ReactOnRails::WebpackerUtils.using_shakapacker_6?

Shakapacker.config.shakapacker_precompile?
end

def shakapacker_clean_task
ReactOnRails::WebpackerUtils.using_shakapacker_6? ? "webpacker:clean" : "shakapacker:clean"
"shakapacker:clean"
end

def compile_command_conflict_message
packer = ReactOnRails::WebpackerUtils.using_shakapacker_6? ? "webpacker" : "shakapacker"

<<~MSG
React on Rails and Shakapacker error in configuration!
In order to use config/react_on_rails.rb config.build_production_command,
you must edit config/#{packer}.yml to include this value in the default configuration:
'#{packer}_precompile: false'
you must edit config/shakapacker.yml to include this value in the default configuration:
'shakapacker_precompile: false'
Alternatively, remove the config/react_on_rails.rb config.build_production_command and the
default bin/#{packer} script will be used for assets:precompile.
default bin/shakapacker script will be used for assets:precompile.
MSG
end
Expand Down
2 changes: 1 addition & 1 deletion lib/react_on_rails/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def self.server_bundle_js_file_path
@server_bundle_path = if ReactOnRails::WebpackerUtils.using_webpacker?
begin
bundle_js_file_path(bundle_name)
rescue Webpacker::Manifest::MissingEntryError
rescue Shakapacker::Manifest::MissingEntryError
File.expand_path(
File.join(ReactOnRails::WebpackerUtils.webpacker_public_output_path,
bundle_name)
Expand Down
35 changes: 14 additions & 21 deletions lib/react_on_rails/webpacker_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ module WebpackerUtils
def self.using_webpacker?
return @using_webpacker if defined?(@using_webpacker)

@using_webpacker = ReactOnRails::Utils.gem_available?("webpacker") ||
ReactOnRails::Utils.gem_available?("shakapacker")
@using_webpacker = ReactOnRails::Utils.gem_available?("shakapacker")
end

def self.dev_server_running?
return false unless using_webpacker?

Webpacker.dev_server.running?
Shakapacker.dev_server.running?
end

def self.shakapacker_version
Expand Down Expand Up @@ -41,51 +40,51 @@ def self.bundle_js_uri_from_webpacker(bundle_name)
# [2] (pry) ReactOnRails::WebpackerUtils: 0> Webpacker.manifest.lookup("app-bundle.js")
# "/webpack/development/app-bundle-c1d2b6ab73dffa7d9c0e.js"
# Next line will throw if the file or manifest does not exist
hashed_bundle_name = Webpacker.manifest.lookup!(bundle_name)
hashed_bundle_name = Shakapacker.manifest.lookup!(bundle_name)

# Support for hashing the server-bundle and having that built
# the webpack-dev-server is provided by the config value
# "same_bundle_for_client_and_server" where a value of true
# would mean that the bundle is created by the webpack-dev-server
is_server_bundle = bundle_name == ReactOnRails.configuration.server_bundle_js_file

if Webpacker.dev_server.running? && (!is_server_bundle ||
if Shakapacker.dev_server.running? && (!is_server_bundle ||
ReactOnRails.configuration.same_bundle_for_client_and_server)
"#{Webpacker.dev_server.protocol}://#{Webpacker.dev_server.host_with_port}#{hashed_bundle_name}"
"#{Shakapacker.dev_server.protocol}://#{Shakapacker.dev_server.host_with_port}#{hashed_bundle_name}"
else
File.expand_path(File.join("public", hashed_bundle_name)).to_s
end
end

def self.webpacker_source_path
Webpacker.config.source_path
Shakapacker.config.source_path
end

def self.webpacker_source_entry_path
Webpacker.config.source_entry_path
Shakapacker.config.source_entry_path
end

def self.nested_entries?
Webpacker.config.nested_entries?
Shakapacker.config.nested_entries?
end

def self.webpacker_public_output_path
# Webpacker has the full absolute path of webpacker output files in a Pathname
Webpacker.config.public_output_path.to_s
# Shakapacker has the full absolute path of webpacker output files in a Pathname
Shakapacker.config.public_output_path.to_s
end

def self.manifest_exists?
Webpacker.config.public_manifest_path.exist?
Shakapacker.config.public_manifest_path.exist?
end

def self.webpacker_source_path_explicit?
# WARNING: Calling private method `data` on Webpacker::Configuration, lib/webpacker/configuration.rb
config_webpacker_yml = Webpacker.config.send(:data)
# WARNING: Calling private method `data` on Shakapacker::Configuration, lib/webpacker/configuration.rb
config_webpacker_yml = Shakapacker.config.send(:data)
config_webpacker_yml[:source_path].present?
end

def self.check_manifest_not_cached
return unless using_webpacker? && Webpacker.config.cache_manifest?
return unless using_webpacker? && Shakapacker.config.cache_manifest?

msg = <<-MSG.strip_heredoc
ERROR: you have enabled cache_manifest in the #{Rails.env} env when using the
Expand Down Expand Up @@ -141,11 +140,5 @@ def self.raise_shakapacker_not_installed
def self.semver_to_string(ary)
"#{ary[0]}.#{ary[1]}.#{ary[2]}"
end

def self.using_shakapacker_6?
shakapacker_major_version = shakapacker_version_as_array[0]

shakapacker_major_version == 6
end
end
end
51 changes: 20 additions & 31 deletions spec/react_on_rails/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module ReactOnRails

before do
allow(Rails).to receive(:root).and_return(File.expand_path("."))
allow(Webpacker).to receive_message_chain("config.public_output_path")
allow(Shakapacker).to receive_message_chain("config.public_output_path")
.and_return(webpacker_public_output_path)
end

Expand Down Expand Up @@ -75,70 +75,59 @@ module ReactOnRails
end

describe ".build_production_command" do
context "when using Shakapacker 6" do
context "when using Shakapacker 7" do
before do
allow(ReactOnRails::WebpackerUtils)
.to receive("shakapacker_version")
.and_return("6.0.0")
.and_return("7.0.0")
end

it "fails when \"webpacker_precompile\" is truly and \"build_production_command\" is truly" do
allow(Webpacker).to receive_message_chain("config.webpacker_precompile?")
it "fails when \"shakapacker_precompile\" is truly and \"build_production_command\" is truly" do
allow(Shakapacker).to receive_message_chain("config.shakapacker_precompile?")
.and_return(true)
expect do
ReactOnRails.configure do |config|
config.build_production_command = "RAILS_ENV=production NODE_ENV=production bin/webpacker"
config.build_production_command = "RAILS_ENV=production NODE_ENV=production bin/shakapacker"
end
end.to raise_error(ReactOnRails::Error, /webpacker_precompile: false/)
end.to raise_error(ReactOnRails::Error, /shakapacker_precompile: false/)
end

it "doesn't fail when \"webpacker_precompile\" is falsy and \"build_production_command\" is truly" do
allow(Webpacker).to receive_message_chain("config.webpacker_precompile?")
it "doesn't fail when \"shakapacker_precompile\" is falsy and \"build_production_command\" is truly" do
allow(Shakapacker).to receive_message_chain("config.shakapacker_precompile?")
.and_return(false)
expect do
ReactOnRails.configure do |config|
config.build_production_command = "RAILS_ENV=production NODE_ENV=production bin/webpacker"
config.build_production_command = "RAILS_ENV=production NODE_ENV=production bin/shakapacker"
end
end.not_to raise_error
end

it "doesn't fail when \"webpacker_precompile\" is truly and \"build_production_command\" is falsy" do
allow(Webpacker).to receive_message_chain("config.webpacker_precompile?")
it "doesn't fail when \"shakapacker_precompile\" is truly and \"build_production_command\" is falsy" do
allow(Shakapacker).to receive_message_chain("config.shakapacker_precompile?")
.and_return(true)
expect do
ReactOnRails.configure {} # rubocop:disable-line Lint/EmptyBlock
end.not_to raise_error
end

it "doesn't fail when \"webpacker_precompile\" is falsy and \"build_production_command\" is falsy" do
allow(Webpacker).to receive_message_chain("config.webpacker_precompile?")
it "doesn't fail when \"shakapacker_precompile\" is falsy and \"build_production_command\" is falsy" do
allow(Shakapacker).to receive_message_chain("config.shakapacker_precompile?")
.and_return(false)
expect do
ReactOnRails.configure {} # rubocop:disable-line Lint/EmptyBlock
end.not_to raise_error
end
end

context "when using Shakapacker 7" do
context "when using Shakapacker 8" do
before do
allow(ReactOnRails::WebpackerUtils)
.to receive("shakapacker_version")
.and_return("7.0.0")
end

it "doesn't show deprecation message for webpacker_precompile?" do
allow(Webpacker).to receive_message_chain("config.shakapacker_precompile?")
.and_return(false)

expect do
ReactOnRails.configure do |config|
config.build_production_command = "RAILS_ENV=production NODE_ENV=production bin/shakapacker"
end
end.not_to output(/Consider using `shakapacker_precompile?`/).to_stdout
.and_return("8.0.0")
end

it "fails when \"shakapacker_precompile\" is truly and \"build_production_command\" is truly" do
allow(Webpacker).to receive_message_chain("config.shakapacker_precompile?")
allow(Shakapacker).to receive_message_chain("config.shakapacker_precompile?")
.and_return(true)
expect do
ReactOnRails.configure do |config|
Expand All @@ -148,7 +137,7 @@ module ReactOnRails
end

it "doesn't fail when \"shakapacker_precompile\" is falsy and \"build_production_command\" is truly" do
allow(Webpacker).to receive_message_chain("config.shakapacker_precompile?")
allow(Shakapacker).to receive_message_chain("config.shakapacker_precompile?")
.and_return(false)
expect do
ReactOnRails.configure do |config|
Expand All @@ -158,15 +147,15 @@ module ReactOnRails
end

it "doesn't fail when \"shakapacker_precompile\" is truly and \"build_production_command\" is falsy" do
allow(Webpacker).to receive_message_chain("config.shakapacker_precompile?")
allow(Shakapacker).to receive_message_chain("config.shakapacker_precompile?")
.and_return(true)
expect do
ReactOnRails.configure {} # rubocop:disable-line Lint/EmptyBlock
end.not_to raise_error
end

it "doesn't fail when \"shakapacker_precompile\" is falsy and \"build_production_command\" is falsy" do
allow(Webpacker).to receive_message_chain("config.shakapacker_precompile?")
allow(Shakapacker).to receive_message_chain("config.shakapacker_precompile?")
.and_return(false)
expect do
ReactOnRails.configure {} # rubocop:disable-line Lint/EmptyBlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
.and_return(File.join(generated_assets_full_path, "manifest.json"))
allow(ReactOnRails::Utils).to receive(:bundle_js_file_path)
.with("server-bundle.js")
.and_raise(Webpacker::Manifest::MissingEntryError)
.and_raise(Shakapacker::Manifest::MissingEntryError)
touch_files_in_dir(generated_assets_full_path)
end

Expand Down
Loading

0 comments on commit f2099d7

Please sign in to comment.