Jekyll: Specify whether a plugin is active in the development environment - jekyll

In my Jekyll site, I'm using the minify-html plugin. However, it causes the site build time to increase from 2-20 seconds to nearly 5 minutes, which is unacceptable during development. According to the documentation:
After installing, Jekyll's HTML output will be minified by default. If you configure env in your Jekyll configuration, HTML will be minified only when
env is set to production.
env: production
This documentation is useless as it doesn't explain how to configure env. As can be expected, setting env: production in _config.yml has no effect, as it doesn't specify what should vary based on environment. I've spent quite a lot of time searching the internet for how to make this work, but I've turned up nothing. As far as I can tell, env, along with any other method to vary by environment, is totally undocumented in Jekyll.
How can I set minify-html to only run when I call JEKYLL_ENV=production jekyll build?
PS: In case it's relevant, I installed minify-html by adding it to my Gemfile and running bundle.

no env variable set in `_config.yml``=> minification
env: production => minification
env: yolo or whatever or dev => no minification
You can have a development command, using a development config file that will override the default _config.yml. See command options
In a new _config_dev.yml, set :
env: dev
To serve minified : jekyll serve.
To serve unminified : jekyll serve --config _config.yml,_config_dev.yml

Related

Can a Jekyll plugin / generator be disabled for non-production environment?

I have an custom generator written for creating archive pages (per year, month and day) for my Jekyll blog. But that generator takes some time, can I disable a generator for non-production environment or maybe from the plugins entry in the config?
UPDATE: The plugin is just placed in the _pluginsfolder, it is not delivered / distributed as gem package!
I am looking for a kind of blacklisting of generators (and maybe also filters or other plugins) for development environment to save building time.
Local Plugins
For switching local plugins, you could use two different folders, and switch between them with a combination of multiple config files overwriting your plugins_dir.
You would set the a different value for plugins_dir field in the _config-dev.yml file which would overwrite your value in _config.yml (or the default _plugins if unset):
$ bundle exec jekyll build --config _config.yml,_config-dev.yml
This way you can have two folders with your development and production plugins separate. This has a maintenance cost when you use a plugin across both environments.
Gemfile Plugins
For switching gem-based plugins, you could use a different Gemfile for development without the production plugins:
BUNDLE_GEMFILE=Gemfile-dev bundle exec jekyll build
This gives you a lot of flexibility at the cost of maintaining two files. You'd want to ensure the versions of plugins across both are the same.
Alternatively, you could use an additional config file for development. You would set the a different value for plugins field in the _config-dev.yml file which would overwrite your value in _config.yml. You'd need to ensure your plugins are not set in the :jekyll_plugins group in your Gemfile for this to work (as this would shortcut the config setting):
$ bundle exec jekyll build --config _config.yml,_config-dev.yml
General Performance
If your site has a large number of posts, it's likely that your biggest time saving would be made by processing less of them at development time. You can do this with the limit_posts command line option (https://jekyllrb.com/docs/configuration/options/#build-command-options):
$ bundle exec jekyll build --limit_posts 5
For general build time improvements, I'd highly recommend profiling your site to find the best place to optimise:
$ bundle exec jekyll build --profile

Jekyll SystemStackErorr "stack level too deep" on serve with github pages

I can't serve a jekyll site locally and I get notified that there is a problem with GitHub pages when I commit to GitHub.
bundle exec jekyll -v 11:28:34
jekyll 3.7.3
ruby --version 11:28:42
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
When I run:
bundle exec jekyll serve
will fail and spit out hundreds (more like thousnads) of error messages starting with:
bundler: failed to load command: jekyll (/usr/local/bin/jekyll)
SystemStackError: stack level too deep
/usr/local/lib/ruby/gems/2.5.0/gems/kramdown-1.17.0/lib/kramdown/parser/base.rb:106:in `new'
/usr/local/lib/ruby/gems/2.5.0/gems/kramdown-1.17.0/lib/kramdown/parser/base.rb:106:in `add_text'
/usr/local/lib/ruby/gems/2.5.0/gems/kramdown-1.17.0/lib/kramdown/parser/html.rb:171:in `parse_raw_html'
/usr/local/lib/ruby/gems/2.5.0/gems/kramdown-1.17.0/lib/kramdown/parser/kramdown/html.rb:60:in `handle_kramdown_html_tag'
From what I could tell, this can sometimes happen if you use jekyll serve as opposed to bundle exec jekyll serve and if there is a mismatch between your version of jekyll and the one required by github pages, but here I do use bundle exec and so the correct version of jekyll is used.
jekyll itself works if i try to create a new site with jekyll new blah and i can serve that one locally without a problem, but the combo of jekyll and github pages is giving me grief.
I am puzzled and do not know how to proceed.
All this is on Mac OS X 10.13.6.
Not relating to the original cause of the problem here, but i stumbled upon this and had the error stack level too deep. So i guess this might help someone.
I traced my problem to a field that i named as max in some yaml dictionary in the front matter. Maybe you have named a field which has a special meaning in jekyll.

copy jekyll remote theme to local

I just noticed that jekyll has all it needs installed locally, but a remote-theme is fetched every time jekyll is started.
I've also seen that people proposed a cache extension which hasn't been merged yet.
So I wonder if it is possible to just clone the remote theme to a certain folder?
ok, found the solution:
most themes are available also as ruby gems. So instead of specifying a remote-theme, I add the corresponding gem to Gemfile
gem "minimal-mistakes-jekyll"
and then reference it in the _config.yml
theme: minimal-mistakes-jekyll

In Jekyll, how to exclude files based on the JEKYLL_ENV?

This seems like a very basic question but I can't find the answer :-S
I exclude like this:
exclude:
- "*.json"
- "Gemfile*"
- "*.txt"
- vendor
- README.md
- somefile.html
Pretty straightforward. To create the production build I run: $ JEKYLL_ENV=production bundle exec jekyll build
How can I exclude the somefile.html file only when I run the production ENV?
Perhaps you could try using a specific config file for production and one for development. In the production you could exclude the files using exclude as described here.
Then run:
jekyll build --trace --config _config.yml,_config_dev.yml
or
jekyll build --trace --config _config.yml,_config_prod.yml
In the _config.yml you'd set generic settings, and in the config with the environment suffix you'd set a environment specific configuration.
The trace flag is optional, it will help setting it up since it will show occuring errors.
optional: -t, --trace Show the full backtrace when an error occurs

what exactly is build the site (jekyll)?

this's a super noob question but, what exactly is "build the site" running this command:
`~/myblog $ bundle exec jekyll serve`
is this to start the server?, because I'm currently using $ jekyll serve :s
As mentioned in its Quick-start guide, this command will build the site on the preview server.
Update:
There is a difference between jekyll serve and bundle exec jekyll serve:
The Gemfile and Gemfile.lock files inform Bundler about the gem requirements in your site. If your site doesn’t have these Gemfiles, you can omit bundle exec and just run jekyll serve.
When you run bundle exec jekyll serve, Bundler uses the gems and versions as specified in Gemfile.lock to ensure your Jekyll site builds with no compatibility or dependency conflicts.
Update 2:
In simple words, "build the site" means the Bundler will use the gems and versions specified in Gemfile.lock to ensure your Jekyll site builds with no compatibility or dependency conflicts and then will run your site on the preview or local server.
The serve command is actually an extension of the build command.
i.e., when a Jekyll user runs jekyll serve or bundle exec jekyll serve, Jekyll first runs the jekyll build command internally and then starts the local server to host the generated site.
So what exactly is this build the site? Well, Jekyll being a static-site generator generates your site using the build command.
Therefore, whether you run jekyll build or jekyll serve, a default jekyll site gets built or generated into the ./_site folder. The minor difference with running the serve command being Jekyll will proceed to start a local web-server and mount the built / generated site onto the server so that you can navigate the site via http://localhost:4000