Skip to content

Commit

Permalink
Merge pull request #1228 from rails/asset-helper
Browse files Browse the repository at this point in the history
Fix asset helper for non-css assets in hmr mode
  • Loading branch information
guilleiguaran authored Jan 28, 2018
2 parents 4b623d4 + b3654c0 commit d26c164
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/webpacker/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Webpacker::Helper
# # In production mode:
# <%= asset_pack_path 'calendar.css' %> # => "/packs/calendar-1016838bab065ae1e122.css"
def asset_pack_path(name, **options)
unless Webpacker.dev_server.running? && Webpacker.dev_server.hot_module_replacing?
unless stylesheet?(name) && Webpacker.dev_server.running? && Webpacker.dev_server.hot_module_replacing?
asset_path(Webpacker.manifest.lookup!(name), **options)
end
end
Expand Down Expand Up @@ -68,6 +68,10 @@ def stylesheet_pack_tag(*names, **options)
end

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

def sources_from_pack_manifest(names, type:)
names.map { |name| Webpacker.manifest.lookup!(pack_name_with_extension(name, type: type)) }
end
Expand Down
7 changes: 7 additions & 0 deletions test/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ def base_url
def test_asset_pack_path
assert_equal "/packs/bootstrap-300631c4f0e0f9c865bc.js", asset_pack_path("bootstrap.js")
assert_equal "/packs/bootstrap-c38deda30895059837cf.css", asset_pack_path("bootstrap.css")

Webpacker.dev_server.stub :running?, true do
Webpacker.dev_server.stub :hot_module_replacing?, true do
assert_nil asset_pack_path("bootstrap.css")
assert_equal "/packs/application-k344a6d59eef8632c9d1.png", asset_pack_path("application.png")
end
end
end

def test_asset_pack_url
Expand Down
3 changes: 2 additions & 1 deletion test/test_app/public/packs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"bootstrap.css": "/packs/bootstrap-c38deda30895059837cf.css",
"application.css": "/packs/application-dd6b1cd38bfa093df600.css",
"bootstrap.js": "/packs/bootstrap-300631c4f0e0f9c865bc.js",
"application.js": "/packs/application-k344a6d59eef8632c9d1.js"
"application.js": "/packs/application-k344a6d59eef8632c9d1.js",
"application.png": "/packs/application-k344a6d59eef8632c9d1.png"
}

0 comments on commit d26c164

Please sign in to comment.