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

Add source map on debug mode with Sprockets 4 #162

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion lib/sassc/rails/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")

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.

Copy link
Author

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 :

Sprockets::VERSION.to_i >= 4
Gem.loaded_specs['sprockets'].version.segments.first >= 4
!Sprockets::VERSION.start_with?("3")

Sassc-rails doesn't support sprockets < 3 so, because the last line is the simplest, I think this is the better option.

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)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map metadata wasn't transmitted to sprockets and wasn't aware of dependencies, so it wasn't able to generate the source map with complete data.

else
context.metadata.merge(data: css)
end
end

def config_options
Expand Down
14 changes: 14 additions & 0 deletions test/sassc_rails_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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!

Expand All @@ -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")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should definitely write the test so if sprockets 5 is ever released, the test will be run under sprockets 5 too and raise on failure, instead of just skipping this test!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed to make this working with Sprockets >= 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

Expand Down