Created by Jackie Keiser
It helps you be lazy automate repetitive tasks.
Grunt can take care of many of the repetitive tasks we do everyday.
Grab a copy of the example project to work from.
(https://github.com/MongooseDoom/grunt-example)
Examples of the end result are in the examples
folder within the project.
npm install -g grunt-cli
Note: This does not install the task runner. It just allows you to run multiple versions of Grunt. Grunt works more on a per project basis.
You'll need to setup these two files in the root of your project
npm init
General info about your project and manages dependencies
{
"name": "donuts-are-delicious",
"version": "0.1.0",
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-imagemin": "^0.8.0"
}
}
What else you could add to your package.json fileThe main setup file that contains the configuration of the
tasks we install.
module.exports = function(grunt) {
// 1. All configuration goes here
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// 2. Configuration for each task
imagemin: {
...
}
});
// 3. Where we tell Grunt we plan to use this plug-in.
grunt.loadNpmTasks('grunt-contrib-imagemin');
// 4. Where we tell Grunt what to do when we type "grunt" into the terminal.
grunt.registerTask('default', ['imagemin']);
};
Once you have your package.json file you can install all the dependencies you specified using the following command:
npm install
In the package.json example, we listed grunt
and grunt-contrib-imagemin
as our dependencies.
npm install grunt-contrib-sass --save-dev
The --save-dev
flag means our package.json file gets automatically updated to include the dependency we've just installed
grunt.loadNpmTasks('grunt-contrib-sass');
This tells Grunt to load tasks from the specified Grunt plugin.
grunt.initConfig({
...
sass: { // Task
dist: {
options: {
style: 'expanded', // Output style
sourcemap: true // Use source maps
},
files: {
'css/styles.css': 'scss/styles.scss' // Output path : Sass path
}
}
},
imagemin: {
dynamic: { // Another target
files: [{
expand: true, // Enable dynamic expansion
cwd: 'examples/images/', // Src matches are relative to this path
src: ['**/*.{png,jpg,gif}'], // Actual patterns to match
dest: 'images/' // Destination path prefix
}]
}
},
...
});
In this example, we have the configuration for the sass and imagemin tasks.
grunt.registerTask('default', ['sass']);
grunt.registerTask('images', ['imagemin','sprite']);
In this example, running grunt
in the command line without specifying a task will run the sass task. Running grunt images
would run the imagemin and sprite task