Grunt - 创建任务
在本章中,我们来学习创建任务。每当您运行 Grunt 时,都会指定要运行的一项或多项任务,以通知 Grunt 您希望它执行的操作。如果您指定默认任务,那么它将默认运行。
别名任务
每当指定任务列表时,一个或多个其他任务就可以用新任务作为别名。运行别名将依次运行taskList中的每个指定任务。taskList参数应该是一个任务数组,如下所示 -
grunt.registerTask(taskName, [description, ] taskList)
例如,当您使用jshint、concat和uglify任务定义一个任务列表并将任务名称指定为default时,如果执行Grunt时未指定任何任务,则所有列出的任务将自动运行 。
grunt.registerTask('default', ['jshint', 'concat', 'uglify']);
您还可以指定任务参数,如下所示 -
grunt.registerTask('dist', ['concat:dist', 'uglify:dist']);
在上面的任务中,别名dist同时运行concat和uglify任务。
多任务
每当您运行多个任务时,Grunt 都会在 Grunt 配置中搜索同名的属性。这些任务可以有多个配置,这些配置将使用任意命名的目标来定义。
当您同时指定任务和目标时,将仅处理指定的目标配置。
grunt concat:foo
上面的命令将仅运行目标foo。
当您仅指定一个任务时,将处理所有目标。
grunt concat
上面的命令将迭代concat任务的所有目标。
当您使用grunt.task.renameTask重命名任务时, Grunt 将在配置对象中搜索具有新任务名称的属性。
grunt.initConfig({
log: {
foo: [1, 2, 3],
bar: 'Welcome to tutorialspoint',
sap: true
}
});
grunt.registerMultiTask('log', 'Log stuff.', function() {
grunt.log.writeln(this.target + ': ' + this.data);
});
在上面的示例中,如果 Grunt 通过grunt log:foo运行,多任务将记录foo: 1,2,3 ,否则每当通过grunt log:bar运行时, 它将记录bar: Welcome totutorialspoint。当 Grunt 作为grunt log运行时,它将记录foo: 1,2,3然后bar: Welcome totutorialspoint然后 sap: true。
基本任务
每当您运行基本任务时,Grunt 都不会搜索配置或环境。相反,它运行指定的任务函数,将指定的任何冒号分隔的参数传递为函数参数。
grunt.registerTask(taskName, [description, ] taskFunction)
在以下示例中,任务记录foo,测试 123是否通过grunt foo:testing:123命令执行 Grunt 。每当任务在没有参数的情况下运行时,如grunt foo,该任务将记录 foo,没有 args。
grunt.registerTask('foo', 'A simple task to logs stuff.', function(arg1, arg2) {
if (arguments.length === 0) {
grunt.log.writeln(this.name + ", no args");
} else {
grunt.log.writeln(this.name + ", " + arg1 + " " + arg2);
}
});
自定义任务
如果您不想遵循多任务结构,您可以定义自定义任务,如下所示 -
grunt.registerTask('default', 'My "default" task description.', function() {
grunt.log.writeln('Currently running the "default" task.');
});
可以在另一个任务中运行一个任务,如下所示 -
grunt.registerTask('foo', 'My "foo" task.', function() {
// Enqueue bar and baz tasks, to run after foo completes, in-order.
grunt.task.run('bar', 'baz');
// Or:
grunt.task.run(['bar', 'baz']);
});
您还可以创建异步任务,如下所示 -
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 your task..');
// Run some async stuff.
setTimeout(function() {
grunt.log.writeln('Finished!');
done();
}, 1000);
});
您可以创建可以访问其名称和参数的任务,如下所示 -
grunt.registerTask('foo', 'My task "foo" .', 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"
您可以通过以下方式创建任务:每当记录任何错误时,任务都可能失败,如下所示 -
grunt.registerTask('foo', 'My task "foo" .', function() {
if (failureOfSomeKind) {
grunt.log.error('This is an error message.');
}
// If this task had errors then fail by returning false
if (ifErrors) { return false; }
grunt.log.writeln('This is success message');
});
每当任务失败时,除非指定--force ,否则所有后续任务都将终止。
grunt.registerTask('foo', 'My task "foo" .', function() {
// Fail synchronously.
return false;
});
grunt.registerTask('bar', 'My task "bar" .', function() {
var done = this.async();
setTimeout(function() {
// Fail asynchronously.
done(false);
}, 1000);
});
任务可以依赖于其他任务才能成功执行。请记住,grunt.task.requires实际上不会执行其他任务,而是只会检查它是否已执行且未失败。
grunt.registerTask('foo', 'My task "foo" .', function() {
return false;
});
grunt.registerTask('bar', 'My task "bar" .', function() {
// Fail task if foo task failed or never ran.
grunt.task.requires('foo');
// This code executes if the foo task executed successfully.
grunt.log.writeln('Hello, World.. Welcome to Tutorialspoint!..');
});
// Usage:
// grunt foo bar doesn't log, because foo failed to execute.
// **Note: This is an example of space-separated sequential commands,
// (similar to executing two lines of code: `grunt foo` then `grunt bar`)
// grunt bar doesn't log, because foo never ran.
只要找不到所需的配置属性,任务甚至可能失败。
grunt.registerTask('foo', 'My task "foo" .', function() {
// Fail task if meta.name config properties 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 only log if meta.name is defined in the config.');
});
任务可以访问配置属性,如下所示 -
grunt.registerTask('foo', 'My task "foo" .', function() {
// Log the value of the property. Returns null if the property is undefined.
grunt.log.writeln('The meta.name property is: ' + grunt.config('meta.name'));
// Also logs the value of the property. Returns null if the property is undefined.
grunt.log.writeln('The meta.name property is: ' + grunt.config(['meta', 'name']));
});