Module: WebpackerLite::Helper

Defined in:
lib/webpacker_lite/helper.rb

Instance Method Summary collapse

Instance Method Details

#asset_pack_path(name, **options) ⇒ Object

Computes the full path for a given webpacker asset. Return relative path using manifest.json and passes it to asset_url helper This will use asset_path internally, so most of their behaviors will be the same. Examples:

In development mode:

<%= asset_pack_path 'calendar.js' %> # => "/public/webpack/development/calendar-1016838bab065ae1e122.js"

In production mode:

<%= asset_pack_path 'calendar.css' %> # => "/public/webpack/production/calendar-1016838bab065ae1e122.css"


14
15
16
17
# File 'lib/webpacker_lite/helper.rb', line 14

def asset_pack_path(name, **options)
  pack_path = pack_path(name)
  asset_path(pack_path, **options)
end

#javascript_pack_tag(name, **options) ⇒ Object

Creates a script tag that references the named pack file, as compiled by Webpack.

Examples:

In development mode:
<%= javascript_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
<script src="/public/webpack/development/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>

# In production mode:
<%= javascript_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
<script src="/public/webpack/production/calendar-1016838bab065ae1e314.js" data-turbolinks-track="reload"></script>


42
43
44
# File 'lib/webpacker_lite/helper.rb', line 42

def javascript_pack_tag(name, **options)
  javascript_include_tag(asset_source(name, :javascript), **options)
end

#pack_path(name) ⇒ Object

Computes the full path for a given webpacker asset. Return relative path using manifest.json and passes it to asset_url helper Examples:

In production mode:

<%= asset_pack_path 'calendar.css' %> # => "webpack/production/calendar-1016838bab065ae1e122.css"


25
26
27
28
29
# File 'lib/webpacker_lite/helper.rb', line 25

def pack_path(name)
  path = WebpackerLite::Configuration.base_path
  file = WebpackerLite::Manifest.lookup(name)
  "#{path}/#{file}"
end

#stylesheet_pack_tag(name, **options) ⇒ Object

Creates a link tag that references the named pack file(s), as compiled by Webpack per the entries list in client/webpack.client.base.config.js.

Examples:

# In production mode:
<%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
<link rel="stylesheet" media="screen" href="/public/webpack/production/calendar-1016838bab065ae1e122.css" data-turbolinks-track="reload" />

# In development mode:
<%= stylesheet_pack_tag 'calendar', 'data-turbolinks-track': 'reload' %> # =>
<link rel="stylesheet" media="screen" href="/public/webpack/development/calendar-1016838bab065ae1e122.css" data-turbolinks-track="reload" />

# In development mode with hot-reloading
<%= stylesheet_pack_tag('main') %> <% # Default is false for enabled_when_hot_loading%>
# No output

# In development mode with hot-reloading and enabled_when_hot_loading
# <%= stylesheet_pack_tag('main', enabled_when_hot_loading: true) %>
<link rel="stylesheet" media="screen" href="/public/webpack/development/calendar-1016838bab065ae1e122.css" />


67
68
69
70
# File 'lib/webpacker_lite/helper.rb', line 67

def stylesheet_pack_tag(name, **options)
  return "" if WebpackerLite::Env.hot_loading? && !options[:enabled_when_hot_loading].presence
  stylesheet_link_tag(asset_source(name, :stylesheet), **options)
end