Skip to content

Commit

Permalink
Add asset_pack_url (#1102)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwightwatson authored and gauravtiwari committed Dec 14, 2017
1 parent d839231 commit b663c6a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/webpacker/helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Webpacker::Helper
# Computes the full path for a given Webpacker asset.
# Return relative path using manifest.json and passes it to asset_url helper
# 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.
#
# Example:
Expand All @@ -9,6 +9,18 @@ module Webpacker::Helper
def asset_pack_path(name, **options)
asset_path(Webpacker.manifest.lookup!(name), **options)
end

# Computes the absolute path for a given Webpacker asset.
# Return absolute path using manifest.json and passes it to asset_url helper
# This will use asset_url internally, so most of their behaviors will be the same.
#
# Example:
#
# <%= asset_pack_url 'calendar.css' %> # => "http://example.com/packs/calendar-1016838bab065ae1e122.css"
def asset_pack_url(name, **options)
asset_url(Webpacker.manifest.lookup!(name), **options)
end

# Creates a script tag that references the named pack file, as compiled by webpack per the entries list
# in config/webpack/shared.js. By default, this list is auto-generated to match everything in
# app/javascript/packs/*.js. In production mode, the digested reference is automatically looked up.
Expand Down
8 changes: 8 additions & 0 deletions test/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class HelperTest < ActionView::TestCase
def setup
@request = Class.new do
def send_early_hints(links) end
def base_url
"https://example.com"
end
end.new
end

Expand All @@ -16,6 +19,11 @@ def test_asset_pack_path
assert_equal "/packs/bootstrap-c38deda30895059837cf.css", asset_pack_path("bootstrap.css")
end

def test_asset_pack_url
assert_equal "https://example.com/packs/bootstrap-300631c4f0e0f9c865bc.js", asset_pack_url("bootstrap.js")
assert_equal "https://example.com/packs/bootstrap-c38deda30895059837cf.css", asset_pack_url("bootstrap.css")
end

def test_javascript_pack_tag
assert_equal \
%(<script src="/packs/bootstrap-300631c4f0e0f9c865bc.js"></script>),
Expand Down

0 comments on commit b663c6a

Please sign in to comment.