Skip to content

Commit

Permalink
add current_webpacker to helper
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Nov 20, 2018
1 parent e2bc236 commit aa2aabd
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/webpacker/helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
module Webpacker::Helper
# Returns current Webpacker instance.
# Could be overriden to use multiple Webpacker
# configurations within the same app (e.g. with engines)
def current_webpacker
Webpacker.instance
end

# Computes the relative path for a given Webpacker asset.
# Return relative path using manifest.json and passes it to asset_path helper
# This will use asset_path internally, so most of their behaviors will be the same.
Expand All @@ -11,8 +18,8 @@ module Webpacker::Helper
# # In production mode:
# <%= asset_pack_path 'calendar.css' %> # => "/packs/calendar-1016838bab065ae1e122.css"
def asset_pack_path(name, **options)
unless stylesheet?(name) && Webpacker.dev_server.running? && Webpacker.dev_server.hot_module_replacing?
asset_path(Webpacker.manifest.lookup!(name), **options)
unless stylesheet?(name) && current_webpacker.dev_server.running? && current_webpacker.dev_server.hot_module_replacing?
asset_path(current_webpacker.manifest.lookup!(name), **options)
end
end

Expand All @@ -28,8 +35,8 @@ def asset_pack_path(name, **options)
# # In production mode:
# <%= asset_pack_url 'calendar.css' %> # => "http://example.com/packs/calendar-1016838bab065ae1e122.css"
def asset_pack_url(name, **options)
unless Webpacker.dev_server.running? && Webpacker.dev_server.hot_module_replacing?
asset_url(Webpacker.manifest.lookup!(name), **options)
unless current_webpacker.dev_server.running? && current_webpacker.dev_server.hot_module_replacing?
asset_url(current_webpacker.manifest.lookup!(name), **options)
end
end

Expand All @@ -40,7 +47,7 @@ def asset_pack_url(name, **options)
# <%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
# <img alt='Edit Entry' src='/packs/application-k344a6d59eef8632c9d1.png' width='16' height='10' />
def image_pack_tag(name, **options)
image_tag(asset_path(Webpacker.manifest.lookup!(name)), **options)
image_tag(asset_path(current_webpacker.manifest.lookup!(name)), **options)
end

# Creates a script tag that references the named pack file, as compiled by webpack per the entries list
Expand Down Expand Up @@ -72,7 +79,7 @@ def javascript_pack_tag(*names, **options)
# <%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
# <link rel="stylesheet" media="screen" href="/packs/calendar-1016838bab065ae1e122.css" data-turbolinks-track="reload" />
def stylesheet_pack_tag(*names, **options)
unless Webpacker.dev_server.running? && Webpacker.dev_server.hot_module_replacing?
unless current_webpacker.dev_server.running? && current_webpacker.dev_server.hot_module_replacing?
stylesheet_link_tag(*sources_from_pack_manifest(names, type: :stylesheet), **options)
end
end
Expand All @@ -83,7 +90,7 @@ def stylesheet?(name)
end

def sources_from_pack_manifest(names, type:)
names.map { |name| Webpacker.manifest.lookup!(pack_name_with_extension(name, type: type)) }
names.map { |name| current_webpacker.manifest.lookup!(pack_name_with_extension(name, type: type)) }
end

def pack_name_with_extension(name, type:)
Expand Down

0 comments on commit aa2aabd

Please sign in to comment.