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

标签 ruby jekyll

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

asset_pipeline:
  bundle: false
  compress: false

我尝试编写一个插件,但它不起作用。谁能帮我看看为什么?

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

最佳答案

您甚至不需要特殊的 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

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

_config-publish.yml:

asset_pipeline:
  bundle: true
  compress: true

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

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

jekyll build --config _config.yml,_config-publish.yml

Jekyll 将按照您传递它们的顺序应用它们,因此第二个文件中的设置将覆盖第一个文件中的设置,bundlecompress 将被最后设置为 true


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

  • 在默认配置文件中将bundlecompress设置为true
  • 每当您发布时,使用第二个_config-dev.yml 文件来设置bundlecompress 再次为 false

关于ruby - 如何覆盖 Jekyll 构建命令以仅在构建时设置一些配置选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18159018/

相关文章:

css - 如何为 Jekyll 添加自动前缀

ruby-on-rails - rails 4.2 : Unknown Attribute or Server Error in Log

javascript - 将二维数组从 ruby​​ 传递到 javascript

python - 纺织品解析 YAML

rubygems - 无法创建新的 jekyll 站点 : "Could not locate Gemfile or .bundle/directory"

ruby - 有没有办法让文件夹包含在生产构建中但不让 jekyll 编译它?

ruby - 为什么这个 Jekyll Liquid where filter 不过滤?

ruby ,Mongodb, Anemone : web crawler with possible memory leak?

ruby-on-rails - 如何检查 Globalize 是否使用了回退

ruby-on-rails - rails : How to enter value A in Model X only if value A exists in Model Y?