【问题标题】:How to detect if a SCSS file or any of it partial files is updated如何检测 SCSS 文件或其任何部分文件是否已更新
【发布时间】:2013-10-08 19:19:47
【问题描述】:

我想在 Ruby 脚本中运行 sass。编译scss文件scss可以这样做:

require "sass"
Sass::Engine.new(File.read(scss), options).render

options 分配了一些适当的哈希值,但我只想在scss 或其任何部分(导入)文件(如果已更新)时编译它。

options 中,我打开了 sass 缓存。缓存将相关文件的所有更新信息保存在某个目录中。我觉得,通过做到以下几点:

engine = Sass::Engine.new(File.read(scss), options)

engine 中必须有可用的信息,我可以通过这些信息判断scss 或其任何部分文件是否已更新。只有在这种情况下,我才应该运行:

engine.render

进行编译。如何根据 sass 缓存检测文件更新?

【问题讨论】:

  • 如果您只想在 scss 文件更改时对其进行编译,您可以查看 guard-sass 之类的内容。
  • 我不想将它作为一个守护进程运行。正如我在问题中所写,我想在 Ruby 代码中执行此操作。

标签: css ruby sass fileupdate


【解决方案1】:

你可以使用:

Sass::Engine.for_file(scss, options).render

这将检查scss文件哈希的缓存目录中是否有缓存的解析树。在此处查看Sass::Engine.for_file 的文档:http://sass-lang.com/docs/yardoc/Sass/Engine.html

缓存查找在Sass::Engine#_to_tree方法中执行,源代码在这里:https://github.com/nex3/sass/blob/stable/lib/sass/engine.rb

编辑

Sass::Engine#_to_tree的前几行:

def _to_tree
  if (@options[:cache] || @options[:read_cache]) &&
      @options[:filename] && @options[:importer]
    key = sassc_key
    sha = Digest::SHA1.hexdigest(@template)

    if root = @options[:cache_store].retrieve(key, sha)
      root.options = @options
      return root
    end
  end
  # ...
end

【讨论】:

  • 如你所知,如果你真的阅读过Sass::Engine.for_file的源代码,我在询问之前做过,它所做的只是调用Sass::Engine.new(File.read(filename), options.merge(:filename => filename)),这与我在问题。
  • 您没有通过示例中的 :filename 选项。如果您将阅读作为render 一部分调用的Sass::Engine#_to_tree 源,您可以看到缓存查找发生的位置。
  • 我没有写我在options 中的内容,但我在options 中指定了:filename
猜你喜欢
  • 2015-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多