如何使用 Windows 命令行构建 Jekyll 项目生产

How to build Jekyll project production with Windows command line

我尝试使用 JEKYLL_ENV 变量在生产模式下构建我的 jekyll 项目,但它不起作用。

Jekyll 文档指定在构建命令中设置 production 环境:

JEKYLL_ENV=production jekyll build

但是在Windows上,这种语法是不正确的。 我使用了以下语法,但它看起来不起作用:

jekyll build JEKYLL_ENV=production

我也设置了'manually'这个环境变量但是没有生效:

setx JEKYLL_ENV production & jekyll build

set JEKYLL_ENV=production & jekyll build

我 运行 以及我的 Windows/Jekyll 设置。我的解决方法是拥有生产和开发配置文件并在每个文件中设置 environment 变量。

// _config.yml
environment: production
...<other config settings>...

--------

// _config_dev.yml
environment: development

您的生产环境应该 运行 jekyll build 自动使用 _config.yml。您的开发环境应该 运行 jekyll <command> --config _config.yml,_config_dev.yml。在 Jekyll config docs, "Settings in later [config] files override settings in earlier files." 所以,你可以在 prod 和 dev 配置文件中设置变量,并使用 --config _config.yml,_config_dev.yml 在 dev.

中设置变量

要在 Jekyll 中使用它,请使用 Liquid 语句检查环境变量。 Config files set site 个变量,所以检查 site.environment

// some file that will be processed by Jekyll
{% if site.environment == "production" %}
    <do prod stuff>
{% elsif site.environment == "development" %}
    <do dev stuff>
{% endif %}

在 windows 上你应该 运行 两个命令:

第一个命令将 env 设置为生产环境

set JEKYLL_ENV=production

第二个命令运行jekyll buildjekyll server

jekyll build

当您使用开发 evn 时,运行 再次执行此命令:

 set JEKYLL_ENV=development

这对我有用:

set JEKYLL_ENV=production | jekyll build

运行 这两个命令分别像 Thxopen 建议的那样工作。然后我尝试 运行 一行中使用分隔符 here 中提到的两个命令,并且效果很好。