如何使用Sass :: Engine的render_with_sourcemap

时间:2016-07-22 10:22:23

标签: ruby sass

在查看documentation for render_with_sourcemap后(很遗憾无法找到任何示例用法),我觉得以下情况应该有效:

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

然而,我得到的错误是:

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.

它只需使用 render (不需要参数)而不是 render_with_sourcemap ,所以我会相信我的文件名是错误的 - 但是,我不明白我做错了什么。我还尝试了/style.css.map./style.css.map@target_dir + style.css.map,都没有成功(得到同样的错误)

2 个答案:

答案 0 :(得分:1)

查看source code of the Engine class,我发现如果_render_with_sourcemap为零,则会从@options[:filename]抛出错误 - 使用该选项初始化函数会导致成功生成源映射。

以下代码是生成css文件和sourcemap以供将来参考所需的完整代码

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

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

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

答案 1 :(得分:0)

即使没有记录,您也应该设置filename变量。之后你应该得到一个sourcemap对象。