Skip to content

Commit 9742377

Browse files
committed
Error if asset alias is used from helper
1 parent 3a46b52 commit 9742377

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

lib/sprockets/rails/helper.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ def initialize(bad_path, good_path, prefix)
3838
end
3939
end
4040

41+
class AssetAliasUsed < StandardError
42+
def initialize(actual, expected)
43+
msg = "Asset was linked to from an alias rather than its exact path. " +
44+
"Alias resolving may not be available in production.\n" +
45+
"Use #{expected.inspect} instead of #{actual.inspect}"
46+
super(msg)
47+
end
48+
end
49+
4150
if defined? ActionView::Helpers::AssetUrlHelper
4251
include ActionView::Helpers::AssetUrlHelper
4352
include ActionView::Helpers::AssetTagHelper
@@ -121,6 +130,10 @@ def asset_digest_path(path, options = {})
121130

122131
if environment = assets_environment
123132
if asset = environment[path]
133+
if self.raise_runtime_errors && path != asset.logical_path
134+
raise AssetAliasUsed.new(path, asset.logical_path)
135+
end
136+
124137
return asset.digest_path
125138
end
126139
end

test/fixtures/bundle/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var jQuery;

test/fixtures/jquery/bower.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "jquery",
3+
"main": "./jquery.js"
4+
}

test/fixtures/jquery/jquery.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var jQuery;

test/test_helper.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,4 +699,26 @@ def test_ignores_missing_dependencies
699699
end
700700
assert_includes paths, "missing.css.erb"
701701
end
702+
703+
def test_asset_path_with_index_requires_exact_logical_path
704+
Sprockets::Rails::Helper.raise_runtime_errors = true
705+
Sprockets::Rails::Helper.precompile = ["bundle.js"]
706+
707+
assert @view.asset_path("bundle.js")
708+
709+
assert_raises(Sprockets::Rails::Helper::AssetAliasUsed) do
710+
assert @view.asset_path("bundle/index.js")
711+
end
712+
end
713+
714+
def test_asset_path_with_bower_requires_exact_logical_path
715+
Sprockets::Rails::Helper.raise_runtime_errors = true
716+
Sprockets::Rails::Helper.precompile = ["jquery/jquery.js"]
717+
718+
assert @view.asset_path("jquery/jquery.js")
719+
720+
assert_raises(Sprockets::Rails::Helper::AssetAliasUsed) do
721+
assert @view.asset_path("jquery.js")
722+
end
723+
end
702724
end

0 commit comments

Comments
 (0)