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 also:
- Checks if `Shakapacker` is defined; if not, it falls back on
  `Webpacker`.
- Uses the scope resolution operator to resolve at top-level
  scope
- Checks `protocol` instead of `https?` because the former is available
  from Webpacker 3 and the latter is not available anymore in
  Shakapacker >= 8

Refs:
- shakacode/shakapacker#414
- shakacode/shakapacker#429
- shakacode/shakapacker#486

Close #156
  • Loading branch information
tagliala committed May 25, 2024
1 parent 6a603dc commit 3aa0e48
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.protocol == "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 3aa0e48

Please sign in to comment.