module Webpacker::Helper
def asset_pack_path(name, **options)
# 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 asset_path internally, so most of their behaviors will be the same.
Return relative path using manifest.json and passes it to asset_path helper
Computes the relative path for a given Webpacker asset.
def asset_pack_path(name, **options) if Webpacker.config.extract_css? || !stylesheet?(name) asset_path(Webpacker.manifest.lookup!(name), **options) end end
def asset_pack_url(name, **options)
# 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 asset_url internally, so most of their behaviors will be the same.
Return absolute path using manifest.json and passes it to asset_url helper
Computes the absolute path for a given Webpacker asset.
def asset_pack_url(name, **options) if Webpacker.config.extract_css? || !stylesheet?(name) asset_url(Webpacker.manifest.lookup!(name), **options) end end
def image_pack_tag(name, **options)

<%= image_pack_tag 'application.png', size: '16x10', alt: 'Edit Entry' %>
Example:
Creates a image tag that references the named pack file.
def image_pack_tag(name, **options) image_tag(asset_path(Webpacker.manifest.lookup!(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 config/webpack/shared.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_pack_manifest(names, type: :javascript), **options) end
def sources_from_pack_manifest(names, type:)
def sources_from_pack_manifest(names, type:) names.map { |name| Webpacker.manifest.lookup!(name, type: type) }.flatten 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 config/webpack/shared.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 Webpacker.config.extract_css? stylesheet_link_tag(*sources_from_pack_manifest(names, type: :stylesheet), **options) end end