grunt 创建任务
在本章中,让我们学习创建任务。每当你运行Grunt时,指定一个或多个任务来运行,通知Grunt你想要做什么。如果您指定了默认任务,那么它将默认运行。
别名任务
每当指定任务列表时,一个或多个其他任务可以被新任务别名化。运行别名将依次运行taskList中的每个指定任务。taskList参数应该是一个如下所示的任务数组
grunt.registerTask(taskName, [description, ] taskList)
例如,当您使用jshint,concat和uglify任务定义taskList并将taskName指定为default时,如果在未指定任何任务的情况下执行Grunt,则所有列出的任务将自动运行。
grunt.registerTask('default', ['jshint', 'concat', 'uglify']);
您还可以指定任务参数,如下所示 -
grunt.registerTask('dist', ['concat:dist', 'uglify:dist']);
在上面的任务中,别名dist运行concat和uglify任务。
多任务
无论何时运行多个任务,Grunt都会在Grunt配置中搜索同名的属性。这些任务可以有多个配置,这些配置将使用任意命名的目标来定义。
当您同时指定任务和目标时,只会处理指定的目标配置。
grunt concat:foo
上述命令将迭代concat任务的所有目标。
当您使用grunt.task.renameTask重命名任务时,Grunt会在配置对象中搜索具有新任务名称的属性。
grunt concat
上述命令将迭代concat任务的所有目标。
当您使用grunt.task.renameTask重命名任务时,Grunt会在配置对象中搜索具有新任务名称的属性。
grunt.initConfig({
log: {
foo: [1, 2, 3],
bar: 'Welcome to codingdict',
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运行时欢迎使用教程点。它会记录foo:1,2,3然后bar:欢迎来到codingdict,然后sap:当Grunt以咕噜声记录运行时为true。
基本任务
无论何时运行基本任务,Grunt都不会搜索配置或环境。相反,它运行指定的任务函数,传递在函数参数中指定的任何以冒号分隔的参数。
grunt.registerTask(taskName, [description, ] taskFunction)
在以下示例中,任务记录foo,如果通过grunt foo执行Grunt,则测试123:testing:123命令。每当任务运行时没有参数为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 codingdict!..');
});
// 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']));
});
AI 中文社