TheThirdMan TheThirdMan - 6 months ago 45
Ruby Question

How to use Sass::Engine's render_with_sourcemap

After looking at the documentation for render_with_sourcemap (and unfortunately unable to find any example uses of this), I was under the impression that the following should work:

@source_dir = './sass/'
@target_dir = './css/'
@output = + 'style.scss'), {
cache_location: @source_dir + '.sass-cache',
style: :compressed,
syntax: :scss
}).render_with_sourcemap(@target_dir + '')

However, the error I'm getting is:

Error generating source map: couldn't determine public URL for the source stylesheet. (Sass::SyntaxError)
No filename is available so there's nothing for the source map to link to.

It works by simply using render (which doesn't need a parameter) instead of render_with_sourcemap, so I'm led to believe my filename is erroneous - however, I don't see what I'm doing wrong. I also tried
@target_dir +
, all with no success (getting the same error)


Looking at the source code of the Engine class, I found that the error is thrown from _render_with_sourcemap if @options[:filename] is nil - initializing the function with that option resulted in successfully generating the sourcemap.

The following code is the entire code needed to generate a css file and sourcemap for future reference

@source = './sass/'
@target = './css/'
@output = + 'style.scss'), {
    cache_location: @source + '.sass-cache',
    filename: 'style.css',
    style: :compressed,
    syntax: :scss
}).render_with_sourcemap(@target + '')

# write css to file
File.write(@target, @output[0])

# write sourcemap to file
sourcemap_options = {
  :css_path => @target,
  :sourcemap_path => @target + ''
File.write(@target + '', @output[1].to_json(sourcemap_options))