
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...)
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
npm install -g grunt-clinpm install --save-dev grunt grunt-contrib-jshintnpm installpackage.json: The Grunt runner and Grunt plugins used in the Gruntfile.js are set as project dependencies in this fileGruntfile.js: The tasks are defined/configured and grunt plugins are loaded in this file {"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"}}
Gruntfile.js: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']);};
Steps:
npm installgrunt$ 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
Steps:
package.json interactively with npm initGruntfile.js (simple task, no plugin loading).├── Gruntfile.js├── package.json
$ mkdir project$ cd project/$ mkdir src$ npm init$ npm install --save-dev grunt$ vi Gruntfile.jsmodule.exports = function(grunt) {grunt.registerTask('foo', function() {grunt.log.writeln('foo is running...');});};$ grunt foo
Steps:
package.json interactively with npm initGruntfile.js (as defined here).├── Gruntfile.js├── package.json└── src└── foo.js
$ mkdir project2$ cd project2/$ mkdir src$ npm init$ npm install --save-dev grunt grunt-contrib-jshint
$ vi Gruntfile.jsmodule.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
grunt.initConfig({...}) → Configuration objectgrunt.initConfig({pkg: grunt.file.readJSON('package.json')uglify: {options: {// the banner is inserted at the top of the outputbanner: '/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n'},dist: {files: {'dist/<%= pkg.name %>.min.js': ['<%= concat.dist.dest %>']}}}});
grunt.loadNpmTasks(...); → Load the Grunt plugins.grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.registerTask(...); → Aliases for already loaded/created tasksgrunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']);├── 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
Given the previous structure, create a Grunt project that provides the following tasks:
grunt concat → concatenate all .js files at src folder (using ; as separator) in a npm-project-named .js file placed at dist folder
grunt uglify → uglify file generated by concat task into a new .min.js file placed at dist foldergrunt jshint → validate the js code (Gruntfile.js, src/js, test)grunt compass → compile src/styles.scss file into a dist/styles.css grunt or grunt default→ launches all previous tasks (jshint, concat, uglify, compass)
grunt watch → watch any change done at Gruntfile.js or src folder and launches default task w/ new changes
grunt jasmine → launch all jasmine tests at test folder (add it to default task)grunt serve → launch a local server at localhost:8081 (after launching default task)