首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Guard与guard-sass,guard-haml和guard-haml不一致

Guard与guard-sass,guard-haml和guard-haml不一致
EN

Stack Overflow用户
提问于 2014-08-13 15:31:53
回答 1查看 72关注 0票数 0

我希望我的大多数项目都有一个单一的配置。在那里它会在我的项目文件夹中搜索任何sass,haml或sass文件夹。当我添加一个新位置来制作sass文件时,我不想更新配置。

开始文件夹结构

代码语言:javascript
复制
 - sass
     - test.sass
 - coffee
     - test.coffee
 - haml
     - test.html.haml
 - a
     - folder
         - somewhere 
             - deep
                 - sass
                     - test2.sass
                 - coffee
                     - test2.coffee
                 - haml
                     - test2.html.haml
 - Guardfile

包含生成的文件和文件夹的所需文件夹结构:

代码语言:javascript
复制
 - sass
     - test.sass
 - css
     - test.css
 - coffee
     - test.coffee
 - js
     - test.js
 - haml
     - test.html.haml
 - test.html
 - a
     - folder
         - somewhere 
             - deep
                 - sass
                     - test2.sass
                 - css
                     - test2.css
                 - coffee
                     - test2.coffee
                 - js
                     - test2.js
                 - haml
                     - test2.html.haml
                 - test2.html
 - Guardfile

到目前为止,haml与

代码语言:javascript
复制
guard :haml, input: 'haml' do
  watch(/^.+(\.html\.haml)$/)
end

和sass,coffeescript使用完全相同的代码以不同的方式工作

代码语言:javascript
复制
guard :coffeescript, input: 'coffee', output: 'js' do 
  watch(/^.+(\.coffee)$/) 
end 

guard :sass, input: 'sass', output: 'css' do
  watch(/^.+(\.sass)$/)
end

最终结果是将目标css和js文件夹放在projectfolder的根目录下。即使我删除了output:属性,它也会在根目录创建一个make and sass文件夹。

结果文件夹结构

代码语言:javascript
复制
 - sass
     - test.sass
 - css
     - test.css
     - test2.css
 - coffee
     - test.coffee
 - js
     - test.js
     - test2.js
 - haml
     - test.html.haml
 - test.html
 - a
     - folder
         - somewhere 
             - deep
                 - sass
                     - test2.sass
                 - coffee
                     - test2.coffee
                 - haml
                     - test2.html.haml
                 - test2.html
 - Guardfile

我不知道这是怎么回事,有人能告诉我吗?

使用Guard版本2.6.1

EN

回答 1

Stack Overflow用户

发布于 2014-12-17 17:07:34

每个插件都有自己关于如何解释/使用目录的想法。

在coffeescript中,有一个“技巧”可以告诉它输出目录,方法是将输入目录部分放在regexp组中,例如:

代码语言:javascript
复制
guard :coffeescript, input: 'coffee', output: 'js' do
  watch(/^(.+)\\.coffee$/)
end

但我猜这会创建js/a/folder/somewhere/deep,而你想要a/folder/somewhere/deep/js

如果你不指定输出目录,你可能会得到:a/folder/somewhere/deep -它很接近,但是在最后没有/js/文件夹。

实际上,Haml文件是不同的,因为虽然您希望(...)coffee/test2.coffee(...)/js/test2.js的形式结束,但您希望(...)/haml/test2.html.haml(...)/test2.html.haml的形式结束,而不是(...)/html/test2.html.haml (意思是:您不希望它位于html子文件夹中-我同意这将是愚蠢的)……

..。这意味着guard-haml是你所期望的其他插件中的例外。

所以这个短篇故事是:

您不能期望guard-coffeescriptguard-sass的行为像guard-haml, because at the same time you're expecting them to *not* behave the same way (nohtmloutput folder for.haml`文件-这显然是可以理解的)。

在haml中,工作方式如您所愿,只是因为您没有为它提供:output选项(尝试一下,看看会发生什么)。

同时,guard-coffeescript已经(由我)更新为使用较新版本的guard,因此模板现在有点不同,您可能需要:

代码语言:javascript
复制
input_dir = 'a/folder/somewhere/deep'

coffeescript_options = {
  input: "#{input_dir}/coffee,
  output: "#{input_dir}/js",
  patterns: [%r{^#{input_dir}/(.+\.(?:coffee|coffee\.md|litcoffee))$}]
}

guard 'coffeescript', coffeescript_options do
  coffeescript_options[:patterns].each { |pattern| watch(pattern) }
end

guard-sass尚未针对更新版本的Guard进行更新(事实上,它从未迁移到Guard2.0),因此我甚至不会费心去研究它。

我看到的选择是:

  • 对防护进行更改-咖啡脚本可执行您想要的操作(reverse input/output folder order)
  • reorganize your structure (可能建议更快)

因为Guardfile是纯ruby的,所以你可以选择迭代--如果这有帮助的话,例如:

代码语言:javascript
复制
# I don't know which parts of the structure are projects, so I'm guessing it's the first part
Dir[*].each do |project|
  next unless File.directory?(project)
  next if %w(sass css coffee js).include?(project) #(skip top level non-project folders)
  input_dir = "#{project}/a/folder/somewhere/deep"

  coffeescript_options = {
    input: "#{input_dir}/coffee,
    output: "#{input_dir}/js",
    patterns: [%r{^#{input_dir}/(.+\.(?:coffee|coffee\.md|litcoffee))$}]
  }

  guard 'coffeescript', coffeescript_options do
    coffeescript_options[:patterns].each { |pattern| watch(pattern) }
  end

  # guard 'sass' would go here with a similar setup
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25280480

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档