I already wrote about the awesomeness of GruntJS and here is how to set up your own Grunt system.
1. Installation
GruntJS runs on nodejs so you need that installed first. Once nodejs and npm are installed, use the following command to install Grunt (the -g flag installs it globally).
npm install -g grunt-cli
This installs the grunt cli tool, next you need to create your project deployment. Navigate to your target project directory and type the following commands:npm init
npm install gruntYou can safely use the defaults suggested during the npm init process; this process creates a package.json file which contains information about the licence, repository, author and blah blah blah. You can also use pre-created package.json files to tell the node package manager what dependencies to install. Make sure you keep the package.json file safe – it can be used to recreate your project or share project details with other developers. If you get weird errors, try prefixing the commands with sudo. Type grunt now and if you this error: ‘A valid Gruntfile could not be found…‘; then give yourself a pat on the back! :)
2. Using Grunt
When installing new plugins, remember to add the –save-dev switch so that your package.json file is updated. For example, to add the requirejs plugin, you’d type something like this:
npm install grunt-contrib-requirejs –save-dev
Once you’ve installed the plugins, you need to instruct Grunt to load these plugins. Adding grunt.loadNpmTasks(‘grunt-contrib-requirejs’) to your Gruntfile will fix this.
Next, you need to specify the tasks in the Gruntfile; you set up the plugin’s config options – details such as location of input files, the name and location of its output and some other options depending on the plugin. After this, you register the task with the grunt.registerTask() hook. See the sample config file for a demo.
3. Sample Grunt config
3. Sample Grunt config
The Gist below shows a sample commented Gruntfile. The default task is run whenever you type grunt at the CLI; however to run other tasks you need to specify the task name; in the sample file below, typing grunt test will run the jshint:js and qunit tasks.
module.exports = function(grunt) { | |
grunt.initConfig({ | |
qunit: { | |
files: ['test/**/*.html'] | |
}, | |
jshint: { //Lint files, different tasks exist | |
tests: { //lint JS tests | |
files: { | |
src: ['test/**/*.js'] | |
}, | |
options: { | |
globals: { | |
jQuery: true, | |
console: true, | |
module: true, | |
document: true | |
} | |
} | |
}, | |
target: { //lint production JS | |
files: { | |
src: ['target/controllers/*.js', | |
'target/models/*.js', | |
'target/routes/*.js', | |
'target/views/*.js', | |
'target/app.js', | |
'target/main.js'] | |
}, | |
options: { | |
globals: { | |
jQuery: true, | |
console: true, | |
module: true, | |
document: true | |
} | |
} | |
} | |
}, | |
watch: { //Watch files for changes | |
scripts: { | |
options: { | |
livereload: true | |
}, | |
files: ['./js/**/*'], | |
tasks: ['jshint'] | |
} | |
}, | |
compass: { //Compile SASS into CSS | |
dist: { | |
options: { | |
config: './public/config.rb', | |
sassDir: './public/css/scss/', | |
cssDir: './public/dump', | |
imagesDir: './public/images', | |
outputStyle: 'compressed', | |
noLineComments: true | |
} | |
}, | |
dev: { | |
options: { | |
config: './public/config.rb', | |
sassDir: './public/css/scss/', | |
cssDir: './public/css' | |
} | |
} | |
}, | |
cssmin: { //minify CSS | |
minify: { | |
files: { | |
'./public/css/prd.min.css': ['./public/css/styles.css'] | |
}, | |
options: { | |
report: 'gzip' | |
} | |
} | |
} | |
}); | |
grunt.loadNpmTasks('grunt-contrib-uglify'); | |
grunt.loadNpmTasks('grunt-contrib-jshint'); | |
grunt.loadNpmTasks('grunt-contrib-qunit'); | |
grunt.loadNpmTasks('grunt-contrib-watch'); | |
grunt.loadNpmTasks('grunt-contrib-concat'); | |
grunt.loadNpmTasks('grunt-sass'); | |
grunt.loadNpmTasks('grunt-contrib-cssmin'); | |
grunt.registerTask('test', ['jshint:js', 'qunit']); | |
grunt.registerTask('prod', ['jshint:js', 'compass:dist', 'cssmin']); | |
grunt.registerTask('default', ['jshint:js', 'compass:dev', 'compass:dist']); | |
}; |
4. What do these tasks mean?
4. What do these tasks mean?
Related articles
- Grunting with GruntJS (abdulapopoola.wordpress.com)
- Use Grunt.js and the Power of JavaScript to Automating Repetitive Tasks (strongloop.com)
Related articles
- Grunting with GruntJS (abdulapopoola.wordpress.com)
- Use Grunt.js and the Power of JavaScript to Automating Repetitive Tasks (strongloop.com)
3 thoughts on “Automate Builds using GruntJS”