May 25, 2021 Grunt
The task is Grunt's bread and cream. J
ust like your usual tools, such
jshint
or
nodeunit
Whenever you run Grunt, you can assign it one or more tasks that tell Grunt what you want it to do.
If you don't specify a task, and you've defined a task called "default," the task will be executed by default (don't be surprised, there's always something to do!). )。
If a task list is specified, the new task will be an alias for one or more of the specified tasks. W
hen you run this "task alias," each task specified in
taskList
is executed in the order in which it appears.
taskList
parameter must be an array of tasks.
grunt.registerTask(taskName, [description, ] taskList)
The following task alias case defines a 'default' task that automatically performs 'jshint', 'qunit', 'concat', and 'uglify' tasks if no tasks are specified when running Grunt.
grunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']);
You can also specify parameters for tasks. In the following case, the alias "dist" performs both "concat" and "uglify" tasks, and they both carry a "dist" parameter:
grunt.registerTask('dist', ['concat:dist', 'uglify:dist']);
When you run a multitaser, Grunt automatically looks for properties with the same name from the project's configuration objects. Multitassing can have multiple configurations, and you can use any named 'targets'.
Tasks and targets
grunt concat:foo
or
grunt concat:bar
are specified at run
grunt concat
time, and Grunt only handles the configuration of the specified target at runtime;
Note that if a task has been renamed
with grunt.task.renameTask,
Grunt will automatically look for new task name properties in the configuration object.
Most of the contrib tasks (mainly officially provided tasks), including the jshint task of the grunt-contrib-jshint plug-in, and the concat task of the grunt-contrib-concat plug-in, are multitasked.
grunt.registerMultiTask(taskName, [description, ] taskFunction)
For the specified configuration, here is a case study that shows that if
grunt log:foo
runs Grunt, it outputs
foo: 1,2,3
and
grunt log:bar
runs Grunt,
bar: hello world
However,
grunt log
it
foo: 1,2,3
bar: hello world
and finally
baz: false
task target is processed in the specified order).
grunt.initConfig({
log: {
foo: [1, 2, 3],
bar: 'hello world',
baz: false
}
});
grunt.registerMultiTask('log', 'Log stuff.', function() {
grunt.log.writeln(this.target + ': ' + this.data);
});
When a basic task is performed, Grunt does not check the configuration and environment -- it simply executes the specified task function and passes any arguments split with a colon as an argument to the function.
grunt.registerTask(taskName, [description, ] taskFunction)
In the following case, if
grunt foo:testing:123
is performed, the log
foo, testing 123
If the parameters are not passed when this task is performed,
grunt foo
is executed, the
foo, no args
grunt.registerTask('foo', 'A sample task that logs stuff.', function(arg1, arg2) {
if (arguments.length === 0) {
grunt.log.writeln(this.name + ", no args");
} else {
grunt.log.writeln(this.name + ", " + arg1 + " " + arg2);
}
});
You can go crazy with the mission. If your task doesn't follow the "multitasth" structure, use custom tasks.
grunt.registerTask('default', 'My "default" task description.', function() {
grunt.log.writeln('Currently running the "default" task.');
});
Within one task, you can perform other tasks.
grunt.registerTask('foo', 'My "foo" task.', function() {
// Enqueue "bar" and "baz" tasks, to run after "foo" finishes, in-order.
grunt.task.run('bar', 'baz');
// Or:
grunt.task.run(['bar', 'baz']);
});
Tasks can also be asynchronous.
grunt.registerTask('asyncfoo', 'My "asyncfoo" task.', function() {
// Force task into async mode and grab a handle to the "done" function.
var done = this.async();
// Run some sync stuff.
grunt.log.writeln('Processing task...');
// And some async stuff.
setTimeout(function() {
grunt.log.writeln('All done!');
done();
}, 1000);
});
Tasks can also access their own names and parameters.
grunt.registerTask('foo', 'My "foo" task.', function(a, b) {
grunt.log.writeln(this.name, a, b);
});
// 用法:
// grunt foo foo:bar
// logs: "foo", undefined, undefined
// logs: "foo", "bar", undefined
// grunt foo:bar:baz
// logs: "foo", "bar", "baz"
If any errors are logged, the task fails.
grunt.registerTask('foo', 'My "foo" task.', function() {
if (failureOfSomeKind) {
grunt.log.error('This is an error message.');
}
// 如果这个任务出现错误则返回false
if (ifErrors) { return false; }
grunt.log.writeln('This is the success message');
});
When a task fails, all subsequent tasks are terminated unless
--force
grunt.registerTask('foo', 'My "foo" task.', function() {
// Fail synchronously.
return false;
});
grunt.registerTask('bar', 'My "bar" task.', function() {
var done = this.async();
setTimeout(function() {
// Fail asynchronously.
done(false);
}, 1000);
});
Tasks can also depend on the successful execution of other tasks.
Note
grunt.task.requires
actually run other tasks, it simply checks whether other tasks have been performed and has not failed.
grunt.registerTask('foo', 'My "foo" task.', function() {
return false;
});
grunt.registerTask('bar', 'My "bar" task.', function() {
// 如果"foo"任务运行失败或者没有运行则任务失败。
grunt.task.requires('foo');
// 如果"foo"任务运行成功则执行这里的代码。
grunt.log.writeln('Hello, world.');
});
// 用法:
// grunt foo bar
// 没有输出,因为"foo"失败。
// grunt bar
// 没有输出,因为"foo"从未运行。
If the configuration property required by the task does not exist, it may also fail.
grunt.registerTask('foo', 'My "foo" task.', function() {
// Fail task if "meta.name" config prop is missing
// Format 1: String
grunt.config.requires('meta.name');
// or Format 2: Array
grunt.config.requires(['meta', 'name']);
// Log... conditionally.
grunt.log.writeln('This will only log if meta.name is defined in the config.');
});
Tasks can also access configuration properties.
grunt.registerTask('foo', 'My "foo" task.', function() {
// 记录属性值,如果属性未定义(undefined)则返回null。
grunt.log.writeln('The meta.name property is: ' + grunt.config('meta.name'));
// 同样的记录属性值,如果属性未定义(undefined)则返回null。
grunt.log.writeln('The meta.name property is: ' + grunt.config(['meta', 'name']));
});
You can see more cases in contrib tasks.
Refer to the Use command line tools section for a complete list of command line options.
If this happens, it may be because you
forgot to call the this.async
method to tell Grunt that your task is asynchronous.
For simplicity, Grunt uses a synchronized coding style that can be converted to asynchronous in the task body by calling
this.async()
Note that
false
done()
function tells Grunt that your task has failed.
For example:
grunt.registerTask('asyncme', 'My asynchronous task.', function() {
var done = this.async();
doSomethingAsync(done);
});
If you need more resources to create your own task, refer to the API documentation