Skip to content

Commit

Permalink
Add support to Shakapacker
Browse files Browse the repository at this point in the history
Shakapacker is the official, actively maintained successor to Webpacker.

Shakapacker v7 changed the spelling of `Webpacker` to `Shakapacker` in
the entire project, but still provided backward compatibility for
`Webpacker` spelling.

v8 dropped the deprecated spelling

This commit checks if Shakapacker is defined, otherwise it falls back
on Webpacker.

Ref: shakacode/shakapacker#429

Close jamesmartin#156
  • Loading branch information
tagliala committed May 19, 2024
1 parent 6a603dc commit 26b2711
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/inline_svg/webpack_asset_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,31 @@ def self.find_asset(filename)

def initialize(filename)
@filename = filename
manifest_lookup = Webpacker.manifest.lookup(@filename)
manifest_lookup = wrapper.manifest.lookup(@filename)
@asset_path = manifest_lookup.present? ? URI(manifest_lookup).path : ""
end

def pathname
return if @asset_path.blank?

if Webpacker.dev_server.running?
if wrapper.dev_server.running?
dev_server_asset(@asset_path)
elsif Webpacker.config.public_path.present?
File.join(Webpacker.config.public_path, @asset_path)
elsif wrapper.config.public_path.present?
File.join(wrapper.config.public_path, @asset_path)
end
end

private

def wrapper
@wrapper ||=
if defined?(::Shakapacker)
::Shakapacker
else
::Webpacker
end
end

def dev_server_asset(file_path)
asset = fetch_from_dev_server(file_path)

Expand All @@ -38,8 +47,8 @@ def dev_server_asset(file_path)
end

def fetch_from_dev_server(file_path)
http = Net::HTTP.new(Webpacker.dev_server.host, Webpacker.dev_server.port)
http.use_ssl = Webpacker.dev_server.https?
http = Net::HTTP.new(wrapper.dev_server.host, wrapper.dev_server.port)
http.use_ssl = wrapper.dev_server.https?
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

http.request(Net::HTTP::Get.new(file_path)).body
Expand Down
10 changes: 10 additions & 0 deletions spec/webpack_asset_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,14 @@
expect(described_class.find_asset('some-file').pathname).to be_nil
end
end

context "when Shakapacker is defined" do
it "uses the new spelling" do
stub_const('Rails', double('Rails').as_null_object)
stub_const('Shakapacker', double('Shakapacker').as_null_object)
expect(::Shakapacker.manifest).to receive(:lookup).with('some-file').and_return(nil)

expect(described_class.find_asset('some-file').pathname).to be_nil
end
end
end

0 comments on commit 26b2711

Please sign in to comment.