From 7111745ed91305b1d494cf575f7caa727b9d6738 Mon Sep 17 00:00:00 2001 From: Alexander Schrot Date: Wed, 15 Apr 2020 15:22:26 +0200 Subject: [PATCH] Fix cache key for coffee script processor The current implementation leads to a problem with the generation of the sourcemap. Two files with a different name but the same content (e.g. empty files) have different sourcemaps. But the current implementation returns the v3SourceMap of the first file for every subsequent call from the cache. So the commit changes the cache key to be dependent on the filename. --- CHANGELOG.md | 1 + lib/sprockets/coffee_script_processor.rb | 2 +- test/test_coffee_script_processor.rb | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72574d902..f9ae17ca8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprocket - Allow assets already fingerprinted to be served through `Sprockets::Server` - Do not fingerprint files that already contain a valid digest in their name - Remove remaining support for Ruby < 2.4.[#672](https://github.com/rails/sprockets/pull/672) +- Fix cache key for coffee script processor to be dependent on the filename [#670](https://github.com/rails/sprockets/pull/670) ## 4.0.2 diff --git a/lib/sprockets/coffee_script_processor.rb b/lib/sprockets/coffee_script_processor.rb index b660a9867..458236d17 100644 --- a/lib/sprockets/coffee_script_processor.rb +++ b/lib/sprockets/coffee_script_processor.rb @@ -20,7 +20,7 @@ def self.cache_key def self.call(input) data = input[:data] - js, map = input[:cache].fetch([self.cache_key, data]) do + js, map = input[:cache].fetch([self.cache_key, data, input[:filename]]) do result = Autoload::CoffeeScript.compile( data, sourceMap: "v3", diff --git a/test/test_coffee_script_processor.rb b/test/test_coffee_script_processor.rb index 86a5c0e9f..614460ebd 100644 --- a/test/test_coffee_script_processor.rb +++ b/test/test_coffee_script_processor.rb @@ -28,6 +28,26 @@ def test_compile_coffee_script_template_to_js assert_equal ["squared.source.coffee"], result[:map]["sources"] end + def test_changing_map_sources_for_files_with_same_content + input = { + load_path: File.expand_path("../fixtures", __FILE__), + filename: File.expand_path("../fixtures/squared.coffee", __FILE__), + content_type: 'application/javascript', + environment: @env, + data: "square = (n) -> n * n", + name: 'squared', + cache: Sprockets::Cache.new, + metadata: { mapping: [] } + } + result = Sprockets::CoffeeScriptProcessor.call(input) + assert_equal ["squared.source.coffee"], result[:map]["sources"] + + input[:filename] = File.expand_path("../fixtures/peterpan.coffee", __FILE__) + + result = Sprockets::CoffeeScriptProcessor.call(input) + assert_equal ["peterpan.source.coffee"], result[:map]["sources"] + end + def test_cache_key assert Sprockets::CoffeeScriptProcessor.cache_key end