Skip to content

Commit

Permalink
Merge pull request #255 from lucasmazza/lm-source-map-fixes
Browse files Browse the repository at this point in the history
Assorted Source Maps patches
  • Loading branch information
rafaelfranca committed Feb 26, 2016
2 parents d2bf7d1 + 34de4ea commit 8824f88
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 19 deletions.
3 changes: 2 additions & 1 deletion lib/sprockets/babel_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def call(input)
'sourceRoot' => input[:load_path],
'moduleRoot' => nil,
'filename' => input[:filename],
'filenameRelative' => PathUtils.split_subpath(input[:load_path], input[:filename])
'filenameRelative' => PathUtils.split_subpath(input[:load_path], input[:filename]),
'sourceFileName' => input[:source_path]
}.merge(@options)

if opts['moduleIds'] && opts['moduleRoot']
Expand Down
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
2 changes: 1 addition & 1 deletion lib/sprockets/source_map_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.call(input)
path = source
end
uri, _ = env.resolve!(path)
links << env.load(uri).uri
links << uri
end

json = env.encode_json_source_map(map, filename: asset.logical_path)
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; }
24 changes: 16 additions & 8 deletions test/test_babel_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def test_compile_es6_features_to_es5
metadata: {},
load_path: File.expand_path("../fixtures", __FILE__),
filename: File.expand_path("../fixtures/mod.es6", __FILE__),
cache: Sprockets::Cache.new
cache: Sprockets::Cache.new,
source_path: "mod.source-XYZ.es6"
}

assert js = Sprockets::BabelProcessor.call(input)[:data]
Expand All @@ -25,7 +26,8 @@ def test_transform_arrow_function
metadata: {},
load_path: File.expand_path("../fixtures", __FILE__),
filename: File.expand_path("../fixtures/mod.es6", __FILE__),
cache: Sprockets::Cache.new
cache: Sprockets::Cache.new,
source_path: "mod.source-XYZ.es6"
}

assert js = Sprockets::BabelProcessor.call(input)[:data]
Expand All @@ -43,7 +45,8 @@ def test_common_modules
metadata: {},
load_path: File.expand_path("../fixtures", __FILE__),
filename: File.expand_path("../fixtures/mod.es6", __FILE__),
cache: Sprockets::Cache.new
cache: Sprockets::Cache.new,
source_path: "mod.source-XYZ.es6"
}

assert js = Sprockets::BabelProcessor.new('modules' => 'common').call(input)[:data]
Expand All @@ -59,7 +62,8 @@ def test_amd_modules
metadata: {},
load_path: File.expand_path("../fixtures", __FILE__),
filename: File.expand_path("../fixtures/mod.es6", __FILE__),
cache: Sprockets::Cache.new
cache: Sprockets::Cache.new,
source_path: "mod.source-XYZ.es6"
}

assert js = Sprockets::BabelProcessor.new('modules' => 'amd').call(input)[:data]
Expand All @@ -75,7 +79,8 @@ def test_amd_modules_with_ids
metadata: {},
load_path: File.expand_path("../fixtures", __FILE__),
filename: File.expand_path("../fixtures/mod.es6", __FILE__),
cache: Sprockets::Cache.new
cache: Sprockets::Cache.new,
source_path: "mod.source-XYZ.es6"
}

assert js = Sprockets::BabelProcessor.new('modules' => 'amd', 'moduleIds' => true).call(input)[:data]
Expand All @@ -91,7 +96,8 @@ def test_system_modules
metadata: {},
load_path: File.expand_path("../fixtures", __FILE__),
filename: File.expand_path("../fixtures/mod.es6", __FILE__),
cache: Sprockets::Cache.new
cache: Sprockets::Cache.new,
source_path: "mod.source-XYZ.es6"
}

assert js = Sprockets::BabelProcessor.new('modules' => 'system').call(input)[:data]
Expand All @@ -112,7 +118,8 @@ def test_system_modules_with_ids
metadata: {},
load_path: File.expand_path("../fixtures", __FILE__),
filename: File.expand_path("../fixtures/mod.es6", __FILE__),
cache: Sprockets::Cache.new
cache: Sprockets::Cache.new,
source_path: "mod.source-XYZ.es6"
}

assert js = Sprockets::BabelProcessor.new('modules' => 'system', 'moduleIds' => true).call(input)[:data]
Expand All @@ -133,7 +140,8 @@ def test_caching_takes_filename_into_account
metadata: {},
load_path: File.expand_path("../fixtures", __FILE__),
filename: File.expand_path("../fixtures/mod1.es6", __FILE__),
cache: Sprockets::Cache.new
cache: Sprockets::Cache.new,
source_path: "mod1.source-XYZ.es6"
}

mod2 = mod1.dup
Expand Down
47 changes: 41 additions & 6 deletions test/test_source_maps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def setup
assert_equal "coffee/main.js.map", asset.logical_path
assert_equal "application/js-sourcemap+json", asset.content_type
assert_equal [
"file://#{fixture_path_for_uri('source-maps/coffee/main.coffee')}?type=text/coffeescript&pipeline=source&id=xxx"
"file://#{fixture_path_for_uri('source-maps/coffee/main.coffee')}?type=text/coffeescript&pipeline=source"
], normalize_uris(asset.links)

assert map = JSON.parse(asset.source)
Expand Down Expand Up @@ -108,15 +108,15 @@ def setup
assert_equal "babel/main.js.map", asset.logical_path
assert_equal "application/js-sourcemap+json", asset.content_type
assert_equal [
"file://#{fixture_path_for_uri('source-maps/babel/main.es6')}?type=application/ecmascript-6&id=xxx"
"file://#{fixture_path_for_uri('source-maps/babel/main.es6')}?type=application/ecmascript-6&pipeline=source"
], normalize_uris(asset.links)

assert map = JSON.parse(asset.source)
assert_equal({
"version" => 3,
"file" => "babel/main.js",
"mappings" => ";;;;;;;;;;AACA,IAAI,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,UAAA,CAAC;SAAI,CAAC,GAAG,CAAC;CAAA,CAAC,CAAC;AACjC,IAAI,IAAI,GAAG,KAAK,CAAC,GAAG,CAAC,UAAC,CAAC,EAAE,CAAC;SAAK,CAAC,GAAG,CAAC;CAAA,CAAC,CAAC;;IAEhC,WAAW;YAAX,WAAW;;AACJ,WADP,WAAW,CACH,QAAQ,EAAE,SAAS,EAAE;0BAD7B,WAAW;;AAEb,+BAFE,WAAW,6CAEP,QAAQ,EAAE,SAAS,EAAE;GAE5B;;eAJG,WAAW;;WAKT,gBAAC,MAAM,EAAE;AACb,iCANE,WAAW,wCAME;KAChB;;;WACmB,yBAAG;AACrB,aAAO,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC;KAC5B;;;SAVG,WAAW;GAAS,KAAK,CAAC,IAAI;;AAapC,IAAI,SAAS,uBACV,MAAM,CAAC,QAAQ,0BAAG;MACb,GAAG,EAAM,GAAG,EAEV,IAAI;;;;AAFN,WAAG,GAAG,CAAC,EAAE,GAAG,GAAG,CAAC;;;AAEd,YAAI,GAAG,GAAG;;AACd,WAAG,GAAG,GAAG,CAAC;AACV,WAAG,IAAI,IAAI,CAAC;;eACN,GAAG;;;;;;;;;;;CAEZ,EACF,CAAA",
"sources" => ["babel/main.es6"],
"sources" => ["babel/main.source-1acb9cf16a3e1ce0fe0a38491472a14a6a97281ceace4b67ec16a904be5fa1b9.es6"],
"names"=>[]
}, map)
end
Expand Down 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&id=xxx"
"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 Expand Up @@ -226,7 +261,7 @@ 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('source-maps/sass/main.scss')}?type=text/scss&pipeline=source&id=xxx"
"file://#{fixture_path('source-maps/sass/main.scss')}?type=text/scss&pipeline=source"
], normalize_uris(asset.links)

assert map = JSON.parse(asset.source)
Expand Down

0 comments on commit 8824f88

Please sign in to comment.