How to customize Grunt tasks

Source: Internet
Author: User

I. Preparation 1. create a directory g12. create a package. json, put it in g13. create Gruntfile. js, put the g1 package. json {"name": "g1", "version": "0.1.0", "author": "@ snandy", "homepage": "http://www.g1.com", "devDependencies ": {"grunt ":"~ 0.4.0 "} 4. cd enters g1, and npm install installs the grunt package. The entire directory structure is as follows: Gruntfile. js. 2. Create a simplest task, grunt. registerTask (taskName, [description,] taskFunction) taskName task name. In the command line, use the description of the grunt + taskNamedescription task taskFunction task implementation Gruntfile. enter the code module in js. exports = function (grunt) {grunt. registerTask ('mytask', 'is the simplest task demonstration. Different outputs are printed based on parameters. ', function (arg1, arg2) {if (arguments. length = 0) {grunt. log. writeln ('task' + this. name + ", no parameter");} else if (arguments. length = 1) {grunt. l Og. writeln ('task' + this. name + ", there is a parameter" + arg1);} else {grunt. log. writeln ('task' + this. name + ", with two parameters:" + arg1 + "," + arg2) ;}};}; registered a task "mytask ", the simplest way is to implement different print outputs based on different passed parameters. To view the running results, we need to enter the command line. Enter the g1 directory, enter grunt mytask, then enter grunt mytask: snandy, and add a colon to the task name. Then you can input grunt mytask: snandy: the backus colon interval can be set to multiple parameters. 3. Create multiple tasks at a time. registerMultiTask (taskName, [description,] taskFunction) can see that the parameters are the same and the method names are different. However, the usage is not the same. You need to initialize config and Gruntfile first. the js module is as follows. exports = function (grunt) {grunt. initConfig ({log: {t1: [1, 2, 3], t2: 'Hello world', t3: false}); grunt. registerMultiTask ('log', 'Log stuff. ', function () {grunt.log.writeln(this.tar get +': '+ this. data) ;};}; go to the g1 directory and test grunt respectively. Three subtasks t1, t2, and t3 are run in sequence, and grunt log: t1 and grunt log: t2, grunt log: t3 4. Inter-task communication within a task can call another task, as shown in the following module. exports = function (grunt) {grunt. registerTask ('mytask', 'is the simplest task demonstration. Different outputs are printed based on parameters. ', function (arg1, arg2) {if (arguments. length = 0) {grunt. log. writeln ('task' + this. name + ", no parameter");} else if (arguments. length = 1) {grunt. log. writeln ('task' + this. name + ", there is a parameter" + arg1);} else {grunt. log. writeln ('task' + this. name + ", which has two parameters:" + arg1 + "," + arg2) ;}}); grunt. registerTask ('default', 'default task', function () {// call mytask grunt. task. run ('mytask: param1: param2')}; enter the command line, enter grunt to call multiple tasks, and send them to the run method by commas (,), or in the form of an array. registerTask ('default', 'default task', function () {grunt. task. run ('mytask1 ', 'mytask2') // or grunt. task. run (['mytask1 ', 'mytask2'])

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.