diff --git a/lib/sprockets/rails/helper.rb b/lib/sprockets/rails/helper.rb index 87e4f9f1..ee32de33 100644 --- a/lib/sprockets/rails/helper.rb +++ b/lib/sprockets/rails/helper.rb @@ -38,6 +38,15 @@ def initialize(bad_path, good_path, prefix) end end + class AssetAliasUsed < StandardError + def initialize(actual, expected) + msg = "Asset was linked to from an alias rather than its exact path. " + + "Alias resolving may not be available in production.\n" + + "Use #{expected.inspect} instead of #{actual.inspect}" + super(msg) + end + end + if defined? ActionView::Helpers::AssetUrlHelper include ActionView::Helpers::AssetUrlHelper include ActionView::Helpers::AssetTagHelper @@ -121,6 +130,10 @@ def asset_digest_path(path, options = {}) if environment = assets_environment if asset = environment[path] + if self.raise_runtime_errors && path != asset.logical_path + raise AssetAliasUsed.new(path, asset.logical_path) + end + return asset.digest_path end end diff --git a/test/fixtures/bundle/index.js b/test/fixtures/bundle/index.js new file mode 100644 index 00000000..30f6635a --- /dev/null +++ b/test/fixtures/bundle/index.js @@ -0,0 +1 @@ +var jQuery; diff --git a/test/fixtures/jquery/bower.json b/test/fixtures/jquery/bower.json new file mode 100644 index 00000000..382a36e2 --- /dev/null +++ b/test/fixtures/jquery/bower.json @@ -0,0 +1,4 @@ +{ + "name": "jquery", + "main": "./jquery.js" +} diff --git a/test/fixtures/jquery/jquery.js b/test/fixtures/jquery/jquery.js new file mode 100644 index 00000000..30f6635a --- /dev/null +++ b/test/fixtures/jquery/jquery.js @@ -0,0 +1 @@ +var jQuery; diff --git a/test/test_helper.rb b/test/test_helper.rb index 98eb2a52..f31c160d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -699,4 +699,28 @@ def test_ignores_missing_dependencies end assert_includes paths, "missing.css.erb" end + + if Sprockets::VERSION > "3" + def test_asset_path_with_index_requires_exact_logical_path + Sprockets::Rails::Helper.raise_runtime_errors = true + Sprockets::Rails::Helper.precompile = ["bundle.js"] + + assert @view.asset_path("bundle.js") + + assert_raises(Sprockets::Rails::Helper::AssetAliasUsed) do + assert @view.asset_path("bundle/index.js") + end + end + + def test_asset_path_with_bower_requires_exact_logical_path + Sprockets::Rails::Helper.raise_runtime_errors = true + Sprockets::Rails::Helper.precompile = ["jquery/jquery.js"] + + assert @view.asset_path("jquery/jquery.js") + + assert_raises(Sprockets::Rails::Helper::AssetAliasUsed) do + assert @view.asset_path("jquery.js") + end + end + end end