-
Notifications
You must be signed in to change notification settings - Fork 106
Add source map on debug mode with Sprockets 4 #162
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,13 +34,36 @@ def call(input) | |
| } | ||
| }.merge!(config_options) { |key, left, right| safe_merge(key, left, right) } | ||
|
|
||
| if Rails.application.config.assets.debug && Sprockets::VERSION.start_with?("4") | ||
| options.merge!( | ||
| source_map_contents: false, | ||
| source_map_file: "#{input[:filename]}.map", | ||
| omit_source_map_url: true, | ||
| ) | ||
| end | ||
|
|
||
| engine = ::SassC::Engine.new(input[:data], options) | ||
|
|
||
| css = Sprockets::Utils.module_include(::SassC::Script::Functions, @functions) do | ||
| engine.render | ||
| end | ||
|
|
||
| context.metadata.merge(data: css) | ||
| if Rails.application.config.assets.debug && Sprockets::VERSION.start_with?("4") | ||
| begin | ||
| map = Sprockets::SourceMapUtils.format_source_map(JSON.parse(engine.source_map), input) | ||
| map = Sprockets::SourceMapUtils.combine_source_maps(input[:metadata][:map], map) | ||
|
|
||
| engine.dependencies.each do |dependency| | ||
| context.metadata[:dependencies] << Sprockets::URIUtils.build_file_digest_uri(dependency.filename) | ||
| end | ||
| rescue SassC::NotRenderedError | ||
| map = input[:metadata][:map] | ||
| end | ||
|
|
||
| context.metadata.merge(data: css, map: map) | ||
|
||
| else | ||
| context.metadata.merge(data: css) | ||
| end | ||
| end | ||
|
|
||
| def config_options | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -225,6 +225,7 @@ def test_compression_works | |
| end | ||
|
|
||
| def test_sassc_compression_is_used | ||
| @app.config.assets.debug = false | ||
| engine = stub(render: "") | ||
| SassC::Engine.expects(:new).returns(engine) | ||
| SassC::Engine.expects(:new).with("", {style: :compressed}).returns(engine) | ||
|
|
@@ -235,6 +236,7 @@ def test_sassc_compression_is_used | |
| end | ||
|
|
||
| def test_allows_for_inclusion_of_inline_source_maps | ||
| @app.config.assets.debug = false | ||
| @app.config.sass.inline_source_maps = true | ||
| initialize_dev! | ||
|
|
||
|
|
@@ -243,6 +245,18 @@ def test_allows_for_inclusion_of_inline_source_maps | |
| assert_match /sourceMappingURL/, asset | ||
| end | ||
|
|
||
| def test_adds_source_map_in_debug_mode | ||
| if Sprockets::VERSION.start_with?("4") | ||
|
||
| @app.config.assets.debug = true | ||
| initialize_dev! | ||
|
|
||
| asset = render_asset("glob_test.debug.css") | ||
| assert_equal app.assets["glob_test.css"].metadata[:map]["sections"].first["map"]["sources"], ["glob_test.source.scss", "globbed/globbed.source.scss", "globbed/nested/nested_glob.source.scss"] | ||
| assert_match /.globbed/, asset | ||
| assert_match /sourceMappingURL/, asset | ||
| end | ||
| end | ||
|
|
||
| #test 'sprockets require works correctly' do | ||
| # skip | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps to be forward compatible this should be written in a way that it will still be enabled for sprockets 5 and other >4? It seems likely this will continue to be needed -- if it causes problems, sassc-rails will be broken with sprockets 5 either way, so doing this even in Sprockets 5+ seems the most likely to lead to things keeping working when/if a sprockets 5 is released.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked and there are some options :
Sassc-rails doesn't support sprockets < 3 so, because the last line is the simplest, I think this is the better option.