【问题标题】:How to use Sass::Engine's render_with_sourcemap如何使用 Sass::Engine 渲染_with_ sourcemap
【发布时间】:2016-08-02 21:08:42
【问题描述】:

在查看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,都没有成功(得到同样的错误)

【问题讨论】:

    标签: ruby sass


    【解决方案1】:

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

    以下代码是生成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))
    

    【讨论】:

      【解决方案2】:

      即使没有记录,您也应该设置文件名变量。之后,您应该获得一个 sourcemap 对象。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-25
        • 2018-10-02
        • 1970-01-01
        相关资源
        最近更新 更多