从 --watch 中排除 Jekyll 目录,但不构建

Exclude Jekyll directory from --watch, but not build

我有一个与 Excluding a directory from Jekyll watch 非常相似的问题,但我认为它有足够的不同以保证它自己的问题。


_config.yml:

exclude: [
  "my_ignored_directory"
]

使用 jekyll build --watch,文件夹 my_ignored_directory 将被监视任务成功忽略,但也不会被构建。

是否有方法可以将我选择的文件夹从 --watch 任务中排除,但又不会被排除在构建之外?

(注:控制台没有报错)

答案是否定的,因为watch排除的文件有:_config.*destination folder(通常是_site)和config['exclude'].

的内容

jekyll-watch code

编辑:但是...

我们可以用插件覆盖 Jekyll::Watcher::custom_excludes 方法。

_plugins/jekyll-watcher-override.rb

require 'jekyll-watch'
module Jekyll
  module Watcher
    # replace old method by new method
    # new method is now :custom_excludes
    # overridden method is now :old_custom_excludes
    alias_method :old_custom_excludes, :custom_excludes
    def custom_excludes(options)
      # if we have an "exclude_from_watch" variable in configuration
      if options['exclude_from_watch'] then
        # merge exclude and exclude_from_watch
        (options['exclude'] << options['exclude_from_watch']).flatten!
      end
      # pass the new option array to overridden method
      old_custom_excludes(options)
    end
  end
end

在_config.yml中,只需设置一个exclude_from_watch变量:

exclude_from_watch:
  - css

现在,当您执行 jekyll serve 时,exclude_from_watch 中的任何文件/文件夹都将被忽略。