【问题标题】:Make less-rails add line numbers as comment让 less-rails 添加行号作为注释
【发布时间】:2015-02-02 14:28:32
【问题描述】:

有什么方法可以配置less-rails 以便将原始源的行号显示为 cmets?是否可以启用源映射?

考虑以下示例:

file1.less

body {
  background-color: black;
}

file2.less

body {
  padding: 20px;
}

application.css.less

@import "file1";
@import "file1";

我得到了什么:

body {
  background-color: black;
}

body {
  padding: 20px;
}

我想要什么

/* file1.less line: 1 */
body {
  background-color: black;
}

/* file2.less line: 1 */
body {
  padding: 20px;
}

或者有没有其他更简单的方法来找出哪个规则属于哪个文件?

更新

在配置我的应用程序时,config.less 仅包含以下内容:

{:paths=>
  [#<Pathname:/home/yan-foto/.../vendor/less>,
   #<Pathname:/home/yan-foto/.../node_modules/bootstrap-datetimepicker>,
   #<Pathname:/home/yan-foto/.../node_modules/bootstrap-slider>],
 :compress=>false}

【问题讨论】:

    标签: ruby-on-rails less asset-pipeline less-rails


    【解决方案1】:

    打开 vendor/bundle/ruby/1.9.1/gems/less-2.6.0/lib/less/parser.rb 文件并替换(大约第 54 行):

      end
      @parser = Less::JavaScript.exec { Less['Parser'].new(env) }
    

    与:

      end
      env['dumpLineNumbers'] = 'comments';
      @parser = Less::JavaScript.exec { Less['Parser'].new(env) }
    

    基于https://github.com/metaskills/less-rails#configuration,您应该尝试:

    MyProject::Application.configure do
      config.less.line-numbers << "comments"
      config.less.compress = true
    end
    

    当上述工作按预期工作时,您还可以考虑使用 CSS 源映射:

    MyProject::Application.configure do
      config.less.source-map = true
      config.less.source-map-map-inline = true
    end
    

    我真的没有找到 config.less.line-numbers

    事实上,我的回答只是一个建议,我无法对其进行测试。以上建议您可以为 Less 编译器设置一些选项。

    您也可以通过不带任何参数运行lessc 来找到此选项:

    -------------------------- Deprecated ----------------
      --line-numbers=TYPE      Outputs filename and line numbers.
                               TYPE can be either 'comments', which will output
                               the debug info within comments, 'mediaquery'
                               that will output the information within a fake
                               media query which is compatible with the SASS
                               format, and 'all' which will do both.
      --verbose                Be verbose.
    

    而且我确信它有语法错误,因为行之间的破折号 和数字

    我敢打赌你是对的。您应该可以使用:config.less.dumpLineNumbersconfig.less.sourceMap

    或者在你的config.less::dumpLineNumbers&gt;comments

    【讨论】:

    • 我真的没有在文档中找到 config.less.line-numbers &lt;&lt; "comments" 行,我确信它有语法错误,因为 linenumbers 之间的破折号。这条神秘的线是从哪里来的?
    • 感谢您抽出宝贵时间。我已经阅读了整个源代码,阅读了lessc 文档,等等。我仍然没有找到可行的解决方案。恕我直言,您的回答确实是推测性的。
    • > 您的回答确实是推测性的。确实是的。我希望它能为您指明正确的方向。
    • 谢谢。我想我对rails-less 有误解。无论如何,我感谢您的帮助。
    • 我真的不愿意更改宝石来源,但你是这里最好的人之一!感谢您投入这么多时间!
    猜你喜欢
    • 2014-11-22
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多