Grunt (software)
Original author(s) | Ben Alman |
---|---|
Developer(s) | Ben Alman, Tyler Kellen, Kyle Robinson Young, Vlad Filippov, Sindre Sorhus, Isaac Durazo, Jarrod Overson, Tim Branyen, Jörn Zaefferer, James Smith, Dave Geddes |
Initial release | 11 January 2012 |
Stable release |
1.0.0
/ 4 April 2016 |
Development status | Active |
Written in | Node.js |
Operating system | Linux, Windows, OS X |
Available in | English |
Type | Task Runner, Build tool |
License | MIT License [1] |
Website |
gruntjs |
Grunt is a JavaScript task runner, a tool used to automatically perform frequently used tasks such as minification, compilation, unit testing, linting, etc. It uses a command-line interface to run custom tasks defined in a file (known as a Gruntfile). Grunt was created by Ben Alman and is written in Node.js. It is distributed via npm.
Presently, there are more than five thousand plugins available in the Grunt ecosystem.[2]
Companies that use Grunt include Adobe Systems, jQuery, Twitter, Mozilla, Bootstrap, Cloudant, Opera, WordPress, Walmart and Microsoft.[2]
Overview
Grunt was originally created by Ben Alman in 2012, as an efficient alternative to simply writing and maintaining a suite of "javascript build process" tasks in one huge file. It was designed as a task-based command line build tool for JavaScript projects.[3]
Grunt is primarily used to automate tasks that need to be performed routinely. There are thousands of plugins that can be installed and used directly to perform some commonly used tasks. One of Grunt's most desirable features is that it is highly customizable i.e it allows developers to add, extend and modify custom tasks to fit their personal requirements. Each task has a set of configuration options that can be set by the user to meet their specific needs. Moreover, Grunt offers the ability to define custom tasks, which can combine multiple existing tasks into a single task or add entirely new functionality.[4]
Basic Concepts
Command-Line Interface
Grunt's command-line interface (CLI) can be installed globally through npm. Executing the grunt
command will load and run the version of Grunt locally installed in the current directory. Hence, we can maintain different versions of Grunt in different folders and execute each one as we wish.[2]
Files
To use Grunt in a project, two specific files need to be created in the root directory, namely package.json and a Gruntfile.
- package.json - contains the metadata for the project including name, version, description, authors, licenses and its dependencies (Grunt plugins required by the project). All the dependencies are listed either in the
dependencies
or thedevDependencies
section. - Gruntfile - a valid JavaScript or CoffeeScript file named "Gruntfile.js" or "Gruntfile.coffee" that contains code to configure tasks, load existing plugins and/or create custom tasks.
Tasks
Tasks are the modules that perform a specified job. They are defined in the Gruntfile.
Developers can load predefined tasks from existing Grunt plugins and/or write custom code to define their own tasks depending on their requirements. Once defined, these tasks can be run from the command line by simply executing grunt <taskname>
. If the <taskname>
defined in the Gruntfile is 'default' then simply executing grunt
will suffice.
Example
The following is an example of a Gruntfile written in JavaScript that shows how to load plugins, create custom tasks and configure them:
module.exports = function(grunt) { //task configuration grunt.initConfig({ taskName1: 'Task1 Configuration', taskName2: 'Task2 Configuration' }); // loads plugins grunt.loadNpmTasks('pluginName1'); grunt.loadNpmTasks('pluginName2'); //custom tasks grunt.registerTask('customTaskName1', 'Custom task description', function(taskParameter) { //custom statements }); // combining multiple tasks to a single task grunt.registerTask('customTaskName2', ['taskName1,customTaskName1']); // default task- runs if task name is not specified grunt.registerTask('default', ['customTaskName2']); };
In the above example, executing the grunt
command will run <customtaskName2>
which has been defined above as a combination of both <taskName1>
and <customTaskName1>
.
Plugins
Plugins are reusable code that defines a set of tasks. Each plugin internally contains a tasks directory with JavaScript files that have the same syntax as a Gruntfile. Most of the grunt plugins are published with the keyword gruntplugin
[5] in npm and prefixed with grunt. This helps grunt in showing the all the plugins in Grunt's plugin listing. The plugins officially supported by Grunt are prefixed with grunt-contrib
[5] and are also marked with star symbol in the plugins listing. Some popular plugins include grunt-contrib-watch, grunt-contrib-clean, grunt-contrib-uglify etc.
Developers can even create their own Grunt plugins by using the grunt-init
plugin and publish them to npm using the npm publish
command.
Advantages
The following are some of the advantages of using Grunt:
- all task runners have the following properties: consistency, effectiveness, efficiency, repeatability etc.
- access to many predefined plugins that can be used to work with JavaScript tasks and on static content.
- allows users to customize tasks using predefined plugins.
- prefers the configuration approach to coding.
- allows users to add their own plugins and publish them to npm.
Comparison
Ant
Ant or Apache Ant is a Java-based build tool. Ant has a little over hundred built-in tasks that are better suited to projects with a Java build structure. Writing custom code in Ant requires users to write a JAR file and reference it from XML. This would add unnecessary complexities to projects that do not require Java themselves. Ant build configurations are listed in XML rather than in JSON format.[4]
Rake
Rake allows developers to define tasks in Ruby. Rake doesn't have the concept of plugins or predefined tasks which means all the required actions must be written and then executed. This makes the developments costly when compared to Grunt which has a large set of reusable plugins.[4]
Gulp
Gulp.js is a relatively new JavaScript based task runner tool. It is similar to Grunt since both follow a modular-based architecture and are based on npm. Gulp tasks are defined by code rather than configuration. Gulp is faster than Grunt. Grunt uses temporary files to transfer output from one task to another whereas in Gulp files are piped between the tasks.[4]
See also
- Node.js
- Npm
- Build automation
- List of build automation software
- Apache Maven
- Rake
- Yeoman (computing)
- Modernizr
References
- ↑ "Grunt License - NPM".
- 1 2 3 "Grunt: The JavaScript Task Runner". gruntjs.com. Retrieved 2016-09-14.
- ↑ "Introducing Grunt - Open Source, Performance, Tools & Workflow - Bocoup". bocoup.com. Retrieved 2016-09-14.
- 1 2 3 4 Cryer, James (2015). Pro Grunt.js. Apress. p. 1. ISBN 978-1-4842-0013-1.
- 1 2 Pillora, Jaime (2014). Getting Started with Grunt: The JavaScript Task Runner. Livery Place 35 Livery Street Birmingham B3 2PB, UK.: Packt Publishing Ltd. ISBN 978-1-78398-062-8.
Further reading
- Cryer, James (2015). Pro Grunt.js. Apress. ISBN 978-1-4842-0013-1.
- Pillora, Jamie (2014). Getting Started with Grunt: The JavaScript Task Runner. Packt Publishing Ltd. ISBN 978-1-78398-062-8.