public
Last active

Sample grunt-jekyll grunt.js

  • Download Gist
grunt.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
// Sample grunt-jekyll grunt.js file
// https://github.com/dannygarcia/grunt-jekyll
 
/*global module:false*/
module.exports = function(grunt) {
 
// Project configuration.
grunt.initConfig({
 
jekyll: {
server : {
src : 'templates',
dest: 'dev',
server : true,
server_port : 8000,
auto : true
},
dev: {
src: 'templates',
dest: 'dev'
},
prod: {
src: 'templates',
dest: 'prod'
}
},
 
watch: { // for development run 'grunt watch'
jekyll: {
files: ['templates/*.html'],
tasks: ['jekyll:dev']
}
}
});
 
// Default task. Run standard jekyll server.
grunt.registerTask('default', 'jekyll:server');
 
// plugin tasks
grunt.loadNpmTasks('grunt-jekyll');
};
readme.markdown
Markdown

Sample grunt-jekyll grunt.js

  1. Use the following folder structure
[root]
    |-- templates/
        |-- index.html (Jekyll templates)
    |-- prod/
    |-- dev/
    |-- _config.yml (optional)
  1. Add the grunt.js file below at the root of your project.

  2. Install the grunt-jekyll gruntmodule next to your project's grunt.js gruntfile with: npm install grunt-jekyll.

  3. Run the grunt jekyll commands:

  • grunt jekyll or grunt jekyll:server to start a server that watches the templates directory.

  • grunt jekyll:dev to process files to the dev directory.

  • grunt jekyll:prod to process files to the prod directory.

Of course, everything here is fully configurable to your liking.

Is there a way to run the server instantly at "watch"? It seems to load only after I make a change to some watched file. Probably missing something obvious here.

EDIT: Solved this using the server task and an alias (grunt.registerTask('default', 'server watch');).

With the new Grunt.js, it worked for me:
grunt.registerTask('default', ['server', 'watch']);

To use the watch functionality do:

npm install grunt-contrib-watch --save-dev

and add

grunt.loadNpmTasks('grunt-contrib-watch');

To your grunt file.

What is the final file supposed to look like?

https://gist.github.com/Wolfr/5332250 returns a warning

Warning: Task "server" not found. Use --force to continue.

using Jekyll 1.0.2, you might need to change the keys "src" and "dest" respectively into "source" and "destination", in order to correctly run the task

Hey, Danny

I setup my Gruntfile.js to reflect the above, but for some reason it keeps building instead of serving the site and the watch doesn't start. It seems to be picking up the defaults from my _config.yml file instead of using the one's specified in my grunt file e.g. I tell it to build to a dev folder, but it keeps building to _site. Any ideas?

jekyll: {
      server : {
        dest: 'dev',
        server : true,
        server_port : 8000,
        auto : true
      },
      dev: {
        dest: 'dev'
      }
    },

    watch: { // for development run 'grunt watch'
      jekyll: {
        files: ['templates/*.html'],
        tasks: ['jekyll:dev']
      }
    }

@tomireland, I think this sample file is out of date. Or at least I don't think the syntax for the server options are set quite right. Yeah, in fact the option "server: true" needs to be "serve: true" with the current version of grunt-jekyll, and should be wrapped in an "options" object : eg options : {serve: true}. Also the rest of the options are just that, options and the default jekyll build and serve will work fine.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.