Grunt.js

The JavaScript Task Runner

http://gruntjs.com

Vitaliy Petrychuk

Front-End Developer @SoftServe

Agenda

  • What is Grunt?
  • Why to use Grunt?
  • How to use Grunt?

What is Grunt?

Grunt is a task-based command-line build tool for JavaScript projects.

Task-based?

  • JS/CSS/HTML concatenation/minification
  • JS linting (JSLint, JSHint)
  • Unit testing
  • SASS/LESS preprocessing
  • ...

Why to use task runner?

Automation

Why to use Grunt?

  • Large Community
  • Easy to use
  • Hundreds of plugins
  • Everybody uses it

How to use Grunt?

Install Node.js + npm

http://nodejs.org

Node.js is a software platform that is used to build scalable network server-side applications.

Install Grunt CLI

Grunt's Command Line Interface (CLI)
should be installed globally:

$ npm install -g grunt-cli

Install Grunt

$ npm install grunt --save-dev

This will install the latest version of Grunt in your current directory and will add grunt as a dependency to devDependencies in your package.json file.

Configure project

  • package.json
  • Gruntfile.js

package.json

{
  "name" : "project-name",
  "version" : 0.0.1,
  "devDependencies" : {
    "grunt" : "~0.4.1",
    "grunt-contrib-jshint" : "~0.6.4"
  }
}

After executing command below npm will install all defined dependencies:

$ npm install

Gruntfile.js

module.exports = function (grunt) {

  // Configure tasks
  grunt.initConfig({
    jshint : {
      options : {
        curly : true,
        eqnull : true
      },
      all : ['Gruntfile.js', 'lib/**/*.js', 'test/**/*.js']
    }
  });

  // Load plugins
  grunt.loadNpmTask('grunt-contrib-jshint');

  // Register custom tasks
  grunt.registerTask('default', ['jshint']);
};

Demo

default task refers to the jshint task.
default is a default Grunt task.

So these commands:

$ grunt
$ grunt default
$ grunt jshint

Do the same thing:

$ grunt
Running "jshint:dist" (jshint) task
>> 42 files lint free.

Done, without errors.

Conclusion

Questions?