Skip to content

Commit

Permalink
Merge pull request #158 from tagliala/feat/support-shakapacker
Browse files Browse the repository at this point in the history
Add support to Shakapacker
  • Loading branch information
jamesmartin authored Sep 3, 2024
2 parents 1d03682 + a9ab1b0 commit bb5634e
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 = asset_helper.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 asset_helper.dev_server.running?
dev_server_asset(@asset_path)
elsif Webpacker.config.public_path.present?
File.join(Webpacker.config.public_path, @asset_path)
elsif asset_helper.config.public_path.present?
File.join(asset_helper.config.public_path, @asset_path)
end
end

private

def asset_helper
@asset_helper ||=
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(asset_helper.dev_server.host, asset_helper.dev_server.port)
http.use_ssl = asset_helper.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 bb5634e

Please sign in to comment.