Skip to content

Commit

Permalink
Merge pull request #113 from kylefox/ssl-webpack-dev-server
Browse files Browse the repository at this point in the history
Fix "EOFError error" when using webpack dev server over HTTPS
  • Loading branch information
jamesmartin authored Feb 23, 2020
2 parents e7a3e1a + d232345 commit 4ed2974
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions lib/inline_svg/webpack_asset_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,45 @@ def self.find_asset(filename)

def initialize(filename)
@filename = filename
@asset_path = Webpacker.manifest.lookup(@filename)
end

def pathname
file_path = Webpacker.instance.manifest.lookup(@filename)
return unless file_path
return if @asset_path.blank?

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
dev_server_asset(@asset_path)
elsif Webpacker.config.public_path.present?
File.join(Webpacker.config.public_path, @asset_path)
end
end

private

else
public_path = Webpacker.config.public_path
return unless public_path
def dev_server_asset(file_path)
asset = fetch_from_dev_server(file_path)

File.join(public_path, file_path)
begin
Tempfile.new(file_path).tap do |file|
file.binmode
file.write(asset)
file.rewind
end
rescue StandardError => e
Rails.logger.error "[inline_svg] Error creating tempfile for #{@filename}: #{e}"
raise
end
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.verify_mode = OpenSSL::SSL::VERIFY_NONE

http.request(Net::HTTP::Get.new(file_path)).body
rescue StandardError => e
Rails.logger.error "[inline_svg] Error fetching #{@filename} from webpack-dev-server: #{e}"
raise
end
end
end

0 comments on commit 4ed2974

Please sign in to comment.