Each time you run grunt, you can assign one or more tasks to the task that tells grunt what you want it to do.
If you do not specify a task, and you have defined a task named "Default", the task will be executed by default.
Task aliases
If you specify a task list tasklist, the new task name TaskName will be the alias of one or more of the specified tasks. When this "task alias" is run, each task specified in is executed sequentially in the taskList
order in which it appears. taskList
parameter must be an array of tasks. Grunt.registertask (TaskName, TaskList)
As an example:
Grunt.registertask (' default ', [' Jshint ', ' qunit ', ' concat ', ' uglify ']);
A ' default ' task is defined in the above task alias case, and if no task is specified when running grunt, it will automatically perform the ' Jshint ', ' qunit ', ' concat ', and ' uglify ' tasks.
You can also specify parameters for the task. In the following example, the alias "Dist" performs both "Concat" and "uglify" tasks, and they all come with a "dist" parameter:
Grunt.registertask (' dist ', [' concat:dist ', ' uglify:dist ']);
Multi-tasking
When you run a multi-tasking, Grunt automatically looks for properties with the same name from the project's configuration object. Multitasking can have multiple configurations and can use any named ' targets '. As an example:
grunt.initconfig ({ log: { foo: [1, 2, 3], ' Hello World ', False }}); Grunt.registermultitask (function() { Grunt.log.writeln ( this. data);});
If you run grunt through grunt Log:foo, it will output foo:1,2,3, and if you run grunt through Log:bar grunt, it will output Bar:hello world. However, if you run grunt through grunt log, it will output foo:1,2,3, then Bar:hello World, and finally Baz:false (the task target will be processed in the order specified).
"Basic" Tasks
When a basic task is executed, grunt does not check the configuration and environment-it simply executes the specified task function and passes any parameters that use a colon-separated argument as a function. As an example:
function (Arg1, arg2) { if (arguments.length = = = 0) { Grunt.log.writeln (this. Name) + ", no args"); Else { Grunt.log.writeln (this. Name + "," + Arg1 + "" + arg2); }});
If executed grunt foo:testing:123
, the log is output foo, testing 123
. If you do not pass parameters when performing this task, just execute grunt foo
, then the log will be output foo, no args
.
Custom tasks
If your task does not follow the multi-tasking structure, you can use a custom task. Like what:
function () { Grunt.log.writeln (' currently running the ' default ' task. ') );});
Other features of the task
Within a task, you can perform other tasks. Like what:
function () { // Enqueue "bar" and "Baz" tasks, to run after "foo" finishes, In-order. Grunt.task.run (' Bar ', ' Baz '); // Or: Grunt.task.run ([' Bar ', ' Baz ');});
The task can also be asynchronous. Like what:
function () { // Force task into async mode and grab a handle to the ' done ' function. var This . Async (); // Run some sync stuff. Grunt.log.writeln (' Processing task ... '); // and some async stuff. SetTimeout (function() { Grunt.log.writeln (' All done! ') ); Done (); ();});
Tasks can also access their own names and parameters. Like what:
grunt.registertask (' foo ', ' My ' foo ' task. ', function (A, b) {Grunt.log.writeln ( this .name, a , b);}); // usage: // grunt foo // logs:" foo ", Undefined, undefined // Grunt Foo:bar // logs:" foo "," Bar ", undefined // Grunt Foo:bar:baz // logs:" foo "," Bar "," Baz "
If the task logs any errors while it is executing, the task fails. When a task fails, all subsequent tasks are terminated. Like what:
Grunt.registertask (' foo ', ' My ' foo ' task. ',function() { return false;}); Grunt.registertask (' Bar ', ' My ' Bar ' task. ',function() { //if the "foo" task fails to run or does not run, the task fails. Grunt.task.requires (' foo ')); //if the "Foo" task runs successfully, execute the code here. Grunt.log.writeln (' Hello, world. '));});//Usage://Grunt Foo Bar//without the output, the bar task is not running because "foo" fails, so the tasks behind it are terminated. //Grunt Bar//without the output, the bar task runs, but the movement stops halfway, because "foo" has never run, so the Foo task fails, and after the failure, the bar task stops, so there is no output.
Note that Grunt.task.requires does not actually run other tasks, it simply checks to see if other tasks have been executed and has not failed.
Tasks can also access configuration properties. Like what:
function () { // record the property value and return NULL if the property is undefined (undefined). Grunt.log.writeln (' The Meta.name property is: ' + grunt.config (' Meta.name ')); // the same record property value, which returns null if the property is undefined (undefined). Grunt.log.writeln (' The Meta.name property is: ' + grunt.config ([' Meta ', ' name ']);});
If a task requires a configuration property that does not exist, it will also fail. Like what:
function () { // if the "meta.name" configuration property is missing, the task fails. grunt.config.requires (' meta.name '); // if the "mata.name" configuration property is missing, the task also fails. grunt.config.requires ([' Meta ', ' name ']); // logs are logged as appropriate. Grunt.log.writeln (' This would only log if meta.name are defined in the CONFIG. ') );});
Why is my asynchronous task not complete?
If this happens, it may be because you forget to call the This.async method to tell grunt that your task is asynchronous. For simplicity, Grunt uses a synchronized encoding style that can be converted to asynchronous in the task body by calling This.async ().
Note that passing false to the done () function will tell grunt that your task has failed.
For example:
function () { varthis. Async (); SetTimeout (
Done (false); Failed to represent ASYNCME task
();});
Come on!
Grunt Tutorial 4: How to create a task