Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embed sourcesContent in source maps #697

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprocket
## Master

- Remove remaining support for Ruby < 2.4.[#672](https://github.com/rails/sprockets/pull/672)
- Embed sourcesContent in source maps. [#697](https://github.com/rails/sprockets/pull/697)

## 4.0.2

Expand Down
4 changes: 1 addition & 3 deletions lib/sprockets/preprocessors/default_source_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ def call(input)
load_path = input[:load_path]
lines = input[:data].lines.length
basename = File.basename(filename)
mime_exts = input[:environment].config[:mime_exts]
pipeline_exts = input[:environment].config[:pipeline_exts]
if map.nil? || map.empty?
result[:map] = {
"version" => 3,
"file" => PathUtils.split_subpath(load_path, filename),
"mappings" => default_mappings(lines),
"sources" => [PathUtils.set_pipeline(basename, mime_exts, pipeline_exts, :source)],
"sources" => [basename],
"names" => []
}
else
Expand Down
15 changes: 10 additions & 5 deletions lib/sprockets/source_map_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ def self.call(input)
dependencies.merge(asset.metadata[:dependencies])

map["file"] = PathUtils.split_subpath(input[:load_path], input[:filename])
sources = map["sections"] ? map["sections"].map { |s| s["map"]["sources"] }.flatten : map["sources"]
sections = map["sections"] ? map["sections"] : [{ "map" => map }]
sections.each do |section|
section["map"]["sourcesContent"] = section["map"]["sources"].map do |source|
source = PathUtils.join(File.dirname(map["file"]), source)
uri, _ = env.resolve!(source)
next nil unless uri.start_with? "file://"

sources.each do |source|
source = PathUtils.join(File.dirname(map["file"]), source)
uri, _ = env.resolve!(source)
links << uri
path = URIUtils.split_file_uri(uri)[2]
next nil unless File.exist?(path)
File.read(path)
end
end

json = JSON.generate(map)
Expand Down
4 changes: 1 addition & 3 deletions lib/sprockets/source_map_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ def format_source_map(map, input)
filename = input[:filename]
load_path = input[:load_path]
load_paths = input[:environment].config[:paths]
mime_exts = input[:environment].config[:mime_exts]
pipeline_exts = input[:environment].config[:pipeline_exts]
file = PathUtils.split_subpath(load_path, filename)
{
"version" => 3,
Expand All @@ -50,7 +48,7 @@ def format_source_map(map, input)
source = PathUtils.join(File.dirname(filename), source) unless PathUtils.absolute_path?(source)
_, source = PathUtils.paths_split(load_paths, source)
source = PathUtils.relative_path_from(file, source)
PathUtils.set_pipeline(source, mime_exts, pipeline_exts, :source)
source
end,
"names" => map["names"]
}
Expand Down
4 changes: 2 additions & 2 deletions test/test_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ def setup
end

test "digest path" do
assert_equal "application.debug-dee339ce0ee2dc7cdfa0d36dff3ef946cebe1bd3e414515d40c3cafc49c0a51a.js",
assert_equal "application.debug-5bafea519f7aae9679023c6441b8c3623b4147cf5bca607abc5aab0c35ce6618.js",
@asset.digest_path
end

Expand Down Expand Up @@ -1180,7 +1180,7 @@ def setup
$('search').focus();
});

//# sourceMappingURL=application.js-95e519d4e0f0a5c4c7d24787ded990b0d027f7ad30b39f402c4c5e3196a41e8b.map
//# sourceMappingURL=application.js-ba55f2ffb2663c056b196f7874897ca13fc2fb892dfdda1f9535d105e3c9ee25.map
EOS

assert_equal expected, @asset.to_s
Expand Down
3 changes: 2 additions & 1 deletion test/test_coffee_script_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ def test_compile_coffee_script_template_to_js
result = Sprockets::CoffeeScriptProcessor.call(input)
assert result[:data].match(/var square/)
assert_equal 13, Sprockets::SourceMapUtils.decode_source_map(result[:map])[:mappings].size
assert_equal ["squared.source.coffee"], result[:map]["sources"]
assert_equal ["squared.coffee"], result[:map]["sources"]
assert_nil result[:map]["sourcesContent"]
end

def test_cache_key
Expand Down