Grunt (software)
   HOME

TheInfoList



OR:

Grunt is a JavaScript task runner, a tool used to automatically perform frequent tasks such as minification, compilation, unit testing, and linting. It uses a
command-line interface A command-line interpreter or command-line processor uses a command-line interface (CLI) to receive commands from a user in the form of lines of text. This provides a means of setting parameters for the environment, invoking executables and pro ...
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. As of October 2022, there were more than 6,000 plugins available in the Grunt ecosystem. Companies that use Grunt include
Adobe Systems Adobe Inc. ( ), originally called Adobe Systems Incorporated, is an American multinational computer software company incorporated in Delaware and headquartered in San Jose, California. It has historically specialized in software for the crea ...
, jQuery, Twitter, Mozilla, Bootstrap, Cloudant, Opera, WordPress, Walmart, and Microsoft.


Overview

Grunt was originally created by Ben Alman in 2012 as an efficient alternative to simplify 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. 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 accomplish 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 needs; each task has a set of configuration options that the user can set. Moreover, Grunt offers the ability to define custom tasks, which can combine multiple existing tasks into a single task or add entirely new functionality.


Basic concepts


Command-line interface

Grunt's
command-line interface A command-line interpreter or command-line processor uses a command-line interface (CLI) to receive commands from a user in the form of lines of text. This provides a means of setting parameters for the environment, invoking executables and pro ...
(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.


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 Metadata is "data that provides information about other data", but not the content of the data, such as the text of a message or the image itself. There are many distinct types of metadata, including: * Descriptive metadata – the descriptive ...
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 the devDependencies 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 . If the 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) ; In the above example, executing the grunt command will run which has been defined above as a combination of both and .


Plugins

Plugins Plug-in, plug in or plugin may refer to: * Plug-in (computing) is a software component that adds a specific feature to an existing computer program. ** Audio plug-in, adds audio signal processing features ** Photoshop plugin, a piece of software t ...
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 in npm and prefixed with grunt. This helps Grunt in showing all the plugins in Grunt'
plugin listing
The plugins officially supported by Grunt are prefixed with grunt-contrib and are also marked with a star symbol in the plugins listing. Some popular plugins includ
grunt-contrib-watchgrunt-contrib-cleangrunt-contrib-uglify
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 a 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 JSON (JavaScript Object Notation, pronounced ; also ) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other ser ...
format.


Rake

Rake Rake may refer to: * Rake (stock character), a man habituated to immoral conduct * Rake (theatre), the artificial slope of a theatre stage Science and technology * Rake receiver, a radio receiver * Rake (geology), the angle between a feature on a ...
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.


Gulp

Gulp.js gulp is an open-source JavaScript toolkit created by Eric Schoffstall used as a streaming build system (similar to a more package-focused Make) in front-end web development. It is a task runner built on Node.js and npm, used for automation o ...
is a JavaScript based task runner tool 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 A pipe is a tubular section or hollow cylinder, usually but not necessarily of circular cross-section, used mainly to convey substances which can flow — liquids and gases (fluids), slurries, powders and masses of small solids. It ...
between the tasks.


See also

* Node.js * Npm * Build automation * List of build automation software * Apache Maven *
Rake Rake may refer to: * Rake (stock character), a man habituated to immoral conduct * Rake (theatre), the artificial slope of a theatre stage Science and technology * Rake receiver, a radio receiver * Rake (geology), the angle between a feature on a ...
* Yeoman (computing) *
Modernizr Modernizr is a JavaScript library that detects the features available in a user's browser. This lets web pages avoid unsupported features by informing the user their browser isn't supported or loading a polyfill. Modernizr aims to provide feat ...
* JavaScript framework * JavaScript library


References


Further reading

* *


External links

* * {{Github, gruntjs/grunt, Grunt Automation software JavaScript programming tools