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 all commits
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
24 changes: 23 additions & 1 deletion lib/sassc/rails/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def initialize(options = {}, &block)

def call(input)
context = input[:environment].context_class.new(input)
support_source_maps = Rails.application.config.assets.debug && !Sprockets::VERSION.start_with?("3")

options = {
filename: input[:filename],
Expand All @@ -34,13 +35,34 @@ def call(input)
}
}.merge!(config_options) { |key, left, right| safe_merge(key, left, right) }

if support_source_maps
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)
return context.metadata.merge(data: css) unless support_source_maps

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)
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
unless Sprockets::VERSION.start_with?("3")
@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