Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add asset_pack_url helper #1102

Merged
merged 1 commit into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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