Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample grunt-jekyll grunt.js

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.

// 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');
};
@bebraw

This comment has been minimized.

Show comment
Hide comment
@bebraw

bebraw Jan 7, 2013

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');).

bebraw commented Jan 7, 2013

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');).

@caiogondim

This comment has been minimized.

Show comment
Hide comment
@caiogondim

caiogondim Mar 28, 2013

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

caiogondim commented Mar 28, 2013

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

@tarjei

This comment has been minimized.

Show comment
Hide comment
@tarjei

tarjei Apr 2, 2013

To use the watch functionality do:

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

and add

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

To your grunt file.

tarjei commented Apr 2, 2013

To use the watch functionality do:

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

and add

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

To your grunt file.

@Wolfr

This comment has been minimized.

Show comment
Hide comment
@Wolfr

Wolfr Apr 7, 2013

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.

Wolfr commented Apr 7, 2013

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.
@fcalderan

This comment has been minimized.

Show comment
Hide comment
@fcalderan

fcalderan Jun 5, 2013

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

fcalderan commented Jun 5, 2013

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

@tomireland

This comment has been minimized.

Show comment
Hide comment
@tomireland

tomireland Feb 4, 2014

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 commented Feb 4, 2014

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']
      }
    }
@YoungElPaso

This comment has been minimized.

Show comment
Hide comment
@YoungElPaso

YoungElPaso Mar 5, 2014

@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.

YoungElPaso commented Mar 5, 2014

@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.

@liquidvisual

This comment has been minimized.

Show comment
Hide comment
@liquidvisual

liquidvisual May 13, 2014

Any chance of an update? Would love to get this working with Jekyll 2.0.2

liquidvisual commented May 13, 2014

Any chance of an update? Would love to get this working with Jekyll 2.0.2

@prbaron

This comment has been minimized.

Show comment
Hide comment
@prbaron

prbaron Jun 18, 2014

@liquidvisual, I made it works by modifying these lines, allowing to the code of the plugin.

jekyll: {
    server : {
        options : {
            server : true,
            server_port : 8000,
            auto : true
        }
    }
}

prbaron commented Jun 18, 2014

@liquidvisual, I made it works by modifying these lines, allowing to the code of the plugin.

jekyll: {
    server : {
        options : {
            server : true,
            server_port : 8000,
            auto : true
        }
    }
}
@ezze

This comment has been minimized.

Show comment
Hide comment
@ezze

ezze Oct 28, 2014

I was unable to run a server with Jekyll 2.4.0 and plugin version 0.4.2 on Ubuntu 14.04 LTS. After looking at the source code of the Grunt task I got that it must be serve option instead of server.

server: {
    options: {
        serve: true,
        port: 8000,
        auto: true
    }
}

ezze commented Oct 28, 2014

I was unable to run a server with Jekyll 2.4.0 and plugin version 0.4.2 on Ubuntu 14.04 LTS. After looking at the source code of the Grunt task I got that it must be serve option instead of server.

server: {
    options: {
        serve: true,
        port: 8000,
        auto: true
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment