Class: InlineSvg::WebpackAssetFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/inline_svg/webpack_asset_finder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ WebpackAssetFinder

Returns a new instance of WebpackAssetFinder.



7
8
9
# File 'lib/inline_svg/webpack_asset_finder.rb', line 7

def initialize(filename)
  @filename = filename
end

Class Method Details

.find_asset(filename) ⇒ Object



3
4
5
# File 'lib/inline_svg/webpack_asset_finder.rb', line 3

def self.find_asset(filename)
  new(filename)
end

Instance Method Details

#pathnameObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/inline_svg/webpack_asset_finder.rb', line 11

def pathname
  file_path = Webpacker.instance.manifest.lookup(@filename)
  return unless file_path

  if Webpacker.dev_server.running?
    asset = Net::HTTP.get(Webpacker.dev_server.host, file_path, Webpacker.dev_server.port)

    begin
      Tempfile.new(file_path).tap do |file|
        file.binmode
        file.write(asset)
        file.rewind
      end
    rescue StandardError => e
      Rails.logger.error "Error creating tempfile: #{e}"
      raise
    end

  else
    public_path = Webpacker.config.public_path
    return unless public_path

    File.join(public_path, file_path)
  end
end