diff --git a/CHANGELOG.md b/CHANGELOG.md index 99619f68f..e60ec4707 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprocket ## Master - Fix for Ruby 2.7 keyword arguments warning in `base.rb`. [#660](https://github.com/rails/sprockets/pull/660) +- Fix for when `x_sprockets_linecount` is missing from a source map. - Remove remaining support for Ruby < 2.4.[#672](https://github.com/rails/sprockets/pull/672) ## 4.0.0 diff --git a/UPGRADING.md b/UPGRADING.md index 37b221516..dcab46401 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -53,7 +53,7 @@ If you wanted to specify additional assets to deliver that were not included by config.assets.precompile += ["marketing.css"] ``` -If you are using Sprockets 4, Rails changes it's default logic for determining top-level targets. It will now use _only_ a file at `./app/assets/config/manifest.js` for specifying top-level targets; this file may already exist in your Rails app (although Rails only starts automatically using it once you are using sprockets 4), if not you should create it. +If you are using Sprockets 4, Rails changes its default logic for determining top-level targets. It will now use _only_ a file at `./app/assets/config/manifest.js` for specifying top-level targets; this file may already exist in your Rails app (although Rails only starts automatically using it once you are using sprockets 4), if not you should create it. The `manifest.js` file is meant to specify which files to use as a top-level target using sprockets methods `link`, `link_directory`, and `link_tree`. diff --git a/lib/sprockets/source_map_utils.rb b/lib/sprockets/source_map_utils.rb index f1b4cce88..b686bbb50 100644 --- a/lib/sprockets/source_map_utils.rb +++ b/lib/sprockets/source_map_utils.rb @@ -78,7 +78,7 @@ def concat_source_maps(a, b) offset = 0 if a["sections"].count != 0 && !a["sections"].last["map"]["mappings"].empty? last_line_count = a["sections"].last["map"].delete("x_sprockets_linecount") - offset += last_line_count + offset += last_line_count || 1 last_offset = a["sections"].last["offset"]["line"] offset += last_offset diff --git a/test/test_source_map_utils.rb b/test/test_source_map_utils.rb index febca7b45..1fff15d7a 100644 --- a/test/test_source_map_utils.rb +++ b/test/test_source_map_utils.rb @@ -177,6 +177,51 @@ def test_concat_source_maps ], Sprockets::SourceMapUtils.decode_source_map(combined)[:mappings] end + def test_concat_without_x_sprockets_linecount_works + abc_mappings = [ + { source: 'a.js', generated: [1, 0], original: [1, 0] }, + { source: 'b.js', generated: [2, 0], original: [20, 0] }, + { source: 'c.js', generated: [3, 0], original: [30, 0] } + ].freeze + + d_mapping = [ + { source: 'd.js', generated: [1, 0], original: [1, 0] } + ].freeze + + abc_map = { + "version" => 3, + "file" => "a.js", + "mappings" => Sprockets::SourceMapUtils.encode_vlq_mappings(abc_mappings), + "sources" => ["a.js", "b.js", "c.js"], + "names" => [], + "x_sprockets_linecount" => 3 + } + + d_map = { + "version" => 3, + "file" => "d.js", + "mappings" => Sprockets::SourceMapUtils.encode_vlq_mappings(d_mapping), + "sources" => ["d.js"], + "names" => [], + } + + combined = Sprockets::SourceMapUtils.concat_source_maps(deep_dup(abc_map), deep_dup(d_map)) + assert_equal [ + { source: 'a.js', generated: [1, 0], original: [1, 0] }, + { source: 'b.js', generated: [2, 0], original: [20, 0] }, + { source: 'c.js', generated: [3, 0], original: [30, 0] }, + { source: 'd.js', generated: [4, 0], original: [1, 0] } + ], Sprockets::SourceMapUtils.decode_source_map(combined)[:mappings] + + combined = Sprockets::SourceMapUtils.concat_source_maps(deep_dup(d_map), deep_dup(abc_map)) + assert_equal [ + { source: 'd.js', generated: [1, 0], original: [1, 0] }, + { source: 'a.js', generated: [2, 0], original: [1, 0] }, + { source: 'b.js', generated: [3, 0], original: [20, 0] }, + { source: 'c.js', generated: [4, 0], original: [30, 0] } + ], Sprockets::SourceMapUtils.decode_source_map(combined)[:mappings] + end + def test_combine_source_maps_returns_original abc_mappings = [ { source: 'a.js', generated: [1, 0], original: [0, 0] },