Skip to content

Commit

Permalink
Expand source paths on Source Maps generated by the SassProcessor
Browse files Browse the repository at this point in the history
The original `source` values are the absolute file paths of each imported/processed
stylesheet, which must be replaced by their `source_path` counterpart so we can
serve its content to the browser.
  • Loading branch information
lucasmazza committed Feb 25, 2016
1 parent b847bbf commit 34de4ea
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
17 changes: 14 additions & 3 deletions lib/sprockets/sass_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def call(input)

map = SourceMapUtils.combine_source_maps(
input[:metadata][:map],
SourceMapUtils.decode_json_source_map(map.to_json(css_uri: '', type: :inline))["mappings"]
expand_map_sources(
SourceMapUtils.decode_json_source_map(map.to_json(css_uri: '', type: :inline))["mappings"],
input[:environment]
)
)

# Track all imported files
Expand All @@ -94,6 +97,16 @@ def call(input)
context.metadata.merge(data: css, sass_dependencies: sass_dependencies, map: map)
end

private

def expand_map_sources(mapping, env)
mapping.each do |map|
uri, _ = env.resolve!(map[:source], pipeline: :source)
source_path = env.load(uri).digest_path
map[:source] = source_path
end
end

# Public: Build the cache store to be used by the Sass engine.
#
# input - the input hash.
Expand All @@ -104,7 +117,6 @@ def call(input)
def build_cache_store(input, version)
CacheStore.new(input[:cache], version)
end
private :build_cache_store

def merge_options(options)
defaults = @sass_config.dup
Expand All @@ -116,7 +128,6 @@ def merge_options(options)
options.merge!(defaults)
options
end
private :merge_options

# Public: Functions injected into Sass context during Sprockets evaluation.
#
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/source-maps/sass/_imported.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
body { color: red; }
3 changes: 3 additions & 0 deletions test/fixtures/source-maps/sass/with-import.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@import 'imported';

nav { color: blue; }
39 changes: 37 additions & 2 deletions test/test_source_maps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,50 @@ def setup
assert_equal "sass/main.css.map", asset.logical_path
assert_equal "application/css-sourcemap+json", asset.content_type
assert_equal [
"file://#{fixture_path_for_uri('source-maps/sass/main.scss')}?type=text/scss"
"file://#{fixture_path_for_uri('source-maps/sass/main.scss')}?type=text/scss&pipeline=source"
], normalize_uris(asset.links)

assert map = JSON.parse(asset.source)
assert_equal({
"version" => 3,
"file" => "sass/main.css",
"mappings" => "AACE,MAAG;EACD,MAAM,EAAE,CAAC;EACT,OAAO,EAAE,CAAC;EACV,UAAU,EAAE,IAAI;AAGlB,MAAG;EAAE,OAAO,EAAE,YAAY;AAE1B,KAAE;EACA,OAAO,EAAE,KAAK;EACd,OAAO,EAAE,QAAQ;EACjB,eAAe,EAAE,IAAI",
"sources" => [fixture_path_for_uri('source-maps/sass/main.scss')],
"sources" => ["sass/main.source-86fe07ad89fecbab307d376bcadfa23d65ad108e3735b564510246b705f6ced1.scss"],
"names" => []
}, map)
end

test "compile scss source map with imported dependencies" do
asset = silence_warnings do
@env.find_asset("sass/with-import.css")
end
assert asset
assert_equal fixture_path('source-maps/sass/with-import.scss'), asset.filename
assert_equal "text/css", asset.content_type

assert_match "body {\n color: red; }", asset.source

asset = silence_warnings do
@env.find_asset("sass/with-import.css.map")
end
assert asset
assert_equal fixture_path('source-maps/sass/with-import.scss'), asset.filename
assert_equal "sass/with-import.css.map", asset.logical_path
assert_equal "application/css-sourcemap+json", asset.content_type
assert_equal [
"file://#{fixture_path_for_uri('source-maps/sass/_imported.scss')}?type=text/scss&pipeline=source",
"file://#{fixture_path_for_uri('source-maps/sass/with-import.scss')}?type=text/scss&pipeline=source"
], normalize_uris(asset.links)

assert map = JSON.parse(asset.source)
assert_equal({
"version" => 3,
"file" => "sass/with-import.css",
"mappings" => "AAAA,IAAK;EAAE,KAAK,EAAE,GAAG;;ACEjB,GAAI;EAAE,KAAK,EAAE,IAAI",
"sources" => [
"sass/_imported.source-9767e91e9d4b0334e59a1d389e9801bc6a2c5c4a5500a3c2c7915687965b2c16.scss",
"sass/with-import.source-5d53742ba113ac26396986bf14ab5c7e19ef193e494d5d868a9362e3e057cb26.scss"
],
"names" => []
}, map)
end
Expand Down

0 comments on commit 34de4ea

Please sign in to comment.