module Webpacker::Helper

def asset_pack_path(name, **options)

<%= asset_pack_path 'calendar.css' %> # => "/packs/calendar-1016838bab065ae1e122.css"
# When extract_css is true in webpacker.yml or the file is not a css:

<%= asset_pack_path 'calendar.css' %> # => nil
# When extract_css is false in webpacker.yml and the file is a css:

Example:

This will use path_to_asset internally, so most of their behaviors will be the same.
Returns the relative path using manifest.json and passes it to path_to_asset helper.
Computes the relative path for a given Webpacker asset.
def asset_pack_path(name, **options)
  if current_webpacker_instance.config.extract_css? || !stylesheet?(name)
    path_to_asset(current_webpacker_instance.manifest.lookup!(name), options)
  end
end

def asset_pack_url(name, **options)

<%= asset_pack_url 'calendar.css' %> # => "http://example.com/packs/calendar-1016838bab065ae1e122.css"
# When extract_css is true in webpacker.yml or the file is not a css:

<%= asset_pack_url 'calendar.css' %> # => nil
# When extract_css is false in webpacker.yml and the file is a css:

Example:

This will use url_to_asset internally, so most of their behaviors will be the same.
Returns the absolute path using manifest.json and passes it to url_to_asset helper.
Computes the absolute path for a given Webpacker asset.
def asset_pack_url(name, **options)
  if current_webpacker_instance.config.extract_css? || !stylesheet?(name)
    url_to_asset(current_webpacker_instance.manifest.lookup!(name), options)
  end
end

def current_webpacker_instance

configurations within the same app (e.g. with engines).
Could be overridden to use multiple Webpacker
Returns the current Webpacker instance.
def current_webpacker_instance
  Webpacker.instance
end

def favicon_pack_tag(name, **options)


<%= favicon_pack_tag 'mb-icon.png', rel: 'apple-touch-icon', type: 'image/png' %>

Example:

Creates a link tag for a favicon that references the named pack file.
def favicon_pack_tag(name, **options)
  favicon_link_tag(resolve_path_to_image(name), options)
end

def image_pack_tag(name, **options)


<%= image_pack_tag 'picture.png', srcset: { 'picture-2x.png' => '2x' } %>

Edit Entry
<%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>

Example:

Creates an image tag that references the named pack file.
def image_pack_tag(name, **options)
  if options[:srcset] && !options[:srcset].is_a?(String)
    options[:srcset] = options[:srcset].map do |src_name, size|
      "#{resolve_path_to_image(src_name)} #{size}"
    end.join(", ")
  end
  image_tag(resolve_path_to_image(name), options)
end

def javascript_pack_tag(*names, **options)


<%= javascript_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>

Example:

app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.
in package/environments/base.js. By default, this list is auto-generated to match everything in
Creates a script tag that references the named pack file, as compiled by webpack per the entries list
def javascript_pack_tag(*names, **options)
  javascript_include_tag(*sources_from_manifest_entries(names, type: :javascript), **options)
end

def javascript_packs_with_chunks_tag(*names, **options)

<%= javascript_packs_with_chunks_tag 'map' %>
<%= javascript_packs_with_chunks_tag 'calendar' %>

DON'T:

<%= javascript_packs_with_chunks_tag 'calendar', 'map' %>

DO:






<%= javascript_packs_with_chunks_tag 'calendar', 'map', 'data-turbolinks-track': 'reload' %> # =>

Example:

See: https://webpack.js.org/plugins/split-chunks-plugin/
app/javascript/packs/*.js and all the dependent chunks. In production mode, the digested reference is automatically looked up.
By default, this list is auto-generated to match everything in
as compiled by webpack per the entries list in package/environments/base.js.
Creates script tags that reference the js chunks from entrypoints when using split chunks API,
def javascript_packs_with_chunks_tag(*names, **options)
  javascript_include_tag(*sources_from_manifest_entrypoints(names, type: :javascript), **options)
end

def preload_pack_asset(name, **options)


<%= preload_pack_asset 'fonts/fa-regular-400.woff2' %> # =>

Example:

See: https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content
In production mode, the digested reference is automatically looked up.
Creates a link tag, for preloading, that references a given Webpacker asset.
def preload_pack_asset(name, **options)
  if self.class.method_defined?(:preload_link_tag)
    preload_link_tag(current_webpacker_instance.manifest.lookup!(name), options)
  else
    raise "You need Rails >= 5.2 to use this tag."
  end
end

def resolve_path_to_image(name)

def resolve_path_to_image(name)
  path = name.starts_with?("media/images/") ? name : "media/images/#{name}"
  path_to_asset(current_webpacker_instance.manifest.lookup!(path))
rescue
  path_to_asset(current_webpacker_instance.manifest.lookup!(name))
end

def sources_from_manifest_entries(names, type:)

def sources_from_manifest_entries(names, type:)
  names.map { |name| current_webpacker_instance.manifest.lookup!(name, type: type) }.flatten
end

def sources_from_manifest_entrypoints(names, type:)

def sources_from_manifest_entrypoints(names, type:)
  names.map { |name| current_webpacker_instance.manifest.lookup_pack_with_chunks!(name, type: type) }.flatten.uniq
end

def stylesheet?(name)

def stylesheet?(name)
  File.extname(name) == ".css"
end

def stylesheet_pack_tag(*names, **options)


<%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
# When extract_css is true in webpacker.yml:

nil
<%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
# When extract_css is false in webpacker.yml:

Examples:

In that setup you need to configure your styles to be inlined in your JavaScript for hot reloading.
Note: If the development server is running and hot module replacement is active, this will return nothing.

app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.
in package/environments/base.js. By default, this list is auto-generated to match everything in
Creates a link tag that references the named pack file, as compiled by webpack per the entries list
def stylesheet_pack_tag(*names, **options)
  if current_webpacker_instance.config.extract_css?
    stylesheet_link_tag(*sources_from_manifest_entries(names, type: :stylesheet), **options)
  end
end

def stylesheet_packs_with_chunks_tag(*names, **options)

<%= stylesheet_packs_with_chunks_tag 'map' %>
<%= stylesheet_packs_with_chunks_tag 'calendar' %>

DON'T:

<%= stylesheet_packs_with_chunks_tag 'calendar', 'map' %>

DO:




<%= stylesheet_packs_with_chunks_tag 'calendar', 'map' %> # =>

Examples:

See: https://webpack.js.org/plugins/split-chunks-plugin/
app/javascript/packs/*.js and all the dependent chunks. In production mode, the digested reference is automatically looked up.
By default, this list is auto-generated to match everything in
as compiled by webpack per the entries list in package/environments/base.js.
Creates link tags that reference the css chunks from entrypoints when using split chunks API,
def stylesheet_packs_with_chunks_tag(*names, **options)
  if current_webpacker_instance.config.extract_css?
    stylesheet_link_tag(*sources_from_manifest_entrypoints(names, type: :stylesheet), **options)
  end
end