Getting started with Grunt

by JuanMa Garrido

Grunt Logo Ironhak Logo

Getting started with GRUNT

Teacher: JuanMa Garrido

Contents

  1. What is Grunt? (5m)
  2. Clear Ideas about Grunt (10m)
  3. Using Grunt in an existing project (10m)
  4. Creating my first Grunt project (10m)
  5. Loading Grunt plugins (10m)
  6. Clear Ideas about Gruntfile.js (10m)
  7. Fully functional Grunt project (...)

Grunt is a Task Runner

Grunt is a Build Tool

Grunt is a Command Line

Grunt is a (Javascript) Task Runner. Some of these tasks Build stuff (transcompiling, deploy...)

Originally described as:

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

and for Ruby developers...

Grunt is like a javascript version of Ruby's Rake

Clear Ideas about Grunt

Clear Ideas about Grunt

Clear Ideas about Grunt

{
  "name": "my-project-name",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "~0.4.5",
    "grunt-contrib-jshint": "~0.10.0",
    "grunt-contrib-nodeunit": "~0.4.1",
    "grunt-contrib-uglify": "~0.5.0"
  }
}

Clear Ideas about Grunt

module.exports = function(grunt) {
  grunt.initConfig({
    jshint: {
      files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'],
      options: {
        globals: {
          jQuery: true
        }
      }
    },
    watch: {
      files: ['<%= jshint.files %>'],
      tasks: ['jshint']
    }
  });
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.registerTask('default', ['jshint']);
};

Clear Ideas about Grunt

Clear Ideas about Grunt

Clear Ideas about Grunt

Using Grunt in an existing project

Using Grunt in an existing project

$ git --version
$ node -v
$ npm -v
$ npm install -g grunt-cli
$ git clone https://github.com/juanmaguitar/grunt-workshop.git
$ cd grunt-workshop
$ npm install
$ grunt --version
$ grunt -h
$ grunt tasks
$ grunt compass
$ grunt shower
$ grunt serve

Using Grunt in an existing project

[+]

Creating my first Grunt project

Creating my first Grunt project

.
├── Gruntfile.js
├── package.json
$ mkdir project
$ cd project/
$ mkdir src
$ npm init
$ npm install --save-dev grunt
$ vi Gruntfile.js
    module.exports = function(grunt) {
        grunt.registerTask('foo', function() {
               grunt.log.writeln('foo is running...');
        });
    };
$ grunt foo

Creating my first Grunt project

[+]

Loading Grunt plugins

Loading Grunt plugins

.
├── Gruntfile.js
├── package.json
└── src
    └── foo.js
$ mkdir project2
$ cd project2/
$ mkdir src
$ npm init
$ npm install --save-dev grunt grunt-contrib-jshint

Loading Grunt plugins

$ vi Gruntfile.js
    module.exports = function(grunt) {
      grunt.loadNpmTasks('grunt-contrib-jshint');
      grunt.initConfig({
        jshint: {
          options: {
            curly: true,
            eqeqeq: true
          },
          target1: ['Gruntfile.js', 'src/**/*.js']
        }
      });
      grunt.registerTask('default', ['jshint']);
    };
$ grunt

Loading Grunt plugins

[+]

Clear Ideas about Gruntfile.js

Clear Ideas about Gruntfile.js

Clear Ideas about Gruntfile.js

Fully functional Grunt project

├── Gruntfile.js
├── dist
├── index.html
├── package.json
├── src
│   ├── js
│   │   ├── bar.js
│   │   └── foo.js
│   ├── scss
│   │   └── styles.scss
│   └── vendor
│       └── jquery-2.1.3.js
└── test
  ├── barSpec.js
  └── fooSpec.js

Fully functional Grunt project

Fully functional Grunt project

Fully functional Grunt project (extras)

##More info