如何仅在构建时覆盖 Jekyll 构建命令以设置一些配置选项?

lia*_*dee 2 ruby jekyll

我正在使用Jekyll Asset Pipeline来构建我的网站,我只想在发布时压缩网站(大约需要 20 秒)。为此,我必须在配置文件中以编程方式启用这些值:

asset_pipeline:
  bundle: false
  compress: false
Run Code Online (Sandbox Code Playgroud)

我试图编写一个插件,但它不起作用。有人可以帮助我为什么吗?

module Jekyll
    module Commands
        # I overwrite this here so we only do heavy work (like compressing HTML and stuff)
        # when we are building the site, not when testing (which uses jekyll serve)
        class << Build
            alias_method :_process, :process
            def process(options)
                require 'jekyll-press'
                options['asset_pipeline']['bundle'] = true
                options['asset_pipeline']['compress'] = true
                _process(options)
            end
        end
    end
end
Run Code Online (Sandbox Code Playgroud)

Chr*_*cht 5

您甚至不需要特殊的 gem - 您可以将多个配置文件传递给jekyll build:

首先,常规配置文件,包含始终需要的所有设置,以及禁用压缩的值,因为您并不总是希望每次在本地构建时都运行它:

_config.yml:

destination: _site
source: src
markdown: rdiscount
# ... and many more settings that are always needed

asset_pipeline:
  bundle: false
  compress: false
Run Code Online (Sandbox Code Playgroud)

然后,您需要一个用于发布的第二个配置文件,它仅覆盖您实际想要不同的值:

_config-publish.yml:

asset_pipeline:
  bundle: true
  compress: true
Run Code Online (Sandbox Code Playgroud)

因此,当您不发布时,您jekyll build就像以前一样运行。

但是当你发布时,你以正确的顺序传递两个配置文件:

jekyll build --config _config.yml,_config-publish.yml
Run Code Online (Sandbox Code Playgroud)

杰奇将应用它们在你通过它们的顺序,所以在第二个文件中的设置将覆盖在第一个文件中的,并且bundle和compress将被设置为true在最后。


如果您无法控制将传递给哪些参数jekyll build (也许在 GitHub Pages 上?我从未使用过它,但也许...)您可以做同样的事情,只是反过来:

  • 设置bundle并compress以true默认的配置文件
  • 每当你不发布,使用第二个_config-dev.yml文件来设置bundle和compress对false再次