我正在使用 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 将按照您传递它们的顺序应用它们,因此第二个文件中的设置将覆盖第一个文件中的设置,bundle
和 compress
将被最后设置为 true
。
如果您无法控制将哪些参数传递给 jekyll build
(也许在 GitHub Pages 上?我从未使用过它,但也许......) 你可以做同样的事情,只是反过来:
- 在默认配置文件中将
bundle
和compress
设置为true
- 每当您不发布时,使用第二个
_config-dev.yml
文件来设置bundle
和compress
再次为false
关于ruby - 如何覆盖 Jekyll 构建命令以仅在构建时设置一些配置选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18159018/