创建任务
任务是 Grunt 的基本需要,是你经常要做的事,比如 JS 验证或单元测试。每当运行 Grunt 时,你可以指定一个或多个任务,这些任务用于告诉 Grunt 你想要它做什么事情。
如果你没有指定任务,但是已经定义了一个名为 "default" 的任务,则默认运行此任务。
别名任务
如果指定了任务列表,则新任务是这一个或多个指定任务的别名。当运行这个别名任务时,taskList
中指定的每个任务都会依次执行。taskList
参数必须是任务数组。
grunt.registerTask(taskName, [description, ] taskList)
下面的例子定义了 "default" 任务,如果执行 Grunt 时没有指定任何任务,则它将自动运行 "jshint"、"qunit"、"concat" 和 "uglify" 任务。
grunt.registerTask('default', ['jshint', 'qunit', 'concat', 'uglify']);
可以给任务指定参数。下面的例子中,别名任务 "dist" 会运行 "concat" 和 "uglify" 两个任务,并且它们都带有 "dist" 参数。
grunt.registerTask('dist', ['concat:dist', 'uglify:dist']);
多重任务
当运行多重任务时,Grunt 会从项目的配置对象中查找同名属性。多重任务可以有多个配置,并且可以使用任意命名的目标。
如果指定任务和目标 grunt concat:foo
或 grunt concat:bar
,则只处理指定目标的配置;如果运行grunt concat
,则会处理所有的目标。
grunt.registerMultiTask(taskName, [description, ] taskFunction)
例子,如果运行 grunt log:foo
,则输出 foo: 1,2,3
;如果运行 grunt log:bar
,则输出 bar: hello world
;如果运行 grunt log
, 则先输出 foo: 1,2,3
,然后 bar: hello world
,最后 baz: false
。
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);
});
基本任务
当运行基本任务时,Grunt 不会检查配置和环境 -- 它仅仅执行指定的任务函数,并传递任何使用冒号分割的参数。
grunt.registerTask(taskName, [description, ] taskFunction)
例子,如果运行 grunt foo:testing:123
,则输出 foo, testing 123
;如果运行 grunt foo
,则输出 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);
}
});
自定义任务
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" finishes, 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 task...');
// And some async stuff.
setTimeout(function() {
grunt.log.writeln('All done!');
done();
}, 1000);
});
任务可以访问自身名称和参数。
grunt.registerTask('foo', 'My "foo" task.', function(a, b) {
grunt.log.writeln(this.name, a, b);
});
// Usage:
// grunt foo foo:bar
// logs: "foo", undefined, undefined
// logs: "foo", "bar", undefined
// grunt foo:bar:baz
// logs: "foo", "bar", "baz"
如果遇到错误,任务会中止。
grunt.registerTask('foo', 'My "foo" task.', function() {
if (failureOfSomeKind) {
grunt.log.error('This is an error message.');
}
// Fail by returning false if this task had errors
if (ifErrors) { return false; }
grunt.log.writeln('This is the success message');
});
当任务中止时,所有后续任务都将终止,除非指定 --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);
});
任务可以依赖于其他任务的成功执行。grunt.task.requires
并不会真正的运行其他任务,它仅仅检查其它任务是否已经成功执行。
grunt.registerTask('foo', 'My "foo" task.', function() {
return false;
});
grunt.registerTask('bar', 'My "bar" task.', function() {
// Fail task if "foo" task failed or never ran.
grunt.task.requires('foo');
// This code executes if the "foo" task ran successfully.
grunt.log.writeln('Hello, world.');
});
// Usage:
// grunt foo bar
// doesn't log, because foo failed.
// grunt bar
// doesn't log, because foo never ran.
如果任务需要的配置属性不存在,则任务中止。
grunt.registerTask('foo', 'My "foo" task.', function() {
// Fail task if "meta.name" config prop is missing.
grunt.config.requires('meta.name');
// Also fails if "meta.name" config prop is missing.
grunt.config.requires(['meta', 'name']);
// Log... conditionally.
grunt.log.writeln('This will only log if meta.name is defined in the config.');
});
任务可以访问配置属性。
grunt.registerTask('foo', 'My "foo" task.', function() {
// Log the property value. Returns null if the property is undefined.
grunt.log.writeln('The meta.name property is: ' + grunt.config('meta.name'));
// Also logs the property value. Returns null if the property is undefined.
grunt.log.writeln('The meta.name property is: ' + grunt.config(['meta', 'name']));
});
异步任务
Grunt 使用同步的编码风格,可以在任务中调用 this.async 将其转换为异步的。
例子:
grunt.registerTask('asyncme', 'My asynchronous task.', function() {
var done = this.async();
doSomethingAsync(done);
});