Grunt (software)
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 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.[5] Companies and projects that use Grunt include Adobe Systems, jQuery, Twitter, Mozilla, Bootstrap, Cloudant, Opera, WordPress, Walmart, and Microsoft.[5] OverviewGrunt 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.[6] 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.[7] Basic conceptsCommand-line interfaceGrunt's command-line interface (CLI) can be installed globally through npm. Executing the FilesTo use Grunt in a project, two specific files need to be created in the root directory, namely package.json and a Gruntfile.
TasksTasks 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 ExampleThe 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 PluginsPlugins 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 Developers can even create their own Grunt plugins by using the AdvantagesThe following are some of the advantages of using Grunt:
ComparisonAntAnt 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 format.[7] RakeRake 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.[7] GulpGulp.js 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 between the tasks.[7] See also
References
Further reading
External links |
Portal di Ensiklopedia Dunia