Skip to content

Instantly share code, notes, and snippets.

@joeyred
Last active December 13, 2016 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeyred/e21dac6ae445928ca1c5612ca25fcab8 to your computer and use it in GitHub Desktop.
Save joeyred/e21dac6ae445928ca1c5612ca25fcab8 to your computer and use it in GitHub Desktop.
Use Jekyll with gulp.js while leveraging the use of a `--production` flag when running `gulp` to set the environment in Jekyll to production as well.

A Gulp Task for integrating Jekyll into a Gulp workflow

This is just the simple, but highly effective, task to use Jekyll without having to be chained to how its development server, preprocessing, and so on.

NPM Dependencies

  • gulp
npm install gulp --save-dev
  • yargs
npm install yargs --save-dev

How to Use

The DEPLOY constant is used with yargs to allow the use of a --production flag on the default gulp task. If the flag is used, then DEPLOY is true. This allows all kinds of conditional task actions in the gulpfile, but in this case, it allows leveraging Jekyll's own environment variable options. Using the child_process module native to Node.js, this is all possible.

See the documentation on the module to learn more about the methods used in this task.

var gulp = require('gulp');
var yargs = require('yargs');
var DEPLOY = Boolean(yargs.argv.production);
/**
* Jekyll Task
*
* Make Jekyll run stuff when told to by gulp.
*
* The result of this task is a CLI input based on if the '--production' flag exists.
*
* If `--production` argument is passed, then the output to the Jekyll will be:
*
* 'JEKYLL_ENV=production jekyll build'
*
* Else it will be:
*
* 'jekyll build'
*
* @see https://nodejs.org/api/child_process.htm
*/
gulp.task('jekyll', function(cb) {
var spawn = require('child_process').spawn;
// After build: cleanup HTML
var options = {stdio: 'inherit'};
// Run Jekyll build in production mode if --deploy flag is passed.
if (DEPLOY) {
var env = Object.create(process.env);
env.JEKYLL_ENV = 'production';
options.env = env;
}
var jekyll = spawn('jekyll', ['build'], options);
jekyll.on('exit', function(code) {
cb(code === 0 ? null : 'ERROR: Jekyll process exited with code: ' + code);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment